首页 文章

Docker RUN groupadd && useradd指令无效

提问于
浏览
3

我用Dockerfile构建了我的Docker nginx'base'图像,其片段如下:

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
..

然后将该图像与数据库容器和数据卷链接 . 后来,我想在容器中添加一个用户,以便我可以像这个用户一样运行应用程序,所以我在Dockerfile中添加了'RUN groupadd -r luqo33 && useradd -r -g luqo33 luqo33'指令,这样我的Dockerfile看起来就像这样:

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
..

然后我使用 docker build -rm . 重新映射图像,然后使用 docker-compose up 再次运行整个堆栈(我在docker-compose.yml中配置了堆栈) .

不幸的是,虽然 RUN groupadd -r luqo33 && useradd -r -g luqo33 luqo33 步骤没有错误输出,但当我进入正在运行的nginx容器的shell时, luqo33 user或 luqo33 group不存在 . 然后我从shell执行了相同的命令( groupadd -r luqo33 && useradd -r -g luqo33 luqo33 ),并按预期添加了组和用户 .

为什么Dockerfile中的 RUN groupadd -r luqo33 && useradd -r -g luqo33 luqo33 在重建时不会将用户和组添加到新容器中?我也尝试了 docker build --no-cache . 具有相同的效果(或缺乏它) . 我在这里错过了什么?

1 回答

  • 0

    要确保您的构建命令没有使用任何缓存层,请使用--no-cache选项 .

    docker build --no-cache .
    

    如果你想使用docker-compose:

    docker-compose build --no-cache
    

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

相关问题