Automating Docker Container Deployment with Ansible: Building an Nginx ARM64 Container.

Jagrit Thapar

722 words • 4 min read

Introduction

Ansible is an open-source automation tool that can be used to automate various tasks, including the creation and configuration of Docker containers. In this blog post, we will go through an Ansible configuration that creates a network, pulls an Nginx image, and creates a container running Nginx on port 4530. We will explain each task in detail and provide a conclusion at the end.

📁 Check if the directory exists

The first task in the playbook checks if the directory ~/nginx-test exists on the target nodes. If it doesn't exist, the stat module will return false and the when condition will evaluate to true, triggering the next task to create the directory.

- name: Check if the directory exists
  stat:
    path: ~/nginx-test
  register: dir_status
 
- name: Create the directory if it does not exist
  file:
    path: ~/nginx-test
    state: directory
  when: not dir_status.stat.exists

🌉 Create a network

The second task creates a Docker network named my-network-test with a bridge driver. This network will be used by the Nginx container to communicate with other containers or services.

- name: Create a network
  community.docker.docker_network:
    name: my-network-test
    driver: bridge
    state: present

📥 GET Nginx image

The third task pulls the arm64v8/nginx:1.25.4 Docker image from a registry. This image is specifically built for ARM64 architecture, which is commonly used in cloud environments. By pulling the image, Ansible ensures that it is available on the target nodes before creating the container.

- name: GET Nginx image
  community.docker.docker_image_pull:
    name: arm64v8/nginx:1.25.4
    pull: not_present
    # state: present

🐳 Create Nginx ARM64 container

The fourth task creates a Docker container named nginx using the arm64v8/nginx:1.25.4 image. The container is set to the started state, which means it will be started automatically when created. The container is also configured to expose port 80 internally and map it to port 4530 on the host machine. This allows you to access the Nginx web server from the host machine.

- name: Create Nginx ARM64 container
  community.docker.docker_container:
    name: nginx
    image: arm64v8/nginx:1.25.4
    state: started
    # platform: arm64/v8
    ports:
      - "4530:80"
    # volumes:
      # - "/path/to/nginx/conf:/etc/nginx/conf.d"
      # - "/path/to/nginx/html:/usr/share/nginx/html"
    restart_policy: always

📋 Check if Nginx container is running

The fifth task retrieves information about the nginx container and stores it in the nginx_container_info variable. This information includes the container's state, network settings, and other details.

- name: Check if Nginx container is running
  community.docker.docker_container_info:
    name: nginx
  register: nginx_container_info

⚠️ Ensure Nginx container is running

The final task checks if the nginx container is running by examining the Status field in the nginx_container_info variable. If the container is not running, the playbook will fail with an error message. This is an important step to ensure that the Nginx web server is up and running before proceeding with any further configuration or testing.

- name: Ensure Nginx container is running
  fail:
    msg: "Nginx container is not running"
  when: not nginx_container_info.container.State.Status == "running"

Full Code

---
- name: Create network and build Nginx image
  hosts: nodes
  gather_facts: yes
  tasks:
    - name: Check if the directory exists
      stat:
        path: ~/nginx-test
      register: dir_status
 
    - name: Create the directory if it does not exist
      file:
        path: ~/nginx-test
        state: directory
      when: not dir_status.stat.exists
 
    - name: Create a network
      community.docker.docker_network:
        name: my-network-test
        driver: bridge
        state: present
 
    - name: GET Nginx image
      community.docker.docker_image_pull:
        name: arm64v8/nginx:1.25.4
        pull: not_present
        # state: present
 
    - name: Create Nginx ARM64 container
      community.docker.docker_container:
        name: nginx
        image: arm64v8/nginx:1.25.4
        state: started
        # platform: arm64/v8
        ports:
          - "4530:80"
        # volumes:
          # - "/path/to/nginx/conf:/etc/nginx/conf.d"
          # - "/path/to/nginx/html:/usr/share/nginx/html"
        restart_policy: always
 
    - name: Check if Nginx container is running
      community.docker.docker_container_info:
        name: nginx
      register: nginx_container_info
 
    - name: Ensure Nginx container is running
      fail:
        msg: "Nginx container is not running"
      when: not nginx_container_info.container.State.Status == "running"

🌟 Conclusion

In this blog post, we went through an Ansible configuration that creates a network, pulls an Nginx image, and creates a container running Nginx on port 4530. We explained each task in detail and provided a conclusion at the end. By using Ansible, you can automate various tasks, including the creation and configuration of Docker containers, making it easier to manage and scale your applications. We hope you found this blog post helpful and informative. If you have any questions or comments, please let us know. Happy automating! 🤖