Docker image vs container: What are the differences? – CircleCI

The Docker containerization platform helps you easily build, deploy, and run applications using containers. If you’re new to Docker, you may be wondering how a Docker image differs from a Docker container. Although Docker images and containers serve a similar purpose, they have different uses. An image is a snapshot of an environment and a container runs the software. Think of a container as a shipping container for software: it contains important content such as files and programs so that an application can be delivered efficiently from producer to consumer.

It may seem like everyone has a different idea of what Docker terms mean, and sometimes the terms are used interchangeably. This article will explore the differences between Docker images and containers, to help you understand how and when to use each.

Docker

Overview

Solutions like Docker

, Flatpak, and Snaps have the same goal of packaging an application into a single package to install on any Linux distribution

.

Solomon Hykes introduced Docker as an open source project in 2013. Its first commercial version was ready for production in 2014.

One of

the biggest benefits of containerization is that it allows developers to package their applications with all the dependencies needed to run on any Linux distribution. This eliminates the need to install each dependency manually.

Multiple containers can run simultaneously, each based on the same image or different images. Docker is similar to virtual machines in the way it creates multiple instances of an operating system. However, Docker allows you to create containers that run on the same operating system. Therefore, more containers than virtual machines can run on a given hardware combination.

Docker containers can even run inside virtual machines. Docker provides an additional layer of abstraction and automation instead of creating a virtual machine, making it easier to use.

The popularity of the containerization platform has increased among developers and system administrators because it encompasses the entire file system of the application with all its dependencies. This configuration enables an immutable infrastructure and ensures that deployments are idempotent: they will remain exactly the same no matter how many times you repeat the operation.

A Docker daemon runs in the background to manage images, containers, and more. A client and the daemon communicate using sockets or through a RESTful API.

What is a Docker image?

Images are read-only templates that contain instructions for creating a container. A Docker image creates containers to run on the Docker platform.

Think of an image

as a blueprint or snapshot of what will be in a container when it runs

.

An image is made up of several stacked layers, like layers in a photo editor, each of which changes something in the environment. Images contain the code or binary, runtimes, dependencies, and other file system objects for running an application. The image is based on the kernel of the host operating system (OS).

For example, to create a web server image, start with an image that includes Ubuntu Linux (a base operating system). Then, add packages like Apache and PHP on top.

You can create images manually using a Dockerfile, a text document that contains all the commands for creating a Docker image. You can also pull images from a central repository called a registry, or from repositories such as Docker Hub using the docker pull [name] command.

When a Docker user runs an image, it becomes one or more container instances. The initial state of the container can be whatever the developer wants: it can have a web server installed and configured, or nothing more than a bash shell running as root. In practice, however, most images include some preconfigured software and configuration files.

Docker images are immutable, so you can’t change them once they’re created. If you need to change something, create another container with your changes and save them as another image. Or simply run your new container using an existing image as a base and change it.

After successfully creating an application, Docker can export even more images to other images. Images derived from each other are usually referred to as main and child images.

An image can have multiple tags, but each tag is unique. Tags distinguish images, such as ubuntu:latest or ubuntu:14.04, for example.

The images themselves do not run, but you can create and run containers from a Docker image.

What is a container?

A container is an isolated place where an application runs without affecting the rest of the system and without the system affecting the application. Because they are isolated, containers are suitable for running software securely such as databases or web applications that need access to sensitive resources without giving access to all users of the system.

Since the

container runs natively on Linux and shares the host machine’s kernel, it is lightweight and uses no more memory than other executables. If you stop a container, it won’t automatically restart unless you configure it that way. However, containers can be much more efficient than virtual machines because they don’t need the overhead of a full operating system. They share a single kernel with other containers and boot in seconds instead of minutes.

You can use containers to package an application with all the components you need and then ship it as a single unit. This approach is popular because it eliminates friction between development, QA, and production environments, allowing for faster software submission. Building and deploying applications within software containers eliminates “jobs on my machine” issues when collaborating on code with other developers.

Applications can also run on any infrastructure and on any cloud. You can isolate applications and their underlying infrastructure from other applications.

Docker images

vs. containers

A Docker image

runs code in a Docker container. Add a core functionality writable layer in a Docker image to create a running container.

Think of a Docker container as a running image instance. You can create many containers from the same image, each with its own unique data and state.

Although images are not the only way to create containers, they are a common method.

A key benefit of adopting containers is the standardization and simplification of development, operations, and testing. However, for teams to get the most out of containers, they must ensure that developers, operations engineers, and testers create consistent environments.

A continuous integration and continuous deployment (CI/CD) pipeline can create, test, and package containers. The deployment then distributes that container to a runtime environment where it can run as part of an application.

A continuous integration solution like CircleCI allows developers to automate builds, tests, and deployments. CircleCI can use Docker containers to make it easier to deploy your applications across multiple environments.

For example, CircleCI can create Docker images and send them to a container image registry such as Docker Hub. From there, you can instantiate the images in containers in Kubernetes, OpenShift, or anywhere else.

The flow works like this:

  1. You commit changes to your
  2. Git repository.

  3. This commit triggers a CircleCI build job that pulls the source code from Git and runs
  4. unit tests on the code. If unit tests pass, CircleCI

  5. sends the compiled image to Docker Hub. If unit tests fail, CircleCI
  6. alerts the developer and stops the workflow.

Features like Docker layer caching and test splitting can also help you build and test your images faster, shortening deployment time. To learn more about the benefits of using a continuous integration platform to orchestrate Docker builds, tests, and deployments, visit How to Create a CI/CD Pipeline with Docker.

Both

containers and images allow users to specify application dependencies and configurations and describe everything needed for a machine to run that application. However, containers and images have different lifecycles. For example, you can use containers, but not images, in container-based systems such as Pivotal Cloud Foundry. Similarly, you can use images, but not containers, on non-container systems like Heroku or OpenShift.

It’s not about choosing containers or images. They trust each other, and you need both to work with Docker.

Now that you understand the nuances between images and Docker containers, you can take full advantage of the Docker platform. By working with Docker, automation helps you integrate quickly and free up developer time to create new application features. Learn more about how CircleCI’s Docker and Kubernetes integrations can improve the efficiency of your software development process.