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

Opsworks Recipe中Bash脚本执行失败求助:Docker清理脚本问题

Troubleshooting Failed Docker Cleanup Bash Script in OpsWorks Chef Recipe

Let’s walk through the common issues that might be causing your Docker cleanup script to fail, along with practical fixes to get it working reliably.

Common Failure Points & Fixes

1. Redundant sudo in Docker Commands

Your script runs as the root user, so adding sudo before docker images is unnecessary—and can actually cause issues if the sudo environment doesn’t include docker in its PATH (unlikely, but avoidable). Remove the sudo to keep commands consistent:

# Replace this:
docker rmi $(sudo docker images | grep -m 1 vouchdocker | awk {'print $3'})
# With this:
docker rmi $(docker images | grep -m 1 vouchdocker | awk {'print $3'})

2. Imprecise Container/Image Matching with grep

Using docker ps | grep scheduler1 can match unintended containers (e.g., one named scheduler1-test or a command string containing scheduler1). Use Docker’s built-in filtering for exact matches instead:

# Check for running scheduler1 container (exact name match)
if docker ps --filter "name=^/scheduler1$" --format "{{.Names}}" | grep -q scheduler1; then
  docker stop scheduler1
  sleep 5 # Give containers more time to shut down properly
  docker rm scheduler1
fi

# Check for any stopped scheduler1 containers
if docker ps -a --filter "name=^/scheduler1$" --format "{{.Names}}" | grep -q scheduler1; then
  docker rm scheduler1
fi

The --filter "name=^/scheduler1$" ensures only containers with the exact name scheduler1 are targeted, and --format "{{.Names}}" simplifies output for reliable grep checks.

3. Unhandled Empty Command Substitution for Image Deletion

If no vouchdocker images exist, the command substitution $(docker images | grep -m 1 vouchdocker | awk {'print $3'}) returns an empty string. Running docker rmi without arguments throws an error that might exceed your allowed returns codes. Fix this by storing the image ID in a variable and validating it first:

IMAGE_ID=$(docker images -q vouchdocker | head -1)
if [ -n "$IMAGE_ID" ]; then
  docker rmi "$IMAGE_ID"
fi

docker images -q directly outputs image IDs, making the command shorter and more reliable. We only run docker rmi if IMAGE_ID isn’t empty.

4. Insufficient Sleep Time

A 3-second sleep after docker stop might not be enough for some containers to fully terminate. Bumping this to 5-10 seconds prevents errors when trying to remove a container that’s still shutting down.

5. Missing Docker Path or Permissions

If the root user’s PATH doesn’t include the Docker binary location (rare, but possible), your script will fail to find docker. Use the absolute path to docker (e.g., /usr/bin/docker) to avoid this:

/usr/bin/docker ps --filter "name=^/scheduler1$"

Verify with Chef Logs

The best way to pinpoint the exact failure is to check your OpsWorks deployment logs. Look for lines starting with stderr: in the Chef run output—this will show you the specific error message (e.g., "docker: command not found" or "container not found").

Revised Working Script

Here’s the cleaned-up version of your bash block incorporating all these fixes:

bash "docker-cleanup" do
  user "root"
  returns [0, 1, 2] # Expand allowed exit codes to cover harmless errors
  code <<-EOH
    # Stop and remove running scheduler1 container
    if docker ps --filter "name=^/scheduler1$" --format "{{.Names}}" | grep -q scheduler1; then
      docker stop scheduler1
      sleep 5
      docker rm scheduler1
    fi

    # Remove any stopped scheduler1 container
    if docker ps -a --filter "name=^/scheduler1$" --format "{{.Names}}" | grep -q scheduler1; then
      docker rm scheduler1
    fi

    # Remove the first vouchdocker image (if exists)
    IMAGE_ID=$(docker images -q vouchdocker | head -1)
    if [ -n "$IMAGE_ID" ]; then
      docker rmi "$IMAGE_ID"
    fi
  EOH
end

内容的提问来源于stack exchange,提问作者Rahadian Kumang

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:00:20