【问题标题】:RUN Instruction in building Docker Image构建 Docker 镜像的 RUN 指令
【发布时间】:2020-08-10 20:33:55
【问题描述】:

下面是我的 Docker 文件

ARG tag_info=latest
FROM alpine:${tag_info} AS my_apline
ARG message='Hello World'
RUN echo ${message}
RUN echo ${message} > myfile.txt
RUN echo "Hi Harry"

当我运行docker image build -t myimage:v1_0 - < Dockerfile

输出是:

Sending build context to Docker daemon  2.048kB
Step 1/6 : ARG tag_info=latest
Step 2/6 : FROM alpine:${tag_info} AS my_apline
latest: Pulling from library/alpine
cbdbe7a5bc2a: Already exists
Digest: sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54
Status: Downloaded newer image for alpine:latest
 ---> f70734b6a266
Step 3/6 : ARG message='Hello World'
 ---> Running in 74bcc8897e8e
Removing intermediate container 74bcc8897e8e
 ---> d8d50432d375
Step 4/6 : RUN echo ${message}
 ---> Running in 730ed8e1c1d3
Hello World
Removing intermediate container 730ed8e1c1d3
 ---> 8417e3167e80
Step 5/6 : RUN echo ${message} > myfile.txt
 ---> Running in c66018331383
Removing intermediate container c66018331383
 ---> 07dc27d8ad3d
Step 6/6 : RUN echo "Hi Harry"
 ---> Running in fb92fb234e42
Hi Harry
Removing intermediate container fb92fb234e42
 ---> a3bec122a77f

它在中间显示“Hi Harry”和“Hello World”(我不明白为什么)

当我从图像文件中旋转容器时,为什么不显示“Hi Harry and “Hello World”?

【问题讨论】:

    标签: docker dockerfile docker-build docker-run


    【解决方案1】:

    因为RUN 命令在构建映像时执行命令,而不是在使用映像启动容器时执行命令。它用于更改图像,例如使用apt-get 添加新包或更改文件权限等

    如果您需要在容器启动时运行某些内容,您需要使用 commandentrypoint 指令

    【讨论】:

    • 感谢您的信息。我假设如果 RUN 指令正在添加新层,那么在容器旋转期间将看到更改
    【解决方案2】:

    来自 docker 官方文档:

    The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.
    

    如果你想获得所描述的行为,你应该使用CMD

    The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.
    

    这有三种形式:

     - CMD ["executable","param1","param2"] (exec form, this is the preferred form)
     - CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
     - CMD command param1 param2 (shell form)
    

    【讨论】:

    • 感谢您的回复。更清楚地说,如果我的 RUN 指令正在向前一层的内容添加一些内容(它回显“Hi Harry and Hello World 没有添加任何内容),那么它将创建新层并且内容将在容器旋转期间显示?
    猜你喜欢
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 2019-06-22
    • 2021-07-03
    • 2017-09-15
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多