I would like to have a shell script that checks if a particular container is running.
For example, I might want to start the container if it’s not already running, or query some piece of information about it like what ports are exposed.
I would like to have a shell script that checks if a particular container is running.
For example, I might want to start the container if it’s not already running, or query some piece of information about it like what ports are exposed.
2
Answers
The
docker ps
command takes a-f
/--filter
option to locate the desired container. To test if the container is running by name we might tryIf we just want the container ID, because we're going to pass it to another command like
docker exec
, we can use-q
/--quiet
:To just check whether it is running, we can see if the output is non-empty:
Or if we want some other piece of information about it,
--format
:You might also try using the docker inspect command which works well with myapp as the container’s name … or the container’s id :