Skip to content

Instantly share code, notes, and snippets.

@alexesDev
Created October 27, 2019 13:07
Show Gist options
  • Save alexesDev/67d1f5d596492e21d3b99609492a0030 to your computer and use it in GitHub Desktop.
Save alexesDev/67d1f5d596492e21d3b99609492a0030 to your computer and use it in GitHub Desktop.
Ansible zero downtime for docker_container
vars:
version: "{{ ansible_date_time.epoch }}"
backend_container_name: "backend-{{ version }}"
tasks:
- name: Run backend container
docker_container:
<<: *base-app
name: "{{ backend_container_name }}"
command: bundle exec puma -C config/puma.rb -t 1:3
restart_policy: always
healthcheck:
test: ["CMD", "sh", "-c", "wget -S localhost:3000/shop 2>&1 | grep -q '200 OK'"]
interval: 15s
timeout: 2s
retries: 3
start_period: 15s
labels:
traefik.enable: "true"
traefik.port: "3000"
traefik.A.frontend.rule: "Host:xxxx;PathPrefix:/shop"
- name: Wait backend healthy
shell: "docker ps | grep {{ backend_container_name }}"
register: ps_cmd
retries: 10
delay: 15
until: ps_cmd.stdout_lines | reject('match', ".*\(healthy\).*") | list | count == 0
- name: Stop other containers
shell: "docker ps | grep backend | grep -v {{ backend_container_name }} | awk '{ print $1 }' | xargs docker stop | xargs docker rm"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment