Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run 'docker volume create' with Ansible?

Tags:

docker

ansible

I have a Rails app I am deploying in Docker containers via Ansible. My app includes three containers so far:

  • A Docker volume container (created with docker volume create --name dbdata)
  • A Postgres container (with volumes_from dbdata)
  • The Rails app container (which links to the postgres container)

My deploy playbook is working, but I had to run the docker volume create command on the server via SSH. I'd love to do that via Ansible, so I could deploy a fresh instance of the app onto an empty container.

Is there a way to run docker volume create via Ansible, or is there some other way to do it? I checked the docs for the Ansible Docker module but it doesn't look like they support volume create yet. Unless I'm missing something?

like image 737
David Ham Avatar asked Jan 27 '16 20:01

David Ham


People also ask

Does Docker run create a volume?

Use a volume with Docker ComposeRunning docker compose up for the first time creates a volume. The same volume is reused when you subsequently run the command. For more information about using volumes with Compose, refer to the Volumes section in the Compose specification.

Can Ansible build Docker image?

The Ansible docker_image module makes it easy to build, save, and load your images without ever hitting a repository. This article walks you through some simple playbooks that you can incorporate into your workflow to manage containers with Ansible.


1 Answers

Here's one option, using the command module.

- hosts: localhost
  tasks:
    - name: check if myvolume exists
      command: docker volume inspect myvolume
      register: myvolume_exists
      failed_when: false

    - name: create myvolume
      command: docker volume create --name myvolume
      when: myvolume_exists|failed

We first check if the volume exists by using docker volume inspect. We save the result of that task in the variable myvolume_exists, and then we only create the volume if the inspect task failed.

like image 141
larsks Avatar answered Oct 06 '22 23:10

larsks