Introduction to Docker

We will need to install the Docker application. You can download your version of Docker with the link below: 

Docker for Linux will need to be installed through the command line.

You will need a Docker account to use Docker so sign up. 

Docker for Windows requires Windows Pro or Enterprise to run so consider a Linux installation if you don’t own said Windows versions. 

To build an image in Docker you need to install some version of an operating system. 

Let's build a copy of CentOS with the latest version. Use the following command: 

   docker pull centos:latest   

To see the installed images, use the command: 

   docker images    


With docker images you can reference them by their Image_ID or their Repository name. You want to use the repository name so you can reference the image easily. 

You run your image in a container using the command: 

   docker run -it --rm --name centos-image centos    


Let's break down this command... 

docker run (runs a given image to make a container of it from its saved state) 

-it (puts the container in interactive terminal mode) 

--rm (when you exit the container it will shut down and be removed for you automatically) 

--name (names the container so you can reference it easily with using the container id) 

centos (the name of the image are making a container of) 


We can see the currently running docker images using the command: 

   docker ps   


Extra arguments you can add are: 

-l (to see the last container that was running) 

-a (to see all stopped containers) 

Making An Ansible Image

To make an Ansible image you need to setup a container and install Ansible inside it. I will be using CentOS 7 as my OS for this, but you can use any OS that can run Ansible. CentOS 7 has Python installed directly so if your OS doesn't have Python directly installed you will have to install it first.  

CentOS uses the package manager Yum to install packages. You will have to do some digging to find Ansible on your package manager. 


To install Ansible use the command: 

   yum install -y ansible    

To check that Ansible is installed on your container you can check the version. 

On CentOS you can use the command: 

   ansible --version    

To leave the terminal without stopping it we can use the keyboard shortcut: 

   Ctrl + p Then Ctrl + q    

You can jump right back in the container using the command: 

   docker attach containerNameOrID   

To make your container an image use the command: 

   docker commit containerNameOrID    

You docker images list will be updated and you can see the new image added but it doesn't have an easy to use name to reference. 

Let's change that by using the command: 

   docker tag imageID imageName   

Now you have an image with a Ansible installed that you can start a container from and build upon.

Docker Networking

Okay now that we have our Ansible environment created let's talk about how to setup networking between containers. Like a regular local network Docker container can be put into a subnet that operates on the local machine. You can perform SSH connections on these virtual environments and access them to perform scripts. 

This is where Ansible comes in, it does SSH connections to remote machines to perform programmatic scripts to provide changes and updates for machines without having to go one by one to each machine. 


Let's create our first Docker network by using the command: 

   docker network create networkName   

You can view the list of networks with the command: 

   docker network ls    

If you want to information on a specific network use the command: 

   docker network inspect networkName    

In order to run a container within a network we need to add an argument to the run command: 

   docker run –it –-network=networkName imageName    

Working With Ansible

In a running Ansible container you can make change your directory to where the Ansible files are located. In CentOS 7 you can find time in /etc/ansible. 

In the file (ansible.cfg) you can find all the settings for Ansible, but we will focus on one setting. Open the file with some command line editor. You can use vi or install something like nano. You can easily install an editor with the Yum package manager. 

In the config file we are looking for a line written as so: 

   #host_key_checking = False    

We just want to remove the # as it’s a comment so now the setting is active. Save the file and now Ansible will not check the SSH keys when connecting to the remote machines. 

Now we can look at the hosts file. Open the hosts file with an editor. At the top of the file we can input the IP addresses of the containers we are connecting to. 

Example line: 

   root@ ansible_ssh_pass=pass123 ansible_ssh_user=user    

The SSH password is not required if you're providing it in a command you are passing to the hosts. Save the hosts file and let ping the hosts we put. 

   ansible all -m ping    

A successful ping confirms we can make an SSH connection to the host/s. 

Anisble Playbooks

For an introduction to how Ansible issues scripts to other machines we are going to push PHP 7.2 to a machine/s with or without PHP 5.*. 

Ansible reads YAML files for playbooks. Let's create a playbook with the command: 

   touch php.yml   

Open the playbook with an editor. Copy the command structure below for this playbook. 

Playbooks require proper spacing to make them correctly you may need to fiddle around with them before they we be read correctly. 


Summary of commands: 

- hosts: (what hosts are going to be selected to be sent the scripts) 

task: (all your scripts you will be running go under here) 

- name (name of what task is being ran) 

yum: (when using a Yum command) 

    name: (requires install source) 

    state: (versioning) 


To run the playbook file, we can use the command:  

   ansible-playbook /etc/ansible/php.yml    

Ansible will SSH into the hosts, then being running the tasks in the playbook in order 

If everything is successful you can check the version of PHP in one of the running host containers  

with the command: 

   php -v    

You will see that the version will the latest version of PHP 7.2.