LiohAu
LiohAu

Reputation: 645

Docker run command create container if not exists

New to docker, I am trying to run the "selenoid" container from docker hub during a TFS / azure devops build process. The 1st execution works, but during the next ones I get a conflict error (container already exists with that name). After reading few posts here, I understood that I have 2 solutions :

1/ keep the run command but execute "rm" command on that container before running it again.

2/ determine if container already exists and use "start" for the next build runs.

For the 1st solution, I guess it is not really performant to delete/recreate that container each time ? I thought about the 2nd one, but it is also a little bit more complicated to make a script that check if container exists, and then use start or run.

So isn't there a simple "run" option/flag that would change the "run" command behavior so that it create the container only if needed and then start it ?

Also, I could have made a script that makes the job, but I prefer to ask it here, because maybe I did not understood the correct way to use containers (if I am the only one to ask for that option, I may have missed something...)

Upvotes: 7

Views: 8815

Answers (3)

SiHa
SiHa

Reputation: 8411

Docker containers are meant to be ephemeral. You spin them up, they do their thing, they die, they are removed (and consume no resources).

Unless the container is initialising a database or some other thing which takes a long time when it starts, or you need to maintain state (without a volume mount), then the simplest thing to do is just run it with the --rm flag, so that when it's finished it removes itself.

docker run --rm --name mycontainer image:tag

Of course, as already commented above. You would normally only bother naming the container if you will need to exec into it when it's running. As it seems like it just runs and exits, this is probably unnecessary.

This is not to say that you can't have long-running containers, of course. But if they naturally exit, then they should normally be cleaned up, rather than jumping through hoops to start the same one again.

Upvotes: 8

Ken Wu
Ken Wu

Reputation: 111

I use the trial and error method as follows

# Shell Script

NAME=my_container_name
docker start $NAME
if [ $? -ne 0 ]; then
    echo 'docker run...' $NAME
    docker run --name $NAME -d image:tag
fi

Upvotes: 2

thompson
thompson

Reputation: 361

I don't know of an flag in docker which can do what you need. But as you said just use a script for that. For example this bash script should do the trick:

#!/usr/bin/env bash

CONTAINER_NAME=example_name

if [[ $(docker ps -a --filter="name=$CONTAINER_NAME" --filter "status=exited" | grep -w "$CONTAINER_NAME") ]]; then
    echo "docker start ..."
elif [[ $(docker ps -a --filter="name=$CONTAINER_NAME" --filter "status=running" | grep -w "$CONTAINER_NAME") ]]; then
    echo "docker still running"
else
    echo "docker run ..."
fi

There are more possible status such as: created, restarting, running, removing, paused, exited, or dead

That means in case of an already starting or created, removing, paused container, the script is not waterproof. You'll just get an error if you hit docker run, but you'll get the point and probably can go on from here. Maybe you'll never hit these cases anyway.

Upvotes: 6

Related Questions