简体   繁体   English

从bash脚本启动Docker容器并在其上执行命令

[英]Start a docker container from bash script and execute commands on it

I am having a problem running Fenics using their docker container: 我在使用其Docker容器运行Fenics时遇到问题:

Background to this is: One main VM communicates with the user via a REST API and gets one or many tasks to compute in Fenics. 其背景是:一个主VM通过REST API与用户通信,并获得一个或多个任务以Fenics计算。 Fenics runs on the docker containers. Fenics在docker容器上运行。 Now the main VM has to orchestrate different worker-VMs using a script. 现在,主虚拟机必须使用脚本来编排不同的辅助虚拟机。

Example: the user wants to have meshes A and B getting computed using parameter sets x and y. 示例:用户希望使用参数集x和y计算网格A和B。 For this two worker-VMs can be used, which have Fenics container installed. 为此,可以使用安装了Fenics容器的两个worker-VM。 How can I start a docker container and pass the parameters into it, by using a bash script? 如何使用bash脚本启动docker容器并将参数传递给它?

In the first try I cannot even access a created container on my own computer using a script, since the container exits immediately. 在第一次尝试中,我什至无法使用脚本访问自己计算机上的已创建容器,因为该容器会立即退出。

#!/bin/bash

clear
docker run -dit -v $(pwd):/home/fenics/shared -w /home/fenics/shared quay.io/fenicsproject/stable:curre$

OUTPUT=$(docker ps -q | grep "$name")
echo $OUTPUT
docker exec -dit $OUTPUT /bin/bash
docker exec -dit $OUTPUT echo "Hallo"
docker exec -dit $OUTPUT mkdir test
docker exec -dit $OUTPUT echo "Ciao"

If I look into docker ps -a I can only see exited containers. 如果查看docker ps -a我只能看到退出的容器。

Two questions arise therefore: Firstly, is this the way to pass the commands appropriately and secondly, how can I keep the container running? 因此,出现两个问题:首先,这是正确传递命令的方式吗?其次,如何保持容器运行?

As might be obvious, I have no prior experience using docker and am therefore grateful for any help. 显而易见,我没有使用docker的经验,因此感谢您的帮助。

most likely, what you want to do is put your commands into a file commands.sh and then run docker run --rm -v $(pwd):/home/fenics/shared -w /home/fenics/share --entrypoint bash quay.io/fenicsproject/stable:current commands.sh 最有可能要做的是将命令放入文件commands.sh ,然后docker run --rm -v $(pwd):/home/fenics/shared -w /home/fenics/share --entrypoint bash quay.io/fenicsproject/stable:current commands.sh

What this will do is share your folder, launch the fenics container, and execute the shell script inside that container (presumably, your output will write to a file in the shared drive). 这样做是共享您的文件夹,启动fenics容器,然后在该容器内执行shell脚本(大概,您的输出将写入共享驱动器中的文件)。 The --rm part will clean up the container after the run. --rm部分将在运行后清理容器。

I don't actually know what fenics is- if there is some sort of state or initialization, make sure you are taking care of that in your commands.sh file, since over-riding the entrypoint will prevent any fancy init scripts they provided from running. 我实际上不知道什么是fenics-如果存在某种状态或初始化,请确保在commands.sh文件中进行了处理,因为重写入口点将阻止他们提供的任何奇特的初始化脚本运行。 Alternatively, their docker image repo may explain how to feed a custom script to the container. 或者,他们的docker映像存储库可能会解释如何将自定义脚本提供给容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM