bash 使用 shell 脚本在 docker 容器内运行脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31578446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running a script inside a docker container using shell script
提问by zappy
I am trying to create a shell script for setting up a docker container. My script file looks like:
我正在尝试创建一个用于设置 docker 容器的 shell 脚本。我的脚本文件如下所示:
#!bin/bash
docker run -t -i -p 5902:5902 --name "mycontainer" --privileged myImage:new /bin/bash
Running this script file will run the container in a newly invoked bash.
运行此脚本文件将在新调用的 bash 中运行容器。
Now I need to run a script file (test.sh)which is already inside container from the above given shell script.(eg: cd /path/to/test.sh && ./test.sh) How to do that?
现在我需要运行一个脚本文件(test.sh),它已经在上面给定的 shell 脚本中的容器内。(例如:cd /path/to/test.sh && ./test.sh)怎么做?
回答by Javier Cortejoso
You can run a command in a running container using docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
:
您可以使用以下命令在正在运行的容器中运行命令docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
:
docker exec mycontainer /path/to/test.sh
And to run from a bash session:
并从 bash 会话运行:
docker exec -it mycontainer /bin/bash
And from there you can run your script or whatever.
从那里你可以运行你的脚本或其他任何东西。
回答by Cyclops
Assuming that your docker container is up and running, you can run commands as:
假设您的 docker 容器已启动并正在运行,您可以运行以下命令:
docker exec mycontainer /bin/sh -c "cmd1;cmd2;...;cmdn"
回答by tojo
I was searching an answer for this same question and found ENTRYPOINT in Dockerfilesolution for me.
我正在寻找相同问题的答案,并在 Dockerfile解决方案中为我找到了ENTRYPOINT。
Dockerfile
文件
...
ENTRYPOINT /my-script.sh ; /my-script2.sh ; /bin/bash
Now the scripts are executed when I start the container and I get the bash prompt after the scripts has been executed.
现在脚本在我启动容器时执行,并且在脚本执行后我得到 bash 提示。
回答by Devpool
You could also mount a local directory into your docker image and source the script in your .bashrc
. Don't forget the script has to consist of functions unless you want it to execute on every new shell. (This is outdated see the update notice.)
您还可以将本地目录挂载到 docker 映像中,并在.bashrc
. 不要忘记脚本必须由函数组成,除非您希望它在每个新 shell 上执行。(这是过时的,请参阅更新通知。)
I'm using this solution to be able to update the script outside of the docker instance. This way I don't have to rerun the image if changes occur, I just open a new shell. (Got rid of reopening a shell - see the update notice)
我正在使用此解决方案来更新 docker 实例之外的脚本。这样我就不必在发生更改时重新运行图像,我只需打开一个新外壳。(摆脱了重新打开外壳 - 请参阅更新通知)
Here is how you bind your current directory:
以下是绑定当前目录的方法:
docker run -it -v $PWD:/scripts $my_docker_build /bin/bash
Now your current directory is bound to /scripts
of your docker instance.
现在您的当前目录绑定到/scripts
您的 docker 实例。
(Outdated)
To save your .bashrc
changes commit your working image with this command:
(过时)要保存.bashrc
更改,请使用以下命令提交工作映像:
docker commit $container_id $my_docker_build
Update
更新
To solve the issue to open up a new shell for every change I now do the following:
为了解决为每次更改打开一个新 shell 的问题,我现在执行以下操作:
In the dockerfile itself I add RUN echo "/scripts/bashrc" > /root/.bashrc"
. Inside zshrc
I export the scripts directory to the path. The scripts directory now contains multiple files instead of one. Now I can directly call all scripts without having open a sub shell on every change.
在 dockerfile 本身中,我添加了RUN echo "/scripts/bashrc" > /root/.bashrc"
. 在里面,zshrc
我将脚本目录导出到路径。脚本目录现在包含多个文件而不是一个。现在我可以直接调用所有脚本,而无需在每次更改时打开子 shell。
BTW you can define the history file outside of your container too. This way it's not necessary to commit on a bash change anymore.
顺便说一句,您也可以在容器之外定义历史文件。这样就不再需要提交 bash 更改了。
回答by Thomio
In case you don't want (or have) a running container, you can call your script directly with the run
command.
如果您不想要(或拥有)正在运行的容器,您可以直接使用该run
命令调用您的脚本。
Remove the iterative tty -i -t
arguments and use this:
删除迭代 tty-i -t
参数并使用:
$ docker run ubuntu:bionic /bin/bash /path/to/script.sh
This will (didn't test) also work for other scripts:
这将(未测试)也适用于其他脚本:
$ docker run ubuntu:bionic /usr/bin/python /path/to/script.py
回答by Boris
Have a look at entry points too. You will be able to use multiple CMD https://docs.docker.com/engine/reference/builder/#/entrypoint
也看看入口点。您将能够使用多个 CMD https://docs.docker.com/engine/reference/builder/#/entrypoint
回答by DMin
If you want to run the same command on multiple instances you can do this :
如果你想在多个实例上运行相同的命令,你可以这样做:
for i in c1 dm1 dm2 ds1 ds2 gtm_m gtm_sl; do docker exec -it $i /bin/bash -c "service sshd start"; done