如何在docker compose中的服务之间共享用户?
如何在docker compose中的服务之间共享用户?我可以创建卷并将其装入/etc/container目录,但它会隐藏另一个文件/目录。有没有更聪明的方法来实现这个目标?您只能在docker容器中装入文件如何在docker compose中的服务之间共享用户?,docker,Docker,如何在docker compose中的服务之间共享用户?我可以创建卷并将其装入/etc/container目录,但它会隐藏另一个文件/目录。有没有更聪明的方法来实现这个目标?您只能在docker容器中装入文件 volumes: - /etc/mysql.cnf:/etc/mysql.cnf 您可以使用卷+绑定装载将一个容器的passwd&组装载到另一个容器 volumes: - /etc/mysql.cnf:/etc/mysql.cnf 下面是一个例子: 如果不使用卷,只需在
volumes:
- /etc/mysql.cnf:/etc/mysql.cnf
您可以使用
卷
+绑定装载
将一个容器的passwd
&组
装载到另一个容器
volumes:
- /etc/mysql.cnf:/etc/mysql.cnf
下面是一个例子:
mysql
user:
docker\u compose.yaml:
version: "3"
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
test:
image: ubuntu:16.04
command: id mysql
depends_on:
- db
version: "3"
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- my_etc:/etc
test:
image: ubuntu:16.04
command: id mysql
depends_on:
- db
volumes:
- /tmp/etc-data/passwd:/etc/passwd
- /tmp/etc-data/group:/etc/group
volumes:
my_etc:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/tmp/etc-data'
$ mkdir -p /tmp/etc-data
$ docker-compose up
Creating network "23_default" with the default driver
Creating 23_db_1 ... done
Creating 23_test_1 ... done
Attaching to 23_db_1, 23_test_1
db_1 | Initializing database
test_1 | uid=999(mysql) gid=999(mysql) groups=999(mysql)
23_test_1 exited with code 0
作为下一个执行:
$ docker-compose up
Creating network "23_default" with the default driver
Creating 23_db_1 ... done
Creating 23_test_1 ... done
Attaching to 23_db_1, 23_test_1
test_1 | id: 'mysql': no such user
db_1 | Initializing database
23_test_1 exited with code 1
从上面,您可以看到ubuntu:16.04
中的容器没有用户mysql
,这是mysql
中的默认用户:
test|1 | id:'mysql':没有这样的用户
用户mysql
对测试
容器可见:
docker\u compose.yaml:
version: "3"
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
test:
image: ubuntu:16.04
command: id mysql
depends_on:
- db
version: "3"
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- my_etc:/etc
test:
image: ubuntu:16.04
command: id mysql
depends_on:
- db
volumes:
- /tmp/etc-data/passwd:/etc/passwd
- /tmp/etc-data/group:/etc/group
volumes:
my_etc:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/tmp/etc-data'
$ mkdir -p /tmp/etc-data
$ docker-compose up
Creating network "23_default" with the default driver
Creating 23_db_1 ... done
Creating 23_test_1 ... done
Attaching to 23_db_1, 23_test_1
db_1 | Initializing database
test_1 | uid=999(mysql) gid=999(mysql) groups=999(mysql)
23_test_1 exited with code 0
按下一步执行,注意:我们需要在启动之前添加新的/tmp/etc数据:
version: "3"
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
test:
image: ubuntu:16.04
command: id mysql
depends_on:
- db
version: "3"
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- my_etc:/etc
test:
image: ubuntu:16.04
command: id mysql
depends_on:
- db
volumes:
- /tmp/etc-data/passwd:/etc/passwd
- /tmp/etc-data/group:/etc/group
volumes:
my_etc:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/tmp/etc-data'
$ mkdir -p /tmp/etc-data
$ docker-compose up
Creating network "23_default" with the default driver
Creating 23_db_1 ... done
Creating 23_test_1 ... done
Attaching to 23_db_1, 23_test_1
db_1 | Initializing database
test_1 | uid=999(mysql) gid=999(mysql) groups=999(mysql)
23_test_1 exited with code 0
从上面可以看到test
服务已经可以拥有用户mysql
:
测试1 | uid=999(mysql)gid=999(mysql)groups=999(mysql)
version: "3"
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
test:
image: ubuntu:16.04
command: id mysql
depends_on:
- db
version: "3"
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- my_etc:/etc
test:
image: ubuntu:16.04
command: id mysql
depends_on:
- db
volumes:
- /tmp/etc-data/passwd:/etc/passwd
- /tmp/etc-data/group:/etc/group
volumes:
my_etc:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/tmp/etc-data'
$ mkdir -p /tmp/etc-data
$ docker-compose up
Creating network "23_default" with the default driver
Creating 23_db_1 ... done
Creating 23_test_1 ... done
Attaching to 23_db_1, 23_test_1
db_1 | Initializing database
test_1 | uid=999(mysql) gid=999(mysql) groups=999(mysql)
23_test_1 exited with code 0
上述解决方案首先使用命名卷
将第一个容器
的/etc
文件夹弹出到docker主机上的/tmp/etc data
文件夹,然后第二个容器将使用绑定装载
将passwd
和group
单独装载到第二个容器。如您所见,第二个容器只装载了2个文件(passwd
,group
),因此它不会隐藏任何其他文件