我有一个 Dockerfile
,它的目的是使用script1
,如下所示:
# Pull from Debian
FROM debian
# Update apt and install dependencies
RUN apt update
RUN apt -y upgrade
RUN apt -y install wget curl
# Download script1.sh
RUN wget -O ./script1.sh https://example.com
# Make script1.sh executable
RUN chmod +x ./script1.sh
目前,我可以做到以下几点:
- 将此Dockerfile构建为镜像
- 在容器中运行该镜像
- 在容器中打开CLI,并手动运行
script1
(使用bash ./script1.sh
) - 脚本运行后,容器保持打开状态。
然而,我希望能够在容器启动时自动运行此脚本。
因此,我尝试将我的Dockerfile
更改为以下内容:
# Pull from Debian
FROM debian
# Update apt and install dependencies
RUN apt update
RUN apt -y upgrade
RUN apt -y install wget curl
# Download script1.sh
RUN wget -O ./script1.sh https://example.com
# Make script1.sh executable
RUN chmod +x ./script1.sh
# Run script1.sh on startup
CMD bash ./script1.sh
然而,当我这样做时,容器只能保持一小段时间就会立即退出。
我怀疑它会在
script1
完成后立即退出...我还尝试过使用
ENTRYPOINT
,但效果不佳。为什么如果我打开CLI并手动运行脚本,容器会保持开启状态,但是如果我尝试在启动时自动运行它,容器不会保持开启状态?
如何在容器启动时自动运行脚本,并使容器不会立即退出?
ls
时,如何使进程在写完其目录列表后不退出?”) - David Maze