How to Use Ansible for Container Deployment

Are you tired of manually deploying containers on your servers? Do you want to automate the process and save time? If yes, then Ansible is the tool you need. Ansible is an open-source automation tool that allows you to automate the deployment and configuration of applications on multiple servers simultaneously. In this article, we will show you how to use Ansible for container deployment.

Prerequisites

Before we dive into the details, let's make sure you have the necessary prerequisites:

If you don't have Ansible installed on your server, you can follow the official documentation to install it.

Step 1: Create an Ansible Inventory

The first step in using Ansible for container deployment is to create an Ansible inventory. An inventory is a file that contains a list of servers that Ansible will manage. You can create an inventory file by running the following command:

$ sudo nano /etc/ansible/hosts

This will open the inventory file in the nano text editor. You can add the IP addresses or hostnames of your servers in this file. For example:

[web_servers]
192.168.1.100
192.168.1.101

[database_servers]
192.168.1.102

In this example, we have two groups of servers: web_servers and database_servers. The web_servers group contains two servers with IP addresses 192.168.1.100 and 192.168.1.101, while the database_servers group contains one server with IP address 192.168.1.102.

Step 2: Install Docker on the Servers

The next step is to install Docker on the servers. You can use Ansible to install Docker on all the servers in your inventory by creating a playbook. A playbook is a file that contains a set of tasks that Ansible will execute on the servers.

Create a new file called docker.yml in the /etc/ansible/playbooks directory and add the following code:

---
- hosts: all
  become: true
  tasks:
    - name: Install Docker
      apt:
        name: docker.io
        state: present

This playbook will install Docker on all the servers in your inventory. To run this playbook, use the following command:

$ ansible-playbook /etc/ansible/playbooks/docker.yml

Step 3: Create a Docker Image

The next step is to create a Docker image that you want to deploy on your servers. You can create a Docker image by writing a Dockerfile. A Dockerfile is a text file that contains a set of instructions that Docker will use to build an image.

Create a new directory called myapp and create a new file called Dockerfile in that directory. Add the following code to the Dockerfile:

FROM ubuntu:latest
RUN apt-get update && apt-get install -y nginx
COPY index.html /var/www/html/
CMD ["nginx", "-g", "daemon off;"]

This Dockerfile will create an image based on the latest version of Ubuntu, install Nginx, copy an index.html file to the /var/www/html/ directory, and start Nginx.

Create a new file called index.html in the myapp directory and add some HTML code to it. This file will be copied to the /var/www/html/ directory in the Docker container.

Step 4: Create an Ansible Playbook to Deploy the Container

The next step is to create an Ansible playbook to deploy the Docker container on your servers. Create a new file called deploy.yml in the /etc/ansible/playbooks directory and add the following code:

---
- hosts: web_servers
  become: true
  tasks:
    - name: Pull Docker Image
      docker_image:
        name: myapp
        tag: latest
        source: pull
    - name: Run Docker Container
      docker_container:
        name: myapp
        image: myapp:latest
        ports:
          - "80:80"

This playbook will deploy the Docker container on the servers in the web_servers group. It will pull the myapp image from Docker Hub and run a container with the name myapp and the latest tag. It will also map port 80 on the host to port 80 in the container.

To run this playbook, use the following command:

$ ansible-playbook /etc/ansible/playbooks/deploy.yml

Step 5: Test the Deployment

The final step is to test the deployment. Open a web browser and enter the IP address of one of your web servers. You should see the index.html file that you created earlier.

Congratulations! You have successfully deployed a Docker container using Ansible.

Conclusion

In this article, we have shown you how to use Ansible for container deployment. We have covered the basics of Ansible and Docker, and we have shown you how to create an Ansible inventory, install Docker on your servers, create a Docker image, and deploy a Docker container using Ansible. With Ansible, you can automate the deployment and configuration of your applications, saving you time and effort.

Editor Recommended Sites

AI and Tech News
Best Online AI Courses
Classic Writing Analysis
Tears of the Kingdom Roleplay
Data Ops Book: Data operations. Gitops, secops, cloudops, mlops, llmops
Modern Command Line: Command line tutorials for modern new cli tools
AI ML Startup Valuation: AI / ML Startup valuation information. How to value your company
ML Platform: Machine Learning Platform on AWS and GCP, comparison and similarities across cloud ml platforms
NFT Sale: Crypt NFT sales