在Dockerfile中运行脚本 [英] Run a script in Dockerfile

查看:1069
本文介绍了在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 bu executing my running container:

docker exec symfony /bin/bash -c "/bootstrap.sh"

似乎没有任何作用。

你知道怎么做吗?

推荐答案

RUN ENTRYPOINT 是执行脚本的两种不同方式。

RUN and ENTRYPOINT are two different way 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.

在两种情况下,都需要添加脚本,并添加一个运行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

考虑脚本( bootstarp.sh :几个 git config --global 命令),最好在您的 Dockerfile 中一次运行 RUN 该脚本,但是要确保使用正确的用户(全局 git config 文件为%HOME%/。gitconfig ,默认情况下为 / root 一个)

Considering your script (bootstarp.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 /bootstart.sh

然后,在运行容器时,检查 /root/.gitconfig 的内容以确认脚本已运行。

Then, when running a container, check the content of /root/.gitconfig to confirm the script was run.

这篇关于在Dockerfile中运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆