You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在docker ps状态列或自定义列中展示Docker镜像内运行的所有服务状态,并在构建Docker容器时获取服务运行状态(如数据库数据上传状态)

How to Show Internal Service Status and Initialization Progress in docker ps

Great question! Let's tackle both of your requirements with practical, Docker-native solutions that play nicely with docker ps.

1. Expose Status of All Services Running Inside a Container

By default, docker ps only tracks the container's main process. To show the status of multiple services (like a web server + database), we'll use Docker's health check system to aggregate service health and display it directly in the STATUS column.

Step 1: Create a Health Check Script

Write a bash script that verifies the status of every service in your container. For example, if you're running PostgreSQL and Nginx:

#!/bin/bash
# Save this as /usr/local/bin/check-services.sh in your image

# Check if PostgreSQL accepts connections
pg_isready -h localhost -U postgres > /dev/null 2>&1
if [ $? -ne 0 ]; then
  echo "PostgreSQL is down"
  exit 1
fi

# Check if Nginx responds to health checks
curl -f http://localhost/health > /dev/null 2>&1
if [ $? -ne 0 ]; then
  echo "Nginx is down"
  exit 1
fi

# All services are healthy
exit 0

Step 2: Add Health Check to Your Dockerfile

Include the HEALTHCHECK directive to run your script on a regular schedule:

HEALTHCHECK --interval=30s \
            --timeout=10s \
            --start-period=10s \
            --retries=3 \
            CMD /usr/local/bin/check-services.sh

Step 3: View Status in docker ps

Now when you run docker ps, the STATUS column will show:

  • Up X seconds (healthy) if all services are running smoothly
  • Up X seconds (unhealthy) if any service fails the check
  • Up X seconds (starting) during the initial warm-up period

For a deeper dive, run docker inspect <container-id> to see full health check logs.

2. Track Database Upload Progress During Container Initialization

For one-time tasks like database data upload that run when the container starts, we can extend the health check system to show an "initializing" state until the task completes.

Step 1: Modify Your Startup Script

Update your entrypoint/startup script to create a flag file once the upload finishes:

#!/bin/bash
# Save this as /usr/local/bin/start-services.sh

# Start database upload with verbose output
echo "Starting database data upload..."
pg_restore -d mydatabase /path/to/backup.sql --verbose
# Mark initialization as complete
touch /tmp/init-completed
echo "Database upload finished!"

# Start your main service (e.g., Nginx)
exec nginx -g "daemon off;"

Step 2: Update the Health Check Script

Modify the health check to detect if initialization is still in progress:

#!/bin/bash
# Save this as /usr/local/bin/check-init-status.sh

if [ -f /tmp/init-completed ]; then
  # Initialization done - reuse your service health check here
  /usr/local/bin/check-services.sh
else
  # Initialization still running
  echo "Initializing database..."
  exit 1
fi

Step 3: Configure Health Check for Initialization

Adjust the HEALTHCHECK to give enough time for the upload to complete:

HEALTHCHECK --interval=5s \
            --timeout=5s \
            --start-period=120s \  # Adjust based on your typical upload duration
            --retries=3 \
            CMD /usr/local/bin/check-init-status.sh

Step 4: Monitor Progress in docker ps

While the upload runs, docker ps will show Up X seconds (starting) in the STATUS column. Once the upload finishes and the flag file is created, it will switch to healthy (or unhealthy if services fail the check).

For granular progress details, check the container logs with docker logs <container-id> while keeping an eye on the STATUS column in docker ps.


内容的提问来源于stack exchange,提问作者Hassan Zaheer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 09:12:43