Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker permissions development environment using a host mounted volume

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"
like image 506
Robert Moskal Avatar asked Jul 16 '15 23:07

Robert Moskal


1 Answers

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.

like image 130
Andrzej Ośmiałowski Avatar answered Nov 15 '22 06:11

Andrzej Ośmiałowski