我正在从提供的Docker映像构建Docker映像。我需要安装需要管理特权的新依赖项。提供的映像的默认用户没有管理特权。完成后,我想将默认用户改回以前的默认用户。
是否有一种方法可以在不事先知道的情况下通用地保留基本映像的用户?
注意:我不是在问如何检查Docker映像并在Dockerfile
之外查找默认用户。我想知道文件本身是否有办法。
请参见下面的示例Dockerfile
FROM supliedImage
USER root
... Perform administrative task
USER <Default user from supplied image>
答案 0 :(得分:0)
好吧……无法按照您想要的方式进行操作,但是您始终可以对多阶段构建使用变通方法:
FROM solr:alpine as build
RUN whoami
FROM build as prepare_dependencies
USER root
RUN echo 'I am root' > /the_root_important_message
FROM build
COPY --from=prepare_dependencies /the_root_important_message /the_root_vital_message
RUN echo "now I'm $( whoami )"
RUN cat /the_root_vital_message
CMD echo 'this is a pain'
我很确定这不是您要的...
由于这个主题很有趣,所以我决定尝试另一种方法:
FROM solr:alpine as build
RUN whoami
FROM build as prepare_dependencies
USER root
RUN apk --no-cache --update add sudo \
&& echo 'ALL ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers
RUN echo "this is secret" > /secretfile
RUN chmod 400 /secretfile
FROM build
COPY --from=prepare_dependencies /usr/bin/sudo /usr/bin/sudo
COPY --from=prepare_dependencies /usr/lib/sudo /usr/lib/sudo
COPY --from=prepare_dependencies /etc/sudoers /etc/sudoers
COPY --from=prepare_dependencies /secretfile /secretfile
RUN sudo -l
RUN sudo cat /secretfile
RUN echo "now I'm $( whoami )"
RUN echo "cleanup" \
&& sudo rm -rf \
/etc/sudoers /secretfile \
/usr/lib/sudo /usr/bin/sudo
CMD echo 'this is a pain'