【问题标题】:Creating a file in Docker build在 Docker 构建中创建文件
【发布时间】:2023-04-05 10:37:01
【问题描述】:

我正在构建一个安装依赖项、克隆 git 存储库、设置 python 环境、更新配置文件和创建 systemctl 服务的映像。除了我尝试在进程结束时创建的脚本 (start.sh) 之外,一切都可以找到。它似乎在 docker build 步骤中存在,但是一旦我运行映像,它就会消失。

# Dockerfile 
RUN echo "#!/bin/bash" > start.sh
RUN echo "systemctl start jenkins" >> start.sh
RUN echo "systemctl start runserver" >> start.sh
RUN cat start.sh
RUN chmod +x start.sh

我的 docker build 终端输出

Step 28/32 : RUN echo "#!/bin/bash" > start.sh
 ---> Running in 4a3f91a67b90
Removing intermediate container 4a3f91a67b90
 ---> 7fc47c36f4b8
Step 29/32 : RUN echo "systemctl start jenkins" >> start.sh
 ---> Running in 6807840fab7e
Removing intermediate container 6807840fab7e
 ---> d945ba395f9d
Step 30/32 : RUN echo "systemctl start runserver" >> start.sh
 ---> Running in 205c38c6e9e3
Removing intermediate container 205c38c6e9e3
 ---> 5684fd6346b9
Step 31/32 : RUN cat start.sh
 ---> Running in ce3326beb99c
#!/bin/bash
systemctl start jenkins
systemctl start runserver
Removing intermediate container ce3326beb99c
 ---> efd96d67c6c3
Step 32/32 : RUN chmod +x start.sh
 ---> Running in fffd5de0f924

我尝试设置绝对路径来定义目标文件:

# Dockerfile 
RUN echo "#!/bin/bash" > /home/ubuntu/start.sh
...

但它失败了。

这里有什么问题?

更新

运行镜像:

docker run -tid \
            --name auto-server \
            --rm \
            --volume data:/home/ubuntu \
            --publish 8080:8080 \
            --publish 8000:8000 \
            --publish 50000:50000 \
            --cap-add=NET_ADMIN \
            --cap-add=NET_RAW \
            auto-server:1.0

查看外壳消失:

# container build
$ docker build -t auto-server:1.0 .
... 
Step 27/31 : RUN echo "#!/bin/bash" > /home/ubuntu/start.sh
 ---> Running in 2a022fcec2f4
Removing intermediate container 2a022fcec2f4
 ---> c9d1ff27d96f
Step 28/31 : RUN echo "systemctl start jenkins" >> /home/ubuntu/start.sh
 ---> Running in 3ed993cd03c7
Removing intermediate container 3ed993cd03c7
 ---> 0e8c4463c472
Step 29/31 : RUN echo "systemctl start runserver" >> /home/ubuntu/start.sh
 ---> Running in aa31dbce3716
Removing intermediate container aa31dbce3716
 ---> d48a1fd0580c
Step 30/31 : RUN cat /home/ubuntu/start.sh
 ---> Running in 23785ebdb620
#!/bin/bash                      <== File is present during build
systemctl start jenkins
systemctl start runserver
Removing intermediate container 23785ebdb620
 ---> 7499157cede3
Step 31/31 : RUN chmod +x /home/ubuntu/start.sh
 ---> Running in 43980bffe8cc
Removing intermediate container 43980bffe8cc
 ---> 2932ca0c3d29
Successfully built 2932ca0c3d29
Successfully tagged auto-server:1.0

$ docker run -tid \
            --name auto-server \
            --rm \
            --volume data:/home/ubuntu \
            --publish 8080:8080 \
            --publish 8000:8000 \
            --publish 50000:50000 \
            --cap-add=NET_ADMIN \
            --cap-add=NET_RAW \
            auto-server:1.0

$ docker exec -ti auto-server bash
root@a36c99271b8a:/home/ubuntu#
root@a36c99271b8a:/home/ubuntu# ls -la
total 1856
drwxr-xr-x. 3 root root    4096 Oct  5 08:36 .
drwxr-xr-x. 1 root root    4096 Oct  6 05:56 ..
drwxr-xr-x. 8 root root    4096 Oct  5 09:21 auto
-rw-r--r--. 1 root root 1885433 Oct  5 08:20 get-pip.py

当我在 Dockerfile 中使用 CMD ["bash", "-c", "systemctl start jenkins"] 时,当我运行映像时,Jenkins 服务就会启动。

【问题讨论】:

  • 你是如何运行镜像的? (您是否使用卷隐藏图像内容?)首先近似systemctl 和类似的命令在 Docker 中不起作用,通常将其拆分为两个单独的容器比尝试运行两个容器更好一个容器中的独立服务。
  • but once I run the image, it disappears. 请说明你是如何运行图像的,你怎么知道它消失了?
  • 脚本应该存在于图像本身中。你验证了吗?构建镜像时还需要使用 RUN 命令,为了运行容器进程,我们必须使用 CMD 或 ENTRYPOINT。
  • 当您 cat 脚本时,您不会重定向到图像上的文件。并且您设置的烫发不在图像上的文件中。使用 COPY cmd 不是更好吗?您还需要设置入口点。
  • --volume data:/home/ubuntu .... 所以你用一些data替换 /home/ubuntu,所以里面的所有东西都会丢失。如果您想在data 卷中创建一些文件,请在此处创建。

标签: linux bash docker shell dockerfile


【解决方案1】:

在您的运行命令中,您将本地目录挂载到/home/ubuntu

--volume data:/home/ubuntu \

这意味着/home/ubuntu 中的任何内容都将替换为本地data 目录的内容 您可以将脚本编写到不同的路径示例/opt/scripts/start.sh
或者,您可以在 data 目录中的 Dockerfile 之外创建脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-02
    • 2020-04-06
    • 1970-01-01
    • 2017-01-11
    • 2021-06-09
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    相关资源
    最近更新 更多