Configuring Docker using Ansible

Configuring Docker using Ansible

Task 1: Write an Ansible Playbook that does the following operations in the managed nodes:

1. Configure Docker

2. Start and enable Docker services

3. Pull the httpd server image from the Docker Hub

4. Run the httpd container and expose it to the public

5. Copy the HTML code in /var/www/html directory and start the webserver

Let's get started

Create a task1.yml file and paste upload the below code

- hosts: all
  tasks:
    - name: Add_repository
      yum_repository:
        name: docker
        description: Docker repo
        baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable/"
        gpgcheck: no
    
    - name: Installing Docker Software
      package:
        name: "docker-ce-18.06.3.ce-3.el7.x86_64"
        state: present
    
    - name: Start the Docker service
      service:
        name: "docker"
        state: started
    
    - pip:
        name: docker
    
    - name: Pull the httpd docker image from dockerhub
      docker_image:
        name: "httpd"
        source: pull
    
    - name: Copy html page "task1.html"
      copy:
        src: "task1.html"
        dest: "/home"
    
    - name: Creating the Container with httpd image
      docker_container:
        name: webos
        image: httpd
        state: started
        ports:
          - "3000:80"
        volumes:
                
          - /home:/usr/local/apache2/htdocs/

And then just run the below command and everything will be configured for you:

$ ansible-playbook task1.yml

Now, for better understanding, I will divide the steps and explain each of it separately

Step 1: Configuring the yum repository

- hosts: all
  tasks:
    - name: Add_repository
      yum_repository:
        name: docker
        description: Docker repo
        baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable/"
        gpgcheck: no

This will create a new docker.repo in the /etc/yum.repo.d/ path as shown below

No alt text provided for this image

Step 2: Now we will install the docker

- hosts: all
  tasks:
    - name: Installing Docker Software
      package:
        name: "docker-ce-18.06.3.ce-3.el7.x86_64"
        
        state: present

This will install the docker and can be checked as below

No alt text provided for this image

Step 3: Next we will start the docker service

 - hosts: all
   tasks: 
     - name: Start the Docker service
       service:
         name: "docker"
      
         state: started

This will start the docker service and can be checked as below

No alt text provided for this image

Step 4: Using the docker modules requires having the Docker SDK for Python installed on the host running Ansible. You will need to have >= 1.7.0 installed. For Python 2.7 or Python 3, you can install it as follows:

 - hosts: all
   tasks:
     - pip:
        name: docker 
     

Step 5: Pulling the httpd docker image from docker hub

 - hosts: all
   tasks:
     - name: Pull the httpd docker image from dockerhub
       docker_image:
         name: "httpd"
         source: pull
       

This will download the httpd image as below

No alt text provided for this image

Step 6: Copying the HTML page

- hosts: all
  tasks:
    - name: Copy html page "task1.html"
      copy:
        src: "task1.html"
        dest: "/home"

This will copy the task1.html file to the /home dir of the host machine

Step 7: Running the httpd image

- hosts: all
  tasks:
    - name: Creating the Container with httpd image
      docker_container:
        name: webos
        image: httpd
        state: started
        ports:
          - "3000:80"
        volumes:
                
          - /home:/usr/local/apache2/htdocs/

This will create a new container with name as webos having the task1.html file and padded at port 8000. Can be checked as follow

No alt text provided for this image

That's all our docker is configured and a container is running successfully. The webpage can be view in the browser at port 3000 as shown below

No alt text provided for this image

All the files can be accessed at my Github repository here:


To view or add a comment, sign in

More articles by Sarthak Phatate

Others also viewed

Explore content categories