RECOVERY AND DELETION OF STOPPED DOCKER CONTAINERS

Running container

517cb347cedc        env_14_apache-php54-node    ...      Up 20 minutes  ...

Stopped container

517cb347cedc        env_14_apache-php54-node    ...    Exited (0) 4 seconds ago    ...

Problematic

9c2d08659089        env_14_apache-php54-node   ...   Up 40 seconds  ...
9c2d08659089        env_14_apache-php54-node    ...    Exited (0) 4 seconds ago    ...
profideo@env_14_apache-php54-node:~$ ls
medias
profideo@env_14_apache-php54-node:~$ mkdir test
profideo@env_14_apache-php54-node:~$ ls
medias  test
profideo@env_14_apache-php54-node:~$
./createEnv.sh 14
- name: Check or build docker image
  docker_image: path="{{basepath}}/{{user}}/{{item.image}}/"
                name="{{ user }}_{{ item.image }}"
                state=build
  with_items: containers

- name: Run docker image
  docker: image="{{ user }}_{{ item.image }}"
          name="{{ user }}_{{ item.image }}"
          hostname="{{ user }}_{{ item.image }}"
          state=running
  with_items: containers

<==>

profideo@env_14_apache-php54-node:~$ ls
medias
profideo@env_14_apache-php54-node:~$
profideo@server-dev4:~|⇒  docker stop env_14_apache-php54-node
env_14_apache-php54-node
profideo@server-dev4:~|⇒ 

Issue

997015c2094b        env_14_apache-php54-node   ...   Up 40 seconds  ...
997015c2094b        env_14_apache-php54-node    ...    Exited (0) 4 seconds ago    ...
profideo@env_14_apache-php54-node:~$ ls
medias
profideo@env_14_apache-php54-node:~$ mkdir test
profideo@env_14_apache-php54-node:~$ ls
medias  test
profideo@env_14_apache-php54-node:~$
profideo@env_14_apache-php54-node:~$ ls
medias  test
profideo@env_14_apache-php54-node:~$
profideo@server-dev4:~|⇒  docker stop env_14_apache-php54-node
env_14_apache-php54-node
profideo@server-dev4:~|⇒ 
profideo@server-dev4:~|⇒  docker start env_14_apache-php54-node
env_14_apache-php54-node
profideo@server-dev4:~|⇒ 
MAN docker
    build     Build an image from a Dockerfile
    load      Load an image from a tar archive
    restart   Restart a running container
    start     Start a stopped container            => This one seems to do what we need

Ansible - Docker states

  • present : only asserts that the matching containers exist.
  • started : asserts that the matching containers both exist and are running, but takes no action if any configuration (DockerFile) has changed.
  • reloaded : (added in Ansible 1.9) asserts that all matching containers are running and restarts any that have any images or configuration out of date.
  • restarted : unconditionally restarts (or starts) the matching containers.
  • stopped / killed : stop and kill all matching containers.
  • absent : stops and then' removes any matching containers.

Ansible - Docker states

started : It doesn't do  what it seems to mean.

...

def started(manager, containers, count, name):
   '''Ensure that exactly `count` matching containers exist and are running.'''
   
   containers.refresh()
   delta = count - len(containers.running)
   if delta > 0:
        if name and containers.deployed:
        # A stopped container exists with the requested name.
        # Clean it up before attempting to start a new one.
        manager.remove_containers(containers.deployed)
        created = manager.create_containers(delta)
        manager.start_containers(created)
        containers.notice_changed(created)
   if delta < 0:
        excess = containers.running[0:-delta]
        manager.stop_containers(excess)
        manager.remove_containers(excess)
        containers.notice_changed(excess)

Source : Ansible GitHub

Ansible - Solution

  - name: Check if docker image exists
    shell:  if [ -n "`docker ps -a | grep {{ user }}_{{ item.image }}`" ]; then 
                docker start {{ user }}_{{ item.image }};
            else
                echo "NOT EXISTING CONTAINER";
            fi
    with_items: containers
    register: docker_image_restored
    ignore_errors: yes
    tags:
      - docker

  - name: Restore a not existing container
    fail: msg="Cannot restore a not existing container '{{ item.item.image }}'.
                Please run createEnv to rebuild ALL containers"
    when: item.changed == true and item.stdout == 'NOT EXISTING CONTAINER'
    with_items: docker_image_restored.results|list

That's all for Docker container recovery ...

REMOVE ALL STOPPED CONTAINERS

  - name: Check confirmation
    fail: msg="No stopped container has been removed. No action."
    when: confirmation == "NO"

  - name: Remove stopped containers
    shell: if [ -n "`docker ps -a | grep Exited`" ]; then 
            docker ps -a | grep Exited | cut -d ' ' -f 1 | xargs docker rm; 
           fi
    ignore_errors: yes
    when: confirmation == "YES"
    tags:
      - docker

That's all for me ... Thanks

Atelier Docker+Ansible

By Ibrahima Sow

Atelier Docker+Ansible

A docker container can be stopped for different reasons. When we execute `docker start DOCKER_TAG` in command line, the container runs again with the modifications made before preserved. Ansible has a docker module with "finite states" but none of them just do a START. Here is a quick ansible task to start a stopped docker container

  • 284