Ansible Play-Book to Launch web-server on Docker, TASK-10 #Arth.

Pradeep Kvssk
4 min readJan 2, 2021

--

🔰Write an Ansible Play-Book that does the
following operations in the managed nodes:
🔹 Configure Docker
🔹 Start and enable Docker services
🔹 Pull the httpd server image from the Docker Hub
🔹 Run the docker container and expose it to the public
🔹 Copy the html code in /var/www/html directory
and start the web server

When Do You Need to Use a Docker?

  • For replicating the environment on your server, while running your code locally on your laptop
  • For Docker CI/CD during numerous development phases (dev/test/QA)
  • For distributing your app’s OS with a team, and as a version control system.

How Do You Setup a Docker Locally

  • Download a Docker edition and the Docker Toolbox
  • Make sure your BIOS has Virtualization Technologies, AMD-V, or KVM enabled
  • Install the Extension Pack in the Oracle VirtualBox.
  • Run the Setup

How Do You Use a Docker?

The biggest advantage of VMs is that they create snapshots which can be revisited instantly later. Docker containers further enhance the lightweight process virtualization by being OS independent and using the Linux Kernel’s functionality. They are created from Docker images — like snapshots. Docker images are created using a Docker file which can be customized or used as is. The default execution driver for creating a docker container is ‘libcontainer’. Docker Hub can be used for searching docker images and seeing the way they have been built.

  • To create a Docker container, download the ‘hello world’ image, by typing the following command in the terminal –

$ docker run hello world

  • For checking the number of images on your system, use the following command –

$ docker images

  • For searching an image in the Docker Hub –

$ docker search <image>

Here’s a List of Docker Commands

  • docker run — Runs a command in a new container.
  • docker start — Starts one or more stopped containers
  • docker stop — Stops one or more running containers
  • docker build — Builds an image form a Docker file
  • docker pull — Pulls an image or a repository from a registry
  • docker push — Pushes an image or a repository to a registry
  • docker export — Exports a container’s filesystem as a tar archive
  • docker exec — Runs a command in a run-time container
  • docker search — Searches the Docker Hub for images
  • docker attach — Attaches to a running container
  • docker commit — Creates a new image from a container’s changes

Docker deployment was once replacement to the solution of heavy time consuming booting stuff to an end..

First in order to setup the Docker through ansible the basic pre-requirements of ansible are the python dependency.

And so the steps are as follows:

1. Adding the new docker repo

2. Installing Docker

3. Starting Docker Service

4. Installing python Latest Version

5. Installing Web Server on top of the Docker

6. Docker container is running in background you can get into anytime

7. disabling firewall to access server from other host

8. check container status

hosts: RProxy
tasks:
— name: Editing the new docker repo
yum_repository:
name: docker
description: setting repo for docker-ce
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
gpgcheck: no

— name: Installing Docker
command: “yum install docker-ce — nobest -y”
— name: Starting Docker Service
service:
name: docker
state: started
— name: Installing python Latest Version
package:
name: python3
— name: Installing WebServer on top of the Docker
docker_image:
name: httpd
state: present

— name: Docker container is running in background you cna get into anytime
docker_container:
detach: yes
name: httpd_docker
ports: “8080:80”
volumes:
— “/var/www/html/:/usr/local/apache2/htdocs/:ro”
image: httpd

— name: disabling firewall to access server from other host
command: systemctl stop firewalld
— name: check container status
command: docker ps
register: res
— debug: var=res.stdout

Deployed web-server on top of Docker
#docker info to connect to the docker_IP

. . . . . . . . . . . . . . . . .. . . . . . . . . . . . Thank you . . .. . . . . . . . . . . .. . . . . . . . . . .. . . . .

--

--

Pradeep Kvssk

Follow me for more Updates :) Passionate — >>🚩,🔍,🔮,Passion