Tip

Ansible vs. Docker: Differences and similarities explained

Ansible and Docker play specific roles, but when used together, Ansible's playbooks and Docker's Dockerfiles provide greater control and configurability over servers.

Ansible and Docker are popular tools for organizations looking to accelerate software releases via automation. While these tools couldn't be more different, they can work well together -- each accomplishing a different, important task for deploying and running software.

Docker enables you to run an application in a maintainable and repeatable manner. An admin can write a Dockerfile that stores all the necessary commands to set up an application, build a Docker image and run it on most systems.

With Ansible's automation capabilities, admins can easily provision and configure servers. Ansible is developed in Python and can run on most machines to install updates to dependencies, configure software or orchestrate running applications.

Let's explore Ansible and Docker to see how these tools differ and how they can work together.

What is Docker and what is it used for?

Docker is an open source containerization tool that takes advantage of OS-level virtualization to provide a platform for running applications in containers. Docker is used to run applications in self-contained environments defined by Dockerfiles.

How do Dockerfiles work?

If you work with a Docker image, then part of your application's environment is contained in a Dockerfile. A Dockerfile is written with commands to configure the application's running environment within the Docker container. Examples of simple commands in the Dockerfile include the COPY and ENTRYPOINT commands. The COPY command copies the application's executable file into the Docker container, while the ENTRYPOINT command runs the executable as the container's main process.

Since these configurations are captured in code, they can be kept in a version control repository. GitHub, for example, maintains a history of all the changes to the application's Dockerfile.

While these tools couldn't be more different, they can work well together -- each accomplishing a different, important task for deploying and running software.

Benefits of using Docker

Docker is a popular tool and offers many benefits, such as providing a reproducible way to run your application, regardless of what underlying system the container is running on. Because Docker containers behave the same, your application is much more portable than if you have it installed directly on a server.

Supported by all cloud providers, Docker is often the path of least resistance to deploying an application to the cloud. Advantages include the ability to quickly get new containers up and running, the portability of a Docker image and the benefits of turning an application's environment configuration into a Dockerfile.

A Docker container doesn't load an OS, which means it can start running in seconds. Applications can scale quickly as well -- simply spin up more containers using Docker deployment tools, such as AWS Fargate, Google Cloud Run or Azure Container Instances. It's also possible to switch an app between cloud providers or even host the app on-premises because Docker containers are easy to configure in cloud-hosted and local environments.

Common Docker use cases

Docker has many use cases, including the following:

  • Microservices architecture. Using Docker or another similar containerization tool is basically a prerequisite for running applications in a microservices architecture. From an operations perspective, it often makes sense to use larger compute instances for cost reasons rather than many small instances. There is a sweet spot between the largest and smallest possible instance where costs are optimized for the amount of compute an instance has. Without a containerization tool, it is difficult to coordinate many running applications on a single instance, but Docker unlocks several possibilities. Amazon Elastic Container Service, Docker Swarm, HashiCorp Nomad and Kubernetes are just a few of the tools that orchestrate the running of Docker containers. There are many more, and the list is certainly growing as most companies are using containerization in one way or another.
  • CI/CD. CI/CD also go hand in hand with Docker, as a Dockerfile provides a way to build a self-contained version of the application, which has an immutable tag attached to it by default. This artifact is referred to as an image and can easily be passed around for testing and deployment after being built.
  • DevOps. Docker enables developers to collaboratively create and maintain the application's running environment. This is critical to a team's DevOps efforts, which aim to break down the barrier between writing application code and running and operating an application.

What is Ansible and what is it used for?

Ansible automates processes, which can help organizations improve a wide range of efficiencies. For IT tasks, such as the management of developer machines, Ansible makes it possible to create and maintain the setup of a developer's machine in code.

How do Ansible playbooks work?

Ansible uses playbooks, which contain plays. A play is the basic unit of Ansible execution and contains variables, roles and an ordered list of tasks that can repeatedly run. For example, a play can include the necessary steps to install and configure a custom Secure Sockets Layer/Transport Layer Security certification and be executable on any machine SSH can access. A playbook could also provision a server to run an application.

Benefits of using Ansible

The benefit of using Ansible, much like Docker, is that containing configurations in code provides a working history of changes. Ansible enables organizations to keep their environment configuration stored in code, which increases maintainability and reliability. When new environment configuration changes are introduced, they can also go through a code review process. Ansible is also useful for eliminating manual processes and automating them.

Common Ansible use cases

Ansible has many use cases, including the following:

  • Configuration management. Organizations can define the desired state of their cloud infrastructure in a declarative way using Ansible playbooks. Ansible can then ensure the actual state of the infrastructure matches the desired state, making it easy to provision, configure and maintain servers and other IT resources.
  • Automated deployment. Ansible can automate the deployment of applications and services across multiple servers. This includes tasks like downloading software, performing database migrations and restarting services.
  • Provisioning. Ansible is also commonly used to provision new infrastructure, such as VMs, containers or cloud resources, and then configure them as needed. This helps to ensure consistency and repeatability in the deployment process.
  • Security and compliance. Ansible playbooks can be used to enforce security policies and ensure compliance across the IT infrastructure. This includes tasks like installing security updates, configuring firewalls and enforcing access controls.
  • Orchestration. Ansible enables teams to manage infrastructure and applications across on-premises, private cloud and public cloud environments. This helps to ensure consistency and portability of configurations and deployments, regardless of the platform running the software.

Ansible vs. Docker: Key differences and similarities

Ansible is an automation tool that doesn't run applications, but rather can perform automated processes to set up, deploy or maintain an application. Docker, on the other hand, is mainly used to run applications as a self-contained container.

Both tools capture configurations in code, which enable organizations to dynamically create, track and manage changes. This change history supports consistency, repeatability and collaboration in DevOps processes.

When to use Ansible and Docker together

It's possible to use Ansible with Docker to manage more infrastructure. Docker containers offer a way to efficiently build an application, but they require a service to handle their configuration, deployment and orchestration. Ansible is useful for this -- an Ansible playbook can install and configure Docker on the host machine, install a Docker image and run that image as a service.

Cost and customization are the advantages of controlling an application's Docker runtime setup and configuration. Other container tools, such as Fargate, are more costly than running a basic server on-premises. When you create your own Ansible playbooks, you also get fuller configurability.

Ansible has a huge advantage over provisioning machines by hand for organizations that don't run applications in the cloud. Using cloud-based tools, such as Fargate, can be compelling, but Ansible and Docker can provide a company with complete control of the server on which their application runs.

Matt Grasberger is a DevOps engineer with experience in test automation, software development and designing automated processes to reduce work.

Next Steps

Use multiple Dockerfiles for complex application configuration

Dig Deeper on Software design and development