使用 shell 脚本在 docker 容器内运行脚本 [英] Running a script inside a docker container using shell script
问题描述
我正在尝试创建一个用于设置 docker 容器的 shell 脚本.我的脚本文件看起来像:
I am trying to create a shell script for setting up a docker container. My script file looks like:
#!bin/bash
docker run -t -i -p 5902:5902 --name "mycontainer" --privileged myImage:new /bin/bash
运行此脚本文件将在新调用的 bash 中运行容器.
Running this script file will run the container in a newly invoked bash.
现在我需要运行一个脚本文件(test.sh),它已经在上面给定的shell脚本中的容器内.(例如:cd/path/to/test.sh && ./test.sh)怎么做?
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?
推荐答案
您可以使用 docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
在正在运行的容器中运行命令::>
You can run a command in a running container using docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
:
docker exec mycontainer /path/to/test.sh
并从 bash 会话运行:
And to run from a bash session:
docker exec -it mycontainer /bin/bash
从那里你可以运行你的脚本.
From there you can run your script.
这篇关于使用 shell 脚本在 docker 容器内运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!