Docker RUN groupadd && useradd directives have no effect

luq*_*o33 5 linux ubuntu docker

I've built my Docker nginx 'base' image with a Dockerfile whose fragment is below:

FROM ubuntu:14.04
MAINTAINER Me <me.net>
RUN apt-get update && apt-get install -y supervisor
ADD supervisord.conf /etc/supervisor/conf.d/
RUN apt-get install -y nginx
..
Run Code Online (Sandbox Code Playgroud)

This image was then linked with a database container and data volume. Later, I wanted to add a user to the container so that I could run applications as this user so I added 'RUN groupadd -r luqo33 && useradd -r -g luqo33 luqo33' directive to the Dockerfile so that my Dockerfile would look like this:

FROM ubuntu:14.04
MAINTAINER Me <me.net>

RUN groupadd -r luqo33 && useradd -r -g luqo33 luqo33

RUN apt-get update && apt-get install -y supervisor
ADD supervisord.conf /etc/supervisor/conf.d/
RUN apt-get install -y nginx
..
Run Code Online (Sandbox Code Playgroud)

Then I rebuit an image with docker build -rm ., and then run the whole stack again with docker-compose up (I have the stack configured in docker-compose.yml).

Unfortunately, although the RUN groupadd -r luqo33 && useradd -r -g luqo33 luqo33 step did not error out, when I entered the shell of the running nginx container, luqo33 user or luqo33 group were not there. I then executed the same commands (groupadd -r luqo33 && useradd -r -g luqo33 luqo33) from the shell, and the group and user were added as expected.

Why wouldn't RUN groupadd -r luqo33 && useradd -r -g luqo33 luqo33 in Dockerfile add the user and the group to the new container upon rebuilding? I also tried docker build --no-cache . with the same effect (or lack of it). What am I missing here?

Tho*_*eil 0

为了确保您的构建命令不使用任何缓存层,请使用该--no-cache选项。

docker build --no-cache .
Run Code Online (Sandbox Code Playgroud)

如果你想与 docker-compose 一样:

docker-compose build --no-cache
Run Code Online (Sandbox Code Playgroud)

另外,由于您使用docker-compose来启动容器,因此请确保运行docker-compose rm,否则 docker-compose 将重用以前构建的映像。