116

I'm trying to connect two containers with a docker-compose-yml, but it isn't working. This is my docker-compose.yml file:

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"

When I make:

docker-compose up

This up my two containers. Then I stop one container and then I run the same container stoped independiently like:

docker-compose run -u root --name nameofcontainer 'name of container named in docker-compose.yml'

With this, the connection of the containers works. Exists a method to configure my docker-compose.yml to connect my containers like root without stop a container and run independently?

1
  • 1
    docker containers are already run by superuser, so why do you need to run them as root? wouldnt be easier to do su to switch to root and then start the containers? Commented Feb 11, 2018 at 8:07

2 Answers 2

197

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
...
Sign up to request clarification or add additional context in comments.

8 Comments

how do you specify the user in the docker-compose up command?
There is no option to do that docs.docker.com/compose/reference/up. It also doesn't make sense to pass it from command line since the user is per container. The only way is to set it inside the compose file.
@AlexanderZeitler you can hack it if you want to pass from command USER=root docker-compose up and on compose file: user: $USER
To add to @Theo response - this does give you the ability to run commands to generate what the user would be, like USER=$(id -u) docker-compose up and in the compose file user: $USER. This is useful when you want to run the container as a non-root user (as you should always strongly consider doing), and you need to programmatically figure out said user.
Does it work within stack files too, to deploy a stack to a swarm ?
|
28

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!

2 Comments

It is documented: domainname, hostname, ipc, mac_address, privileged, read_only, shm_size, stdin_open, tty, user, working_dir Each of these is a single value, analogous to its docker run counterpart. Note that mac_address is a legacy option. docs.docker.com/compose/compose-file/compose-file-v3/…
Thank you! The accepted answer did not work for me, but this one did :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.