VOOZH about

URL: https://thenewstack.io/how-to-deploy-a-container-using-ansible/

⇱ How to Deploy a Container Using Ansible - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2019-12-27 06:00:00
How to Deploy a Container Using Ansible
tutorial,
Containers

How to Deploy a Container Using Ansible

How to use Ansible to manage conatiners.
Dec 27th, 2019 6:00am by Jack Wallen
👁 Featued image for: How to Deploy a Container Using Ansible
Feature image by Bec T from Pixabay.

Containers. You cannot escape them, as they have continued to march forward in their takeover of enterprise IT rollouts. You deploy them as apps, as services, and more. And, the method by which you deploy containers is as varied as the reasons why you deploy them:

  • Docker
  • Kubernetes
  • Rancher
  • Solaris Containers
  • Rocket
  • MicroK8s
  • Tectonic

The list goes on and on. But did you know there’s another way to deploy containers, one that might have remote administrators anxious to give a go? That alternative method is via Red Hat’s open source automation tool, Ansible.

For those that don’t know, Ansible is an open-source software provisioning, configuration management, and application-deployment tool that runs on Linux. Ansible includes its own declarative language and uses playbooks that create the environment for running simple or incredibly complex commands (or groups of commands) on a remote machine.

And of course, Ansible can be used to deploy containers.

Now, before you dive too deep into this, what I’m going to show you isn’t necessarily a best practices scenario. Because everyone’s landscape and needs vary, what you’re going to see here is simply an introduction to using Ansible for the deployment of containers. Are there better methods of doing so? Certainly. But will this give you a launching point that will allow you to craft highly flexible and viable Ansible playbooks? It definitely will.

In the meantime, let’s see how this is done.

What You’ll Need

In order to make this work, you’ll need at least two servers, one of which has Ansible up and running. I’ll be demonstrating with two instances of Ubuntu Server 18.04. You’ll also need a user account with sudo privileges. Finally, you’ll need to have SSH key authentication setup between the servers. Without SSH key authentication, this will not work.

And that’s all you’ll need.

Install Ansible

If you don’t already have Ansible running on one of your servers, follow these steps to install it:

  1. Log into the Ubuntu Server that will host Ansible
  2. Install the necessary repository with the command sudo apt-add-repository ppa:ansible/ansible.
  3. Update apt with the command sudo apt-get update.
  4. Install Ansible with the command sudo apt-get install ansible -y.
  5. If necessary, install a Python interpreter with the command sudo apt-get install python -y.

Copy your SSH Key

In order to make this work, you must have SSH key authentication setup. From your Ansible server, create your SSH key with the command:

ssh-keygen

Once you have the key generated, copy it to the remote machine with the command:

ssh-copy-id SERVER_IP

Where SERVER_IP is the IP address of the remote server.

Install Docker

Next you must install the docker engine on both (or all) machines. To install the docker engine, log into one of your servers and issue the command:

sudo apt-get install docker.io python3-docker -y

Once the installation is complete, start and enable the docker engine with the commands:

sudo systemctl start docker
sudo systemctl enable docker

Finally, add your user to the docker group with the command:

sudo usermod -aG docker $USER

In order for the changes to take effect, log out and log back in.

Once you’ve taken care of these steps on all machines, you’re ready to move on.

Directory Structure

We’re now going to create the necessary directory and files. This will be done on the machine running Ansible. Log into that machine and issue the command:

mkdir ~/docker_project

Change into that newly created directory with the command:

cd ~/docker_project

Now we’re going to create our hosts file. Do this with the command:

nano hosts

In that file, paste the following contents:

[webserver]
SERVER_IP

[webserver:vars]
ansible_python_interpreter=/usr/bin/python3

Where SERVER_IP is the IP address of the remote server. If you have more than one server, that file would look like:

[webserver]
SERVER_IP
SERVER2_IP

[webserver:vars]
ansible_python_interpreter=/usr/bin/python3

Save and close the file.

We’ll now create our Ansible playbook. This playbook will do the following:

  • Install aptitude.
  • Install a few dependencies.
  • Add a docker repository.
  • Install the docker community edition.
  • Install the docker Python module.
  • Pull the official Ubuntu image.
  • Create four containers based on the newly-pulled Ubuntu image.

This new file will be in the standard YAML format, which means you must pay close attention to your line indenting. Inconsistent indenting will cause your playbook to fail. Create a new file with the command:

nano ubuntu_playbook.yml

Our playbook looks like this:

- hosts: all   become: true   vars:     create_containers: 4     default_container_name: docker     default_container_image: ubuntu     default_container_command: sleep 1d   tasks:     - name: Install aptitude using apt       apt: name=aptitude state=latest update_cache=yes force_apt_get=yes     - name: Install required system packages       apt: name={{ item }} state=latest update_cache=yes       loop: [ 'apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common', 'python3-pip', 'virtualenv', 'python3-setuptools']     - name: Add Docker GPG apt Key       apt_key:         url: https://download.docker.com/linux/ubuntu/gpg         state: present     - name: Add Docker Repository       apt_repository:         repo: deb https://download.docker.com/linux/ubuntu xenial stable         state: present     - name: Update apt and install docker-ce       apt: update_cache=yes name=docker-ce state=latest     - name: Install Docker Module for Python       pip:         name: docker     - name: Pull default Docker image       docker_image:         name: "{{ default_container_image }}"         source: pull     - name: Create default containers       docker_container:         name: "{{ default_container_name }}{{ item }}"         image: "{{ default_container_image }}"         command: "{{ default_container_command }}"         state: present       with_sequence: count={{ create_containers }}

Save and close the file. Obviously, you can modify the playbook to fit your needs, so go through it carefully and alter it accordingly.

As I said, there are a multitude of ways to handle the ascribed task. What you see above is only one of them. However, it works and is a great way to see how to deploy containers with Ansible.

Running the Playbook

With our playbook in place, we can now run it with the following command:

ansible-playbook -i hosts ubuntu_playbook.yml --ask-become-pass

The –ask-become-pass option instructs Ansible to require the sudo password for the remote user SSH key (Figure 1). Once you type that password, hit Enter on your keyboard and the playbook will run.

👁 Image

Figure 1: Type the SSH key and the playbook will run.

Depending on your network connection and the speed of your machines, the playbook could take a few minutes to complete. Once it finishes, log onto your remote server and issue the command:

docker ps -a

You should see that the new containers have been deployed (Figure 2).

👁 Image

Figure 2. Created with GIMP

And that’s all there is to deploying containers with Ansible. Are there better ways to do this? Certainly. But if you’re already invested in Ansible, why not add the deployment of containers to your growing list of playbooks.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
Red Hat is a sponsor of The New Stack.
TNS owner Insight Partners is an investor in: Docker.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.