Docker-compose 在安装的卷上设置用户和组 [英] Docker-compose set user and group on mounted volume

查看:26
本文介绍了Docker-compose 在安装的卷上设置用户和组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 docker-compose 中的卷挂载到 apache 映像.问题是,我的 docker 中的 apache 在 www-data:www-data 下运行,但挂载的目录是在 root:root 下创建的.如何指定挂载目录的用户?

I'm trying to mount a volume in docker-compose to apache image. The problem is, that apache in my docker is run under www-data:www-data but the mounted directory is created under root:root. How can I specify the user of the mounted directory?

我尝试运行命令 setupApacheRights.sh.chown -R www-data:www-data/var/www 但它说 chown:改变'/var/www/somefile'的所有权:权限被拒绝

I tried to run command setupApacheRights.sh. chown -R www-data:www-data /var/www but it says chown: changing ownership of '/var/www/somefile': Permission denied

services:
    httpd:
        image: apache-image
        ports:
            - "80:80"
        volumes:
            - "./:/var/www/app"
        links:
            - redis
        command: /setupApacheRights.sh

我希望能够指定安装它的用户.有办法吗?

I would prefer to be able to specify the user under which it will be mounted. Is there a way?

推荐答案

首先确定www-data用户的uid:

First determine the uid of the www-data user:

$ docker exec DOCKER_CONTAINER_ID id
uid=100(www-data) gid=101(www-data) groups=101(www-data)

然后,在您的 docker 主机上,使用 uid(在本例中为 100)更改挂载目录的所有者:

Then, on your docker host, change the owner of the mounted directory using the uid (100 in this example):

chown -R 100 ./

动态扩展

如果你正在使用 docker-compose 你不妨这样去做:

$ docker-compose exec SERVICE_NAME id
uid=100(www-data) gid=101(www-data) groups=101(www-data)
$ chown -R 100 ./

你可以把它放在一行中:

You can put that in a one-liner:

$ chown -r $(docker-compose exec SERVICE_NAME id -u) ./

-u 标志只会将 uid 打印到标准输出.

The -u flag will only print the uid to stdout.

这篇关于Docker-compose 在安装的卷上设置用户和组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆