bash 如果尚未运行,如何运行 docker 容器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44731451/
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
How to run a docker container if not already running
提问by Luke101
I need to run a docker container only if its not already running. Given this command. How would I run it only if it does not exists.
仅当 docker 容器尚未运行时,我才需要运行它。鉴于此命令。如果它不存在,我将如何运行它。
docker run --name nginx -d nginx
I am open to any script or language.
我对任何脚本或语言持开放态度。
采纳答案by VonC
Use a filter to check if a container of a certain name exists:
(See docker ps Filterring)
使用过滤器检查某个名称的容器是否存在:(
参见docker ps Filterring)
#!/bin/bash
name='nginx'
[[ $(docker ps -f "name=$name" --format '{{.Names}}') == $name ]] ||
docker run --name "$name" -d nginx
The docker run
will only be executed if the first part is false.
该docker run
如果第一部分是假的,才执行。
To be on the safe side (docker ps
might return several names), you might alternatively do (if you think the word "nginx" can't be part of any container name):
为了安全起见(docker ps
可能会返回多个名称),您也可以这样做(如果您认为“nginx”一词不能成为任何容器名称的一部分):
if ! docker ps --format '{{.Names}}' | grep -w nginx &> /dev/null; then
docker run --name nginx -d nginx
fi
Or:
或者:
if ! docker ps --format '{{.Names}}' | egrep '^nginx$' &> /dev/null; then
docker run --name nginx -d nginx
fi
回答by Eugen Mayer
I would definitely suggest looking into docker-compose and docker-compose up
as answered above.
我肯定会建议研究 docker-compose 并docker-compose up
如上所述。
Since your question is about docker run
, i would simplify the answer of VonC like this
由于您的问题是关于docker run
,我会像这样简化 VonC 的答案
docker start nginx || docker run --name nginx -d nginx
If the container already is running, docker start
will return 0
thus no docker run
is executed. If the container EXISTS but is not running, docker start
will start it, otherwise it docker run
creates and starts it in one go.
如果容器已经在运行,docker start
将返回0
因此没有docker run
执行。如果容器存在但未运行,docker start
将启动它,否则它docker run
会一次性创建并启动它。
The "exists but stopped" part is missing in VonC's answer.
VonC 的回答中缺少“存在但已停止”部分。
回答by Carlos Rafael Ramirez
Well if you are open to any language I recommend using docker-compose for this task. After installing it, create a file called docker-compose.yml with this content:
好吧,如果您对任何语言都持开放态度,我建议您使用 docker-compose 来完成此任务。安装后,使用以下内容创建一个名为 docker-compose.yml 的文件:
version: '2'
services:
nginx:
image: 'nginx'
Then use:
然后使用:
docker-compose up -d
It will always check if the container is already running. If the container doesn't exists it will create it and run. If the container is stopped it just run it.
它将始终检查容器是否已经在运行。如果容器不存在,它将创建它并运行。如果容器已停止,则只需运行它。
The best thing is if you alter the docker-compose.yml or pull a new version of the image it will automatically recreate the container preserving all volumes even the unnamed ones.
最好的事情是,如果您更改 docker-compose.yml 或拉取新版本的图像,它将自动重新创建容器,保留所有卷,甚至是未命名的卷。
Regards
问候