如何编写dockerfile来执行一个简单的bash脚本? [英] How do I write a dockerfile to execute a simple bash script?
问题描述
所以我在我的docker构建文件夹中:
Dockerfile
index.html
run_netcat_webserver.sh
run_netcat_webserver.sh
非常简单,可以正常工作:
< pre class =lang-sh prettyprint-override>
#!/ bin / bash
while true;做nc-18080 index.html;完成
这是我天真的Dockerfile,当然不起作用:
FROM ubuntu:14.04
CMD run_netcat_webserver.sh
我应该如何在Docker容器内进行这项工作?
你需要使脚本成为容器的一部分。为此,您需要使用Docker文件中的 COPY
命令复制脚本,例如。像这样
FROM ubuntu:14.04
COPY run_netcat_webserver.sh /some/path/run_netcat_webserver.sh
CMD /some/path/run_netcat_webserver.sh
/ some / path
是您选择容器内的路径。既然您不需要关心容器内的用户,就可以使用 /
。
另一个选择是通过安装的卷向外部提供脚本。例如:
FROM ubuntu:14.04
VOLUME / scripts
CMD /scripts/run_netcat_webserver.sh
然后,当您运行容器时,您将指定将挂载的目录为 /脚本
。假设你的脚本是 / tmp
,那么你运行容器为
docker run --volume = / tmp:/ scripts(命令选项和参数的其余部分)
这将导致您的(主机)目录 / tmp
将被挂载在容器的 / scripts
目录下。
I'm trying to write a docker image to run a simple webserver though netcat.
So I have in my docker build folder:
Dockerfile
index.html
run_netcat_webserver.sh
The run_netcat_webserver.sh
is very simple and it works fine:
#!/bin/bash
while true ; do nc -l 8080 < index.html ; done
Here is my naive Dockerfile that of course is not working:
FROM ubuntu:14.04
CMD run_netcat_webserver.sh
How should I proceed to make this work inside a docker container?
You need to make the script part of the container. To do that, you need to copy the script inside using the COPY
command in the Docker file, e.g. like this
FROM ubuntu:14.04
COPY run_netcat_webserver.sh /some/path/run_netcat_webserver.sh
CMD /some/path/run_netcat_webserver.sh
The /some/path
is a path of your choice inside the container. Since you don't need to care much about users inside the container, it can be even just /
.
Another option is to provide the script externally, via mounted volume. Example:
FROM ubuntu:14.04
VOLUME /scripts
CMD /scripts/run_netcat_webserver.sh
Then, when you run the container, you specify what directory will be mounted as /scripts
. Let's suppose that your script is in /tmp
, then you run the container as
docker run --volume=/tmp:/scripts (rest of the command options and arguments)
This would cause that your (host) directory /tmp
would be mounted under the /scripts
directory of the container.
这篇关于如何编写dockerfile来执行一个简单的bash脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!