Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

The 2-Hour Bash Bug That Taught Me How Quoting Actually Works

Описание и применение

Скрипт post-receive в мини‑проекте «git push to deploy» не удалял старый контейнер из‑за отсутствия кавычек вокруг переменной $APP_NAME, что приводило к конфликту имен. Добавление 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