Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you run a Docker container if it doesn't exist yet, or start it if it does?

Tags:

docker

For instance if I have a run command like this:

docker run --name yo -v "$(pwd)":/usr/src/myapp -w /usr/src/myapp ubuntu sh tmp.sh 

If I run it again, it spits out this error:

Error response from daemon: Conflict, the name yo is already assigned to c23849234e. You have to delete (or rename) that container to be able to assign yo to a container again.

It works with docker start though:

docker start -ia yo

So I just want to do one or the other.

like image 568
Travis Reeder Avatar asked Oct 24 '14 00:10

Travis Reeder


1 Answers

Simple answer, use the shells || operation. So if run fails, it will start instead:

docker run --name yo -v $PWD:/usr/src/myapp -w /usr/src/myapp ubuntu sh tmp.sh || docker start -ia yo
like image 92
Travis Reeder Avatar answered Oct 29 '22 15:10

Travis Reeder