Docker permissions development environment using a host mounted volume

Robert Moskal

I'm using docker-compose to set up a portable development environment for a bunch of symfony2 applications (though nothing I want to do is specific to symfony). I've decided to have the source files on the local machine exposed as a data volume with all the other dependencies in docker. This way developers can edit on the local file-system.

Everything works great, except that after running the app my cache and log files and the files created by composer in the /vendor directory are now owned by root.

I've read about this problem and some possible approaches here:

Changing permissions of added file to a Docker volume

But I can't quite quite tease out what changes I have to make in my docker-compose.yml file so that when my symphony container starts with docker-compose up any files that are created have the permissions of the user on the host machine.

I'm posting the file for reference, worker is where php, etc. live:

source:
    image: symfony/worker-dev
    volumes:
    - $PWD:/var/www/app
mongodb:
    image: mongo:2.4
    ports:
    - "27017:27017"
    volumes_from:
    - source
worker:
    image: symfony/worker-dev
    ports:
    - "80:80"
    - mongodb
    volumes_from:
    - source
    volumes:
    - "tmp/:/var/log/nginx"
Andrzej Ośmiałowski

One of the solutions is to execure the commands inside your container. I've tried multiple workarounds for the same issue I faced in the past. I find executing the command inside the container the most user-friendly.

Example command: docker-compose run CONTAINER_NAME php bin/console cache:clear. You may use make, ant or any modern tool to keep the commands short.

Example with Makefile:

all: | build run test

build: | docker-compose-build
run: | composer-install clear-cache

############## docker compose

docker-compose-build:
        docker-compose build

############## composer

composer-install:
        docker-compose run app composer install

composer-update:
        docker-compose run app composer update

############## cache

clear-cache:
        docker-compose run app php bin/console cache:clear

docker-set-permissions:
        docker-compose run app chown -R www-data:www-data var/logs
        docker-compose run app chown -R www-data:www-data var/cache

############## test

test:
        docker-compose run app php bin/phpunit

Alternatively, you may introduce a .env file which contains a environment variables and then user one of the variables to run usermod command in the Docker container.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Docker: Permissions for a mounted volume

How to remove mounted Docker volume directory on host without root permissions

Docker volume and host permissions

How to manage permissions for a volume mounted into a docker container?

Understanding host mounted volume changes in Docker

Docker options to remove host mounted named volume

docker data volume vs mounted host directory

Docker custom user id permissions in a volume (mounted and not mounted)

How to set ownership and permissions of files mounted via a docker volume

Docker doesn't save data in host mounted volume

Running docker on Ubuntu: mounted host volume is not writable from container

Change file permissions in mounted folder inside docker container on Windows Host

How to create files on host using Docker and a text editor for local development (permissions problem)

docker-compose and using local image with mounted volume

Mounted Docker volume has different ownership when using Travis

Can user privilege of docker container impact permission of host machine on mounted volume in docker-compose.yml?

PHP Development Environment using Docker

docker image - mounted volume is empty

Docker mounted volume javascript not syncing

Using environment variable for volume name in docker compose

Docker compose volume definition using environment variable

How to give non-root user in Docker container access to a volume mounted on the host

Write Permissions in Docker Volume

Replace a file from host to container with mounted volume

Orphaned Docker mounted host volumes?

Container content is not being copied to docker host using docker volume

Docker inside docker : volume is mounted, but empty

Docker MySQL Host Volume

Mounted host volume is not writable from host in Azure Pipelines