I am new to docker. I am writing a simple script for docker. I need to check whether docker is running or not. Is there a command to check with container name
问题:
回答1:
If you are looking for a specific container, you can run:
docker inspect -f '{{.State.Running}}' $container_name
If you want to know if dockerd is running itself on the local machine and you have systemd installed, you can run:
systemctl show --property ActiveState docker
You can also connect to docker with docker info
or docker version
and they will error out if the daemon is unavailable.
回答2:
I ended up using
docker info
to check with a bash script if docker engine is running.
回答3:
you can check docker state using: systemctl is-active docker
➜ ~ systemctl is-active docker
active
you can use it as:
➜ ~ if [ "$(systemctl is-active docker)" = "active" ]; then echo "is alive :)" ; fi
is alive :)
➜ ~ sudo systemctl stop docker
➜ ~ if [ "$(systemctl is-active docker)" = "active" ]; then echo "is alive :)" ; fi
* empty response *
回答4:
List all containers:
docker container ls -a
ls
= list
-a
= all
Check the column "status"
回答5:
For OS X users (Mojave 10.14.3)
Here is what i use in my Bash script to test if Docker is running or not
# Check if docker is running
docker_state=$(docker info >/dev/null 2>&1)
if [[ $? -ne 0 ]]; then
echo "Docker does not seem to be running, run it first and retry"
exit 1
fi
回答6:
Any docker command (except docker -v
), like docker ps
If Docker is running, you'll get some valid response, otherwise you'll get a message that includes "Is your docker daemon up and running?"
You can also check your task manager.
回答7:
Sometimes you don't know the full container name, in this case this is what worked for me:
if [ $(docker ps | grep keyword | wc -l) -gt 0 ]
then
echo "Running!"
else
echo "Not running!"
exit 1
fi
We list all the running container processes (docker ps -a would show us also not running ones, but that's not what I needed), we search for a specific word (grep part) and simply count the lines of the result (wc -l), if it's greater than 0 it means we found some running containers which names contain our keyword.
回答8:
You can check with this command systemctl status docker
it will show the status of the docker. If you want to start you can use systemctl start docker
instead of systemctl
you can try also with service
, service docker status
and service docker start
respectively.
回答9:
Run:
docker version
If docker is running you will see:
Client: Docker Engine - Community
Version: ...
[omitted]
Server: Docker Engine - Community
Engine:
Version: ...
[omitted]
If docker is not running you will see:
Client: Docker Engine - Community
Version: ...
[omitted]
Error response from daemon: Bad response from Docker engine
回答10:
You can also check if a particular docker container is running or not using following command:
docker inspect postgres | grep "Running"
This command will check if for example my postgres container is running or not and will return output as "Running": true
Hope this helps.
回答11:
If the underlying goal is "How can I start a container when Docker starts?"
We can use Docker's restart policy
To add a restart policy to an existing container:
Docker: Add a restart policy to a container that was already created
Example:
docker update --restart=always <container>
回答12:
on a Mac you might see the image:
if you right click on the docker icon then you see:
alternatively:
docker ps
and
docker run hello-world