如何将 docker-compose. yml 配置为将一个容器作为 root 用户

我试图用 docker-compose-yml 连接两个容器,但是没有用。这是我的 docker-compose. yml 文件:

version: "3"
services:
datapower:
build: .
ports:
- "9090:9090"
depends_on:
- db
db:
image: "microsoft/mssql-server-linux:2017-latest"
environment:
SA_PASSWORD: "your_password"
ACCEPT_EULA: "Y"
ports:
- "1433:1433"

当我做:

码头工人,冷静点

这是我的两个集装箱。然后我停止一个集装箱,然后我运行相同的集装箱,独立地停止像:

Docker-compose run-u root —— name nameofContainer‘ name of container name in docker-compose. yml’

这样,容器的连接就可以工作了。存在一个配置我的 docker-compose 的方法。如何像 root 一样连接容器而不停止容器并独立运行?

145862 次浏览

Update:

There exists the user property that can be set in the compose file. This is documented in docker-compose file reference.

...
services:
datapower:
build: .
user: root
ports:
- "9090:9090"
depends_on:
- db
...

Setting both a User AND a Group in docker-compose.yml:

Discovered another way to set not only the user but also the group in a docker-compose.yml file which is NOT documented in the Docker Compose File Reference @yamenk helpfully provides in the accepted answer.

I needed to raise a container expressly setting both a user AND a group and found that the user: parameter in docker-compose.yml can be populated as a UID:GID mapping delimited by a colon.

Below is a snippet from my docker-compose.yml file where this form was tested and found to work correctly:

services:
zabbix-agent:
image: zabbix/zabbix-agent2:ubuntu-6.0-latest
container_name: DockerHost1-zabbix-agent2
user: 0:0
<SNIP>

Reference:

https://github.com/zabbix/zabbix-docker/issues/710

Hope this saves others wasted cycles looking for this!