Learn the way to deploy a Docker container utilizing an Ansible playbook.
There are such a lot of methods to deploy containers. You need to use Docker, Kubernetes, MicroK8s, AWS, Google Cloud Platform…. the record goes on and on. However in the event you’re an Ansible admin, you may have one more methodology. That is proper, with an Ansible playbook, you’ll be able to deploy a Docker container.
And that, my pals, is precisely what I’ll present you do. I will be demonstrating with solely two servers, each of which can run Ubuntu Server 18.04. You’ll be able to take a look at this with as many servers as you want, however I want to maintain it easy at this degree.
SEE: Hybrid cloud: A information for IT professionals (TechRepublic obtain)
What you may want
To make this work, you may want the next:
- Not less than two servers (certainly one of which has Ansible), put in and operating
- A person with sudo privileges
It’s important that you just first undergo theinformation, in any other case this is not going to work. And be sure to comply with the steps for establishing SSH key authentication.
Methods to set up Docker
As a result of we will be deploying a Docker container, we’d like Docker put in on each machine. This may be executed by opening a terminal window and issuing the next command:
sudo apt-get set up docker.io python3-docker -y
As soon as the set up is full, begin and allow the docker service with the instructions:
sudo systemctl begin docker sudo systemctl allow docker
Lastly, add your person to the docker group with the command:
sudo usermod -aG docker $USER
Sign off and log again in so the modifications will take impact.
Methods to create the listing construction and recordsdata
On the Ansible server, create a brand new listing with the next command:
Become the docker_project listing with the command cd docker_project and create a hosts file with the command:
In that file, add the next contents:
[webserver] SERVER_IP [webserver:vars] ansible_python_interpreter=/usr/bin/python3
The place SERVER_IP is the IP deal with of the opposite server(s). Save and shut the file.
We’ll create a playbook that may do the next:
- Set up aptitude
- Set up quite a lot of dependencies
- Add a docker repository
- Set up docker-ce
- Set up the docker Python module
- Pull the official Ubuntu picture
- Create 4 containers based mostly on the Ubuntu picture
Situation the command nano ubuntu_playbook.yml. In that new file, paste the next (ensuring the formatting follows the usual YAML format):
--- - hosts: all grow to be: true vars: create_containers: four default_container_name: docker default_container_image: ubuntu default_container_command: sleep 1d duties: - title: Set up aptitude utilizing apt apt: title=aptitude state=newest update_cache=sure force_apt_get=sure - title: Set up required system packages apt: title= state=newest update_cache=sure loop: [ 'apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common', 'python3-pip', 'virtualenv', 'python3-setuptools'] - title: Add Docker GPG apt Key apt_key: url: https://obtain.docker.com/linux/ubuntu/gpg state: current - title: Add Docker Repository apt_repository: repo: deb https://obtain.docker.com/linux/ubuntu xenial steady state: current - title: Replace apt and set up docker-ce apt: update_cache=sure title=docker-ce state=newest - title: Set up Docker Module for Python pip: title: docker - title: Pull default Docker picture docker_image: title: "" supply: pull - title: Create default containers docker_container: title: "" picture: "" command: "" state: current with_sequence: depend=
Save and shut the file. After you have a really feel for the way this playbook works, you too can modify it to suit your wants. Contemplate this a stable template for which to deploy containers from Ansible.
Methods to run the Ansible playbook
We will now run our Ansible playbook. To do that, difficulty the command:
ansible-playbook -i hosts ubuntu_playbook.yml --ask-become-pass
You may be prompted for the sudo password for the distant person. Sort that and hit Enter. The playbook ought to then execute with out fail. When the playbook completes, it’s best to see the output signifies success (Determine A).
If you happen to go over to certainly one of your distant servers and difficulty the command docker ps -a, it’s best to see all 4 ubuntu containers operating (Determine B).
And that is it. You now know deploy a Docker container by means of an Ansible playbook. That is actually not the best route, however it does make it doable so that you can get actually inventive with each your containers and Ansible.