在 Dockerfile 中运行脚本 [英] Run a script in Dockerfile
问题描述
我试图在构建过程中在我的 Dockerfile
中运行一个脚本.但这似乎不起作用.
I'm trying to run a script during my building process in my Dockerfile
.
But it doesn't seems to work.
我试过这样:
FROM php:7-fpm
ADD bootstrap.sh /
ENTRYPOINT ["/bin/bash", "/bootstrap.sh"]
也是这样:
FROM php:7-fpm
ADD bootstrap.sh /
RUN bash -c "/bootstrap.sh"
还可以通过执行我正在运行的容器:
And also by executing my running container:
docker exec symfony /bin/bash -c "/bootstrap.sh"
似乎没有任何效果.
你知道怎么做吗?
推荐答案
RUN
和 ENTRYPOINT
是执行脚本的两种不同方式.
RUN
and ENTRYPOINT
are two different ways to execute a script.
RUN
意味着它创建一个中间容器,运行脚本并将该容器的新状态冻结在一个新的中间图像中.该脚本将不会在此之后运行:您的最终图像应该反映该脚本的结果.
RUN
means it creates an intermediate container, runs the script and freeze the new state of that container in a new intermediate image. The script won't be run after that: your final image is supposed to reflect the result of that script.
ENTRYPOINT
表示您的图像(尚未执行脚本)将创建一个容器并运行该脚本.
ENTRYPOINT
means your image (which has not executed the script yet) will create a container, and runs that script.
在这两种情况下,都需要添加脚本,并且 RUN chmod +x/bootstrap.sh
是个好主意.
In both cases, the script needs to be added, and a RUN chmod +x /bootstrap.sh
is a good idea.
它也应该以 shebang 开头(如 #!/bin/sh
)
It should also start with a shebang (like #!/bin/sh
)
考虑到您的脚本(bootstrap.sh
:几个 git config --global
命令),最好 RUN
在您的 Dockerfile
中编写一次脚本,但确保使用正确的用户(全局 git config
文件是 %HOME%/.gitconfig
,其中默认是/root
之一)
Considering your script (bootstrap.sh
: a couple of git config --global
commands), it would be best to RUN
that script once in your Dockerfile
, but making sure to use the right user (the global git config
file is %HOME%/.gitconfig
, which by default is the /root
one)
添加到您的 Dockerfile:
Add to your Dockerfile:
RUN /bootstrap.sh
然后,在运行容器时,检查 /root/.gitconfig
的内容以确认脚本已运行.
Then, when running a container, check the content of /root/.gitconfig
to confirm the script was run.
这篇关于在 Dockerfile 中运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!