Run Commands On Docker Containers With Ansible

This post will show you how to use Ansible to deploy a container and run a command once the container starts.

Our previous posts showed how to get started with Ansible and the Docker module to deploy containers and volume.

Playbook

In the below playbook, I am deploying a new container and using the command option to run a command when the container starts. The command that I’m using will keep the container running and will not exit.


- hosts: localhost

  tasks:
    - name: Create a data container
      community.docker.docker_container:
        name: web1
        image: ubuntu:latest
        command:
          - tail -f /dev/null

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.