How to run a docker container if not already running

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)