The 2-Hour Bash Bug That Taught Me How Quoting Actually Works
说明与用途
在 mini-paas 项目的 post-receive 钩子中,未对 $APP_NAME 进行引号处理,导致 Docker 容器停止和删除失败,出现名称冲突。使用 set -x 发现问题,添加引号后修复了自动部署流程。
问题
Automated redeploys failed silently because Docker stop and rm commands used the literal string "APP_NAME" instead of the variable value, causing container conflicts on subsequent pushes.
结果
Adding proper quoting to variable usage in Bash resolved silent redeploy failures, enabling reliable automated deployments.
工作流
- 1. Push code to bare Git repo. 2. post-receive hook checks out code. 3. Build Docker image. 4. Stop old container with "docker stop $APP_NAME". 5. Remove old container with "docker rm $APP_NAME". 6. Run new container with "docker run -d --name myapp -p 5001:5000 myapp".
- 7. Verify deployment.
- 8. Debug failed redeploys by adding "set -x" to the hook to trace execution.
- 9. Identify that "docker stop" and "docker rm" were using literal "APP_NAME" due to missing quotes.
- 10. Fix by quoting variable: "docker stop \"$APP_NAME\"" and "docker rm \"$APP_NAME\"".
- 11. Redeploy succeeds.
- 12. Adopt "set -x" as first diagnostic step for silent failures.
证据
docker: Error response from daemon: Conflict. The container name "/myapp" is already in use...
+ docker stop APP_NAME + docker rm APP_NAME + docker run -d --name myapp -p 5001:5000 myapp
docker stop "\$APP_NAME" docker rm "\$APP_NAME"
- 记录日期: 2026-09-17T20:15:26.865Z