[英]How to check if the docker engine and a docker container are running?
在腳本中,我需要檢查:
a) docker 引擎在運行嗎?
b) 給定容器名稱,docker 容器是否正在運行?
[這個問題最初的措辭有歧義,有人解讀為“check docker engine”,也有人解讀為“check docker container”]
如果您正在尋找特定的容器,您可以運行:
if [ "$( docker container inspect -f '{{.State.Running}}' $container_name )" == "true" ]; then ...
為避免容器處於崩潰循環中並不斷重新啟動以顯示它已啟動的問題,可以通過檢查Status
字段來改進上述內容:
if [ "$( docker container inspect -f '{{.State.Status}}' $container_name )" == "running" ]; then ...
如果你想知道 dockerd 是否在本地機器上運行並且你已經安裝了 systemd,你可以運行:
systemctl show --property ActiveState docker
您還可以使用docker info
或docker version
連接到 docker,如果守護程序不可用,它們會出錯。
我最終使用
docker info
使用 bash 腳本檢查 docker 引擎是否正在運行。
您可以使用以下命令檢查 docker 狀態: systemctl is-active docker
➜ ~ systemctl is-active docker
active
您可以將其用作:
➜ ~ 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 *
列出所有容器:
docker container ls -a
ls
= 列表
-a
= 全部
檢查列“狀態”
對於 OS X 用戶(Mojave 10.14.3)
這是我在 Bash 腳本中用來測試 Docker 是否正在運行的內容
# Check if docker is running
if ! docker info >/dev/null 2>&1; then
echo "Docker does not seem to be running, run it first and retry"
exit 1
fi
任何 docker 命令(除了docker -v
),比如docker ps
如果 Docker 正在運行,你會得到一些有效的響應,否則你會得到一條消息,其中包括“你的 docker 守護進程是否啟動並運行?”
您還可以檢查您的任務管理器。
有時您不知道完整的容器名稱,在這種情況下,這對我有用:
if docker ps | grep -q keyword
then
echo "Running!"
else
echo "Not running!"
exit 1
fi
我們列出所有正在運行的容器進程( docker ps -a
會顯示我們也沒有運行,但這不是我需要的),我們搜索一個特定的詞( grep
部分),如果我們沒有找到至少一個正在運行的,就會失敗名稱包含我們關鍵字的容器。
您還可以使用以下命令檢查特定的 docker 容器是否正在運行:
docker inspect postgres | grep "Running"
例如,此命令將檢查我的 postgres 容器是否正在運行,並將輸出返回為"Running": true
希望這可以幫助。
有關第一個問題的答案,請參閱此答案 - https://stackoverflow.com/a/65447848/4691279
對於您的第二個問題-您可以使用docker ps --filter "name=<<<YOUR_CONTAINER_NAME>>>"
來檢查特定容器是否正在運行。
如果 Docker 和 Container 都在運行,那么您將獲得如下輸出:
$ docker ps --filter "name=nostalgic_stallman" CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9b6247364a03 busybox "top" 2 minutes ago Up 2 minutes nostalgic_stallman
如果 Docker 未運行,您將收到一條錯誤消息,指出 docker daemon 未運行。
如果 Docker 正在運行但 Container 沒有運行,那么您將不會在此命令的輸出中獲得容器名稱。
您可以使用此命令systemctl status docker
它會顯示systemctl status docker
。 如果你想啟動你可以使用systemctl start docker
而不是systemctl
你也可以分別嘗試使用service
、 service docker status
和service docker start
。
docker ps -a
您可以看到所有 docker 容器,無論它是活的還是死的。
容器狀態:真/假
# docker inspect --format '{{json .State.Running}}' container-name
true
#
跑:
docker version
如果 docker正在運行,您將看到:
Client: Docker Engine - Community
Version: ...
[omitted]
Server: Docker Engine - Community
Engine:
Version: ...
[omitted]
如果 docker未運行,您將看到:
Client: Docker Engine - Community
Version: ...
[omitted]
Error response from daemon: Bad response from Docker engine
在終端中運行此命令:
docker ps
如果 docker 未運行,您將收到以下消息:
來自守護進程的錯誤響應:撥號 unix docker.raw.sock:連接:連接被拒絕
如果基本目標是“Docker 啟動時如何啟動容器?”
我們可以使用 Docker 的重啟策略
向現有容器添加重啟策略:
例子:
docker update --restart=always <container>
我有一個更充實的例子,在 Gitea 容器的上下文中使用上面的一些工作,但它可以根據名稱輕松轉換為另一個容器。 此外,您可能可以使用docker ps --filter
功能在較新的系統或沒有使用 docker-compose 的系統中設置 $GITEA_CONTAINER。
# Set to name or ID of the container to be watched.
GITEA_CONTAINER=$(./bin/docker-compose ps |grep git|cut -f1 -d' ')
# Set timeout to the number of seconds you are willing to wait.
timeout=500; counter=0
# This first echo is important for keeping the output clean and not overwriting the previous line of output.
echo "Waiting for $GITEA_CONTAINER to be ready (${counter}/${timeout})"
#This says that until docker inspect reports the container is in a running state, keep looping.
until [[ $(docker inspect --format '{{json .State.Running}}' $GITEA_CONTAINER) == true ]]; do
# If we've reached the timeout period, report that and exit to prevent running an infinite loop.
if [[ $timeout -lt $counter ]]; then
echo "ERROR: Timed out waiting for $GITEA_CONTAINER to come up."
exit 1
fi
# Every 5 seconds update the status
if (( $counter % 5 == 0 )); then
echo -e "\e[1A\e[KWaiting for $GITEA_CONTAINER to be ready (${counter}/${timeout})"
fi
# Wait a second and increment the counter
sleep 1s
((counter++))
done
我如何檢查 SSH.Run:
systemctl
如果響應:無法獲得 D-Bus 連接:不允許操作
它是一個 docker 或 WSL 容器。
檢查 .State.Status、.State.Running 等會告訴您它是否正在運行,但最好確保容器的健康狀況。 下面是一個您可以運行的腳本,該腳本將確保在第二個容器中執行命令之前兩個容器處於良好狀態。 如果已達到等待時間/嘗試閾值,它會打印出 docker 日志。
示例取自npm sql-mdb 。
#!/bin/bash
# Wait for two docker healthchecks to be in a "healthy" state before executing a "docker exec -it $2 bash $3"
##############################################################################################################################
# $1 Docker container name that will wait for a "healthy" healthcheck (required)
# $2 Docker container name that will wait for a "healthy" healthcheck and will be used to run the execution command (required)
# $3 The actual execution command that will be ran (required). When "npm_deploy", all tokens will be included in execution of
# "npm run jsdoc-deploy" and "npm publish"
attempt=0
health1=checking
health2=checking
while [ $attempt -le 79 ]; do
attempt=$(( $attempt + 1 ))
echo "Waiting for docker healthcheck on services $1 ($health1) and $2 ($health2): attempt: $attempt..."
if [[ health1 != "healthy" ]]; then
health1=$(docker inspect -f {{.State.Health.Status}} $1)
fi
if [[ $health2 != "healthy" ]]; then
health2=$(docker inspect -f {{.State.Health.Status}} $2)
fi
if [[ $health1 == "healthy" && $health2 == "healthy" ]]; then
echo "Docker healthcheck on services $1 ($health1) and $2 ($health2) - executing: $3"
docker exec -it $2 bash -c "$3"
[[ $? != 0 ]] && { echo "Failed to execute \"$3\" in docker container \"$2\"" >&2; exit 1; }
break
fi
sleep 2
done
if [[ $health1 != "healthy" || $health2 != "healthy" ]]; then
echo "Failed to wait for docker healthcheck on services $1 ($health1) and $2 ($health2) after $attempt attempts"
docker logs --details $1
docker logs --details $2
exit 1
fi
現在,執行 docker ps -a 檢查新創建的容器的狀態。
docker ps-a
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.