Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a docker container if not already running

Tags:

bash

docker

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 run --name nginx -d nginx

I am open to any script or language.

like image 836
Luke101 Avatar asked Jun 24 '17 00:06

Luke101


People also ask

How do I run an existing Docker container?

You can also use the -w (workdir) flag along with the Docker exec command to specify the path of the directory where you want to execute the command inside the container. If you want to access the bash of a container running in background mode, you can use the -it options and provide the /bin/bash command.

How do I go inside a not running Docker container?

When we try to run /bin/sh on a stopped container using docker exec , Docker will throw a No such container error. We have to transform the stopped Docker container into a new Docker image before we can inspect the internals of the container. We can transform a container into a Docker image using the commit command.


3 Answers

I would definitely suggest looking into docker-compose and docker-compose up as answered above.

Since your question is about docker run, i would simplify the answer of VonC like this

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.

The "exists but stopped" part is missing in VonC's answer.

like image 172
Eugen Mayer Avatar answered Oct 21 '22 18:10

Eugen Mayer


Use a filter to check if a container of a certain name exists:
(See 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.

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):

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
like image 30
VonC Avatar answered Oct 21 '22 17:10

VonC


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:

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.

Regards

like image 6
Carlos Rafael Ramirez Avatar answered Oct 21 '22 19:10

Carlos Rafael Ramirez