It's just an ansible playbook and that the playbook installs docker and deploys a nodejs container through that playbook, all that plays are using with inbuilt ansible modules.
- Easy to deploy a container with the help of an ansible-playbook
- All these plays are using inbuilt ansible modules
- Docker installation and container spin up through the same playbook
- Install Ansible on your Master Machine
- Basic Knowledge of ansible
Ansible2 (For your reference visit How to install Ansible)
amazon-linux-extras install -y ansbile2
yum install -y git
git clone https://github.com/yousafkhamza/docker-deployment-from-playbook.git
cd docker-deployment-from-playbook
ansible -i hosts -m ping all <--------- Establish/Check SSH connection via ansible
ansible-playbook -i hosts dockerinstallation.yml <------------- playbook running script
PLAY [Installing docker through ansible] ***********************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************
ok: [172.31.10.48]
TASK [PIP Installation] ****************************************************************************************************************************
changed: [172.31.10.48]
TASK [Depandancy Task Install dokcer-py] ***********************************************************************************************************
changed: [172.31.10.48]
TASK [Docker Installtion] **************************************************************************************************************************
changed: [172.31.10.48]
TASK [Docker Service start and Enable] *************************************************************************************************************
changed: [172.31.10.48]
TASK [Docker image from docker hub] ****************************************************************************************************************
changed: [172.31.10.48]
TASK [Create a container from image] ***************************************************************************************************************
changed: [172.31.10.48]
PLAY RECAP *****************************************************************************************************************************************
172.31.10.48 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ip-172-31-10-48]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
yousafkhamza/node latest 39e4935f129b 6 weeks ago 50.8MB
[root@ip-172-31-10-48]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
79867e5504e3 yousafkhamza/node "node app.js" 8 minutes ago Up 7 minutes 0.0.0.0:80->3000/tcp ansible_nodejs
vim dockerinstallation.yml
---
- name: 'Installing docker through ansible'
hosts: all
become: true
vars:
container_name: "ansible_nodejs"
container_image: "yousafkhamza/node"
container_port: "80:3000"
tasks:
- name: "PIP Installation"
yum:
name: pip
state: present
- name: "Depandancy Task Install dokcer-py"
pip:
name: docker-py
state: present
- name: "Docker Installtion"
yum:
name:
- docker
state: present
- name: "Docker Service start and Enable"
service:
name: docker
state: restarted
enabled: true
- name: "Docker image from docker hub"
docker_image:
name: "{{ container_image }}"
source: pull
- name: "Create a container from image"
docker_container:
name: "{{ container_name }}"
image: "{{ container_image }}"
ports: "{{ container_port }}"
state: started
vim hosts
[slave]
172.31.10.48 ansible_user="ec2-user" ansible_port=22 ansible_ssh_private_key_file="/root/abcd.pem"
Please note that you may use your slave server's details also please put your key absolute path and avoid unnecessary issues
On this playbook was I used my old nodjs container image so please be check the same. I have already explained how we create an image and container from the docker file.
It's a simple Ansible playbook how we pulled an image from docker hub and then how to spin up a container from that image through ansible. I just uploaded the same for educational purposes and this playbook we used inbuilt modules so it is should more helpful for freshers who started ansible and docker.