简体   繁体   English

Docker权限开发环境使用主机安装的卷

[英]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). 我正在使用docker-compose为一堆symfony2应用程序设置一个可移植的开发环境(虽然我想要做的只是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. 我决定将本地计算机上的源文件公开为具有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. 一切都很好,除了在运行应用程序后我的缓存和日志文件以及/ vendor目录中的composer创建的文件现在归root所有。

I've read about this problem and some possible approaches here: 我已经在这里阅读了这个问题和一些可能的方法:

Changing permissions of added file to a Docker volume 将添加文件的权限更改为Docker卷

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. 但是我无法理解我在docker-compose.yml文件中要做出哪些更改,以便当我的symphony容器以docker -compose启动时,所有创建的文件都具有主机上用户的权限。

I'm posting the file for reference, worker is where php, etc. live: 我发布文件供参考,工作者是php等等直播:

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"

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 . 示例命令: docker-compose run CONTAINER_NAME php bin/console cache:clear You may use make , ant or any modern tool to keep the commands short. 您可以使用makeant或任何现代工具来保持命令简短。

Example with Makefile : 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. 或者,您可以引入包含环境变量的.env文件,然后使用其中一个变量在Docker容器中运行usermod命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM