November 8, 2016 · docker
checking if a docker container is running
The following will return true if the container is running and false otherwise.
docker inspect -f {{.State.Running}} "$container_id"
The following fetches container id based on it's name,
name=alpine
count=$(docker ps | grep "$name" | wc -l)
if [ $count == 1 ]; then
id=$(docker ps | grep "$name" | cut -d ' ' -f 1)
echo "$container_id"
elif [ $count == 0 ]; then
echo "No container with name $name"
else
echo "more than one container with name $name"
fi
note: In the above example we use docker ps and not docker ps -a. So, we can already assume that the containers it returns are in running state.