Amin Ba
Amin Ba

Reputation: 2436

What is the difference in the user defined in Dockerfile and docker-compose file?

I have a Dockerfile as below:

FROM jenkins/jenkins:latest

USER root

RUN whoami

USER jenkins

RUN whoami

and this docker-compose file

version: '2'
services:

  test:
    build:
      context: .
      dockerfile: Dockerfile

    container_name: test
    hostname: test

    ports:
      - '8080:8080'

    user: root

I am wondering

  1. What is the difference between the user that is defined in the docker-compose and the user that is defined in the dockerfile

  2. How to see the logs of the build stage? When I RUN whoami, how and where I can see the result?

What if question is if I change the user in docker-compose to other

version: '2'
services:

  test:
    build:
      context: .
      dockerfile: Dockerfile

    container_name: test
    hostname: test

    ports:
      - '8080:8080'

    user: other

Why isn't it working

And if I change the dockerfile to

FROM jenkins/jenkins:latest

USER root

RUN whoami

RUN groupadd -g 999 docker && \
 usermod -aG staff,docker jenkins

USER jenkins

RUN whoami

and change the docker-compose to

version: '2'
services:

  test:
    build:
      context: .
      dockerfile: Dockerfile

    container_name: test
    hostname: test

    ports:
      - '8080:8080'

    user: jenkins

Still not working.

  1. What is the problem

  2. Another question is when I do docker exec -it container_name bash It get access to the container as a root user. How to change that

Upvotes: 1

Views: 747

Answers (1)

Julien B.
Julien B.

Reputation: 3324

The user config in the docker-compose file will overwrite the user used to execute the command in your container. If you omit it, it will use the last USER set in your Dockerfile. If you have neither, it should default to root.

Note that your whoami commands are run at build-time (not at run-time) so they should not be impacted by what you specified in your docker-compose file which is overriding the user at run-time only.

To see the entire build log, you can use docker compose build --progress flat.

Upvotes: 1

Related Questions