How to check if docker is running or not

2020-05-15 08:09发布

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

标签: bash docker
12条回答
乱世女痞
2楼-- · 2020-05-15 08:17

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
查看更多
Evening l夕情丶
3楼-- · 2020-05-15 08:20

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.

查看更多
混吃等死
4楼-- · 2020-05-15 08:20

I ended up using

docker info

to check with a bash script if docker engine is running.

查看更多
做自己的国王
5楼-- · 2020-05-15 08:22

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 *
查看更多
Fickle 薄情
6楼-- · 2020-05-15 08:23

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>
查看更多
Melony?
7楼-- · 2020-05-15 08:24

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.

查看更多
登录 后发表回答