{"id":18947640,"url":"https://github.com/yousafkhamza/docker-deployment-from-playbook","last_synced_at":"2025-04-15T23:30:29.311Z","repository":{"id":195965545,"uuid":"389015641","full_name":"yousafkhamza/docker-deployment-from-playbook","owner":"yousafkhamza","description":"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.","archived":false,"fork":false,"pushed_at":"2021-07-24T07:10:56.000Z","size":11,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2023-09-20T19:52:41.721Z","etag":null,"topics":["ansible-playbook","containerization","deployment","docker","nodejs"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yousafkhamza.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-07-24T06:01:01.000Z","updated_at":"2023-09-20T19:52:45.832Z","dependencies_parsed_at":"2023-09-20T20:13:36.849Z","dependency_job_id":null,"html_url":"https://github.com/yousafkhamza/docker-deployment-from-playbook","commit_stats":null,"previous_names":["yousafkhamza/docker-deployment-from-playbook"],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2Fdocker-deployment-from-playbook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2Fdocker-deployment-from-playbook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2Fdocker-deployment-from-playbook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2Fdocker-deployment-from-playbook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yousafkhamza","download_url":"https://codeload.github.com/yousafkhamza/docker-deployment-from-playbook/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223688710,"owners_count":17186298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ansible-playbook","containerization","deployment","docker","nodejs"],"created_at":"2024-11-08T13:10:49.116Z","updated_at":"2024-11-08T13:10:52.485Z","avatar_url":"https://github.com/yousafkhamza.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Docker Deployment from an Ansible Playbook\n[![Build](https://travis-ci.org/joemccann/dillinger.svg?branch=master)](https://travis-ci.org/joemccann/dillinger)\n\n---\n## Description\nIt'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.\n\n----\n## Feature\n- Easy to deploy a container with the help of an ansible-playbook\n- All these plays are using inbuilt ansible modules \n- Docker installation and container spin up through the same playbook\n\n----\n## Pre-Requests\n- Install Ansible on your Master Machine\n- Basic Knowledge of ansible\n\n##### Installation\n[Ansible2](https://docs.ansible.com/ansible/2.3/index.html) (For your reference visit [How to install Ansible](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html))\n\n### Packages \n```sh\namazon-linux-extras install -y ansbile2\nyum install -y git\n```\n\n##### Ansible Modules used\n- [yum](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_module.html) \n- [pip](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/pip_module.html)\n- [service](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html)\n- [docker_image](https://docs.ansible.com/ansible/2.8/modules/docker_image_module.html)\n- [docker_container](https://docs.ansible.com/ansible/2.5/modules/docker_container_module.html)\n\n----\n## How to use\n```sh\ngit clone https://github.com/yousafkhamza/docker-deployment-from-playbook.git\ncd docker-deployment-from-playbook\n```\n```sh\nansible -i hosts -m ping all                 \u003c--------- Establish/Check SSH connection via ansible\nansible-playbook -i hosts dockerinstallation.yml           \u003c------------- playbook running script\n```\n\n----\n## Output be like\n```sh\nPLAY [Installing docker through ansible] ***********************************************************************************************************\n\nTASK [Gathering Facts] *****************************************************************************************************************************\nok: [172.31.10.48]\n\nTASK [PIP Installation] ****************************************************************************************************************************\nchanged: [172.31.10.48]\n\nTASK [Depandancy Task Install dokcer-py] ***********************************************************************************************************\nchanged: [172.31.10.48]\n\nTASK [Docker Installtion] **************************************************************************************************************************\nchanged: [172.31.10.48]\n\nTASK [Docker Service start and Enable] *************************************************************************************************************\nchanged: [172.31.10.48]\n\nTASK [Docker image from docker hub] ****************************************************************************************************************\nchanged: [172.31.10.48]\n\nTASK [Create a container from image] ***************************************************************************************************************\nchanged: [172.31.10.48]\n\nPLAY RECAP *****************************************************************************************************************************************\n172.31.10.48               : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0\n```\n## Point of view on Slave\n```sh\n[root@ip-172-31-10-48]# docker image ls\nREPOSITORY          TAG       IMAGE ID       CREATED       SIZE\nyousafkhamza/node   latest    39e4935f129b   6 weeks ago   50.8MB\n[root@ip-172-31-10-48]# docker container ls\nCONTAINER ID   IMAGE               COMMAND         CREATED         STATUS         PORTS                  NAMES\n79867e5504e3   yousafkhamza/node   \"node app.js\"   8 minutes ago   Up 7 minutes   0.0.0.0:80-\u003e3000/tcp   ansible_nodejs\n```\n\n----\n## Behind the code\n_vim dockerinstallation.yml_\n```sh\n---\n- name: 'Installing docker through ansible'\n  hosts: all\n  become: true\n  vars:\n    container_name: \"ansible_nodejs\"\n    container_image: \"yousafkhamza/node\"\n    container_port: \"80:3000\"\n  tasks:\n  \n    - name: \"PIP Installation\"\n      yum:\n        name: pip\n        state: present\n        \n    - name: \"Depandancy Task Install dokcer-py\"\n      pip:\n        name: docker-py\n        state: present\n\n    - name: \"Docker Installtion\"\n      yum:\n        name:\n          - docker\n        state: present\n\n    - name: \"Docker Service start and Enable\"\n      service:\n        name: docker\n        state: restarted\n        enabled: true\n\n    - name: \"Docker image from docker hub\"\n      docker_image:\n       name: \"{{ container_image }}\"\n       source: pull\n\n    - name: \"Create a container from image\"\n      docker_container:\n       name: \"{{ container_name }}\"\n       image: \"{{ container_image }}\"\n       ports: \"{{ container_port }}\"\n       state: started\n```\n_vim hosts_\n```sh\n[slave]\n172.31.10.48 ansible_user=\"ec2-user\" ansible_port=22 ansible_ssh_private_key_file=\"/root/abcd.pem\"\n```\n\u003e Please note that you may use your slave server's details also please put your key absolute path and avoid unnecessary issues\n\n### Output Screenshot\n![alt_txt](https://i.ibb.co/x8DXspP/Screenshot-10.png)\n----\n## Reference\n\nOn this playbook was I used my old [nodjs](https://github.com/yousafkhamza/nodejs-dockerfile) container image so please be check the same. I have already explained how we create an image and container from the docker file. \n\n----\n## Conclusion\nIt'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.\n\n### ⚙️ Connect with Me \n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"mailto:yousaf.k.hamza@gmail.com\"\u003e\u003cimg src=\"https://img.shields.io/badge/Gmail-D14836?style=for-the-badge\u0026logo=gmail\u0026logoColor=white\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://www.linkedin.com/in/yousafkhamza\"\u003e\u003cimg src=\"https://img.shields.io/badge/LinkedIn-0077B5?style=for-the-badge\u0026logo=linkedin\u0026logoColor=white\"/\u003e\u003c/a\u003e \n\u003ca href=\"https://www.instagram.com/yousafkhamza\"\u003e\u003cimg src=\"https://img.shields.io/badge/Instagram-E4405F?style=for-the-badge\u0026logo=instagram\u0026logoColor=white\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://wa.me/%2B917736720639?text=This%20message%20from%20GitHub.\"\u003e\u003cimg src=\"https://img.shields.io/badge/WhatsApp-25D366?style=for-the-badge\u0026logo=whatsapp\u0026logoColor=white\"/\u003e\u003c/a\u003e\u003cbr /\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyousafkhamza%2Fdocker-deployment-from-playbook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyousafkhamza%2Fdocker-deployment-from-playbook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyousafkhamza%2Fdocker-deployment-from-playbook/lists"}