I have a Rails app I am deploying in Docker containers via Ansible. My app includes three containers so far:
docker volume create --name dbdata
)volumes_from
dbdata)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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With