Ansible as we know is one of the best automation tools available now and docker is the industry leading container framework. In this article we will see how we can integrate ansible and docker. We will see how we use ansible to create docker containers.
Install the necessary packages for using docker with ansible. The docker-py package is required for playing with docker using ansible. To install this package we will use the python pip command as,
pip install docker-py
Docker Pull - Ansible does contain modules which help us in playing with docker. Lets create a ansible playbook for pulling a image
[root@manja17-I13330 docker-ansible]# cat docker-pull.yml
---
- hosts: localhost
tasks:
- name: Pull Ubuntu image
docker_image:
name: ubuntu
In the above playbook, we are trying to pull an ubuntu image using the ansible playbook. Run the playbook as below,
[root@manja17-I13330 docker-ansible]# ansible-playbook docker-pull.yml
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost] ***************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************
ok: [localhost]
TASK [Pull Ubuntu image] *******************************************************************************************************
changed: [localhost]
PLAY RECAP *********************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
Once the playbook ran successfully, we can see that the ubuntu image is
downloaded. This can be verified by the “docker images” command.
Build the Docker Image - Ansible provides a way to build docker images using the playbook. Below is the simple example of building a image using the ansible playbook,
[root@manja17-I13330 ansible-docker]# cat docker-build.yml
---
- hosts: localhost
tasks:
- name: Build Nginx image
docker_image:
path: .
name: my-nginx
tag: 0.1
In the above snippet , we are building a image from the Dockerfile available in the current location. Here is the Docker file that we are using
[root@manja17-I13330 ansible-docker]# cat Dockerfile
FROM ubuntu
MAINTAINER Jagadish Manchala <jagadish.manchala@gmail.com>
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install -y nginx
Once we run the command ,we will be having a nginx built with tag 0.1
Starting a Container - in order to start a container , we can use the below playbook as,
[root@manja17-I13330 docker-ansible]# cat docker-start-container.yml
---
- hosts: localhost
tasks:
- name: Create another Nginx container
docker_container:
name: my-nginx
image: my-nginx:0.1
ports:
- "18880:80"
env:
KEY: value
command: sleep infinity
In the above playbook, we are actually running the nginx container that we created earlier. In this we are starting the container and making sure a job “sleep infinity” is ran so that we have the container up and running.
[root@manja17-I13330 docker-ansible]# ansible-playbook docker-start-container.yml
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost] *************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************
ok: [localhost]
TASK [Create another Nginx container] ****************************************************************************************
changed: [localhost]
PLAY RECAP *******************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
Lets check the docker ps and see if our container is running,
[root@manja17-I13330 docker-ansible]# docker ps | grep nginx
e132f5acdcca my-nginx:0.1 "sleep infinity" 10 seconds ago Up 8 seconds 0.0.0.0:18880->80/tcp my-nginx
We can see that the container is up and running now. For the example yml files, Check the github repo here.
Install the necessary packages for using docker with ansible. The docker-py package is required for playing with docker using ansible. To install this package we will use the python pip command as,
pip install docker-py
Docker Pull - Ansible does contain modules which help us in playing with docker. Lets create a ansible playbook for pulling a image
[root@manja17-I13330 docker-ansible]# cat docker-pull.yml
---
- hosts: localhost
tasks:
- name: Pull Ubuntu image
docker_image:
name: ubuntu
In the above playbook, we are trying to pull an ubuntu image using the ansible playbook. Run the playbook as below,
[root@manja17-I13330 docker-ansible]# ansible-playbook docker-pull.yml
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost] ***************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************
ok: [localhost]
TASK [Pull Ubuntu image] *******************************************************************************************************
changed: [localhost]
PLAY RECAP *********************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
Once the playbook ran successfully, we can see that the ubuntu image is
downloaded. This can be verified by the “docker images” command.
Build the Docker Image - Ansible provides a way to build docker images using the playbook. Below is the simple example of building a image using the ansible playbook,
[root@manja17-I13330 ansible-docker]# cat docker-build.yml
---
- hosts: localhost
tasks:
- name: Build Nginx image
docker_image:
path: .
name: my-nginx
tag: 0.1
In the above snippet , we are building a image from the Dockerfile available in the current location. Here is the Docker file that we are using
[root@manja17-I13330 ansible-docker]# cat Dockerfile
FROM ubuntu
MAINTAINER Jagadish Manchala <jagadish.manchala@gmail.com>
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install -y nginx
Once we run the command ,we will be having a nginx built with tag 0.1
Starting a Container - in order to start a container , we can use the below playbook as,
[root@manja17-I13330 docker-ansible]# cat docker-start-container.yml
---
- hosts: localhost
tasks:
- name: Create another Nginx container
docker_container:
name: my-nginx
image: my-nginx:0.1
ports:
- "18880:80"
env:
KEY: value
command: sleep infinity
In the above playbook, we are actually running the nginx container that we created earlier. In this we are starting the container and making sure a job “sleep infinity” is ran so that we have the container up and running.
[root@manja17-I13330 docker-ansible]# ansible-playbook docker-start-container.yml
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost] *************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************
ok: [localhost]
TASK [Create another Nginx container] ****************************************************************************************
changed: [localhost]
PLAY RECAP *******************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
Lets check the docker ps and see if our container is running,
[root@manja17-I13330 docker-ansible]# docker ps | grep nginx
e132f5acdcca my-nginx:0.1 "sleep infinity" 10 seconds ago Up 8 seconds 0.0.0.0:18880->80/tcp my-nginx
We can see that the container is up and running now. For the example yml files, Check the github repo here.
No comments :
Post a Comment