FoxuTech

How to Create Docker container using Ansible

How to Create Docker container using Ansible

Ansible and Docker getting occupy vest place in DevOps environment and getting more famous now a day. Let’s see how to create a docker container using ansible.

Let’s start our playbook by giving it a name of Creating Docker Container. This playbook will be executed on host localhost with connection type as local using user root. You can change it based on your requirement.

---
- name: Creating Docker Container
  hosts: localhost
  connection: local 
  remote_user: root

Start with our tasks. First one is to include our variables. This will make this playbook more generic.

tasks: 
    - name: include variables
      include_vars: vars.yml

Lets have a look at the vars.yml file

image: motoskia/apache-php 
name: andoc-web
src_port: 81
dest_port: 80 
src_vol: /mnt/www 
dest_vol: /var/www/html

privileged: true

This is just an example; you can modify it based on your requirement. Below is the description about each varialbes;

Next task will install python-docker to support docker modules available with Ansible 2.2

  - name: Install python-docker on Red Hat based distribution
    yum:
     name: python-docker
     enablerepo: extras
     state: latest
    when: ansible_os_family == 'RedHat'
  - name: Install python-docker  on Debian based distribution
    apt:
      name: python-docker
      update_cache: yes
    when: ansible_os_family == 'Debian'

docker_container module we can use it to create container. And using the variable defined in the vars.yml file

    - name: Create Container
      docker_container:
        name: "{{ name }}"
        image: "{{ image }}"
        ports:
          - "{{ src_port }}:{{ dest_port }}"
        volumes:
          - "{{ src_vol }}:{{ dest_vol }}"
        privileged: "{{ privileged }}"

I would recommend to refer docker-container module document to know more about it and what else it can do.

# ansible-doc docker_container

That’s not it, how about creating a systemd service and starting our container with system boot. For this I have used Jinja2 templates.

This is a jinja2 systemd unit file template.

# cat systemd.j2

[Unit]
Description={{ name }} Docker Container 
Requires=docker.service 
After=docker.service
[Service]
Restart=always 
ExecStart=/usr/bin/docker start -a {{ name }} 
ExecStop=/usr/bin/docker stop -t 2 {{ name }}
[Install]
WantedBy=default.target

We use template module in ansible to create this unit file using the name variable defined in vars.yml

  - name: Create Systemd Unit File as docker-{{ name }}.service
      template: src=systemd.j2 dest=/etc/systemd/system/docker-{{ name }}.service
  - name: reload systemd daemon
    command: systemctl daemon-reload

Next task will enable this service and start it, which should also start our container.

   - name: Start & Enable docker-{{ name }} service
     service:
       name: docker-{{ name }}
       state: started
       enabled: yes

And finally print the docker ps output

    - name: check container status
      command: docker ps
      register: result
    - debug: var=result.stdout

Refer our Repo for complete playbook

How to Run

# ansible-playbook playbook.yml

Learn More : How to write Ansible playbook

Exit mobile version