what is docker compose used for

Understanding Docker: What is Docker Container? What is a Docker file? What is docker compose used for

Docker has become an essential tool for developers and DevOps/SRE engineers, making it simple to develop, test, and deploy applications.

Since its emergence, Docker has been gaining more and more space on servers around the world. Its easy-to-understand and simple-to-apply features allow it to be used in a variety of scenarios.

In this article, we will better understand what Docker is, how it works, what is docker compose used for, and why it is so valuable for managing development environments, and being an essential tool for DevOps/SRE teams, among others.

what is docker compose used for

The problem

Traditionally, setting up and maintaining consistent development environments was a significant challenge. Problems like “it works on my machine but not on the server” were common due to differences in development and production environments.

As the complexity of modern applications increases, these inconsistencies have become even more problematic, resulting in longer development cycles, increased bug resolution times, and general frustration among developers.

Solution

Docker solves these problems by allowing developers to package applications and their dependencies into containers. These containers can run in any environment that supports Docker, ensuring consistency and reliability.

Why Docker?

Case example

Imagine you are a Python developer working on multiple projects. The first project is a web application using MySQL and Python/Django, while the second deals with requests and post automation using REST API, using DeepTranslator and requests library.

Developing on the same machine, you know that dependencies from one project cannot interfere with the other. To solve this problem, you create virtual environments within Python. However, dependency installation issues often arise when distributing projects due to different operating system versions.

This is where Docker comes in. With Docker, you create images that contain your entire project, ensuring that it works on any platform or operating system, eliminating the famous “it was working on my machine” problem.

The main advantages of using Docker

Accelerated development cycles

Utilizing Docker, developers can rapidly launch containers in mere moments, enabling swift deployment. This fast response time facilitates continuous integration and continuous delivery (CI/CD) practices, allowing teams to iterate and deliver features and fixes faster.

Improved collaboration

Because Docker containers are consistent across environments, developers, testers, and operations teams can work in sync, a fundamental principle of agile development and DevOps. Everyone works with the same container configuration, which reduces discrepancies and misunderstandings.

Reduced infrastructure costs

Through efficient resource management and the capability to run numerous containers on a solitary host, Docker diminishes the necessity for multiple servers or virtual machines. This consolidation not only results in significant savings on hardware and maintenance costs but also streamlines operational efficiency.

Repeatable deployment processes

Docker containers derive from images that are versioned and securely stored within a registry system. This means that every time you deploy a container, you use an identical environment configuration. This repeatability ensures that the deployment process is simple, less error-prone, and easily automated, increasing the reliability of deployments.

Docker What is it? 🐳

In short, Docker is an Open Source containerization platform that facilitates the packaging, distribution, and execution of applications in environments based on Unix/Linux containers.

It is a platform as a service (PaaS) that uses virtualization at the operating system level to offer software in packages called containers.
Containers operate in isolation, containing their own set of software, libraries, and configurations, while interacting with each other through clearly defined channels. All containers are run by a single operating system kernel, which makes them more resource-efficient compared to virtual machines.

Virtualization

To advance in your studies on Docker, it is essential to know how virtualization works. There are two main ways to create isolated virtual environments: based on containers or based on hypervisors.

Hypervisor-based virtualization: Involves generating a virtual representation of hardware, referred to as a virtual machine (VM), managed and orchestrated by a hypervisor.

image

Container-based virtualization: It runs at the operating system level, creating isolated environments or containers that share the base system’s kernel. These containers are used to run specific processes and applications, making them lighter and more efficient than virtual machines.

image 2

Comparison between Docker and Traditional Virtualization

Resource Efficiency:

  • Containers consume fewer resources compared to virtual machines because they share the OS kernel.
  • Unlike virtual machines that require a complete operating system, Docker containers share the host OS, resulting in significantly lower resource consumption overall.
  • This efficient use of system resources means you can run multiple containers on a single host, maximizing resource utilization and reducing costs.

Isolation:

  • Containers isolate applications from each other but share the kernel, whereas virtual machines isolate completely, including the operating system.

Portability:

  • Docker facilitates the portability of applications across different environments.
  • There are no concerns about dependencies or underlying operating systems.

With Docker, you can simplify application development and deployment, ensuring consistency and efficiency across different platforms.

Docker terms and technologies

To use Docker effectively, and understand what is Docker Container, what is a Dockerfile, what is docker compose used for, it is essential to be familiar with its main terminologies and associated tools.

WordPress Table

Understanding these terms and how they relate to each other is critical to leveraging the full potential of Docker in application development and deployment.

Likewise, becoming familiar with the Docker ecosystem of tools can significantly improve the development, deployment, and management of containerized applications.

WordPress Table

This table summarizes the main tools and technologies associated with Docker.

What is Docker container?

A Docker container represents the active instance of a working image. When we download an image from Docker Hub, it is a read-only file. We can run several containers using the same image, each in its isolated environment, which ensures that the execution of applications does not interfere with the local system. This format increases application security.

To create a container from an image, we use the docker run command. Each container can be in different states, such as restarting, running, paused, terminated, or inactive. The docker ps command lists all running containers, while docker ps -a shows both running and stopped containers.

Furthermore, we can run multiple instances of the same application using the same image. Each container will have a unique ID, but they will all be based on the same image. This practice is advantageous, for example, to ensure continuous application availability even if a container fails. Each time Docker creates a container from an image, it adds a thin layer of writing over the original image, allowing for changes during the container’s runtime.

Key points to remember:

  • Containers are active instances derived from an image.
  • Multiple containers can be instantiated from the same image.
  • Each container functions within its own isolated environment, ensuring no interference with other system applications.
  • To initiate a container from an image, utilize the docker run command.
  • Every container possesses a writable layer that permits runtime modifications.

So, what is Docker Container? In short, a Docker container is an autonomous and isolated application unit that can represent anything from a database to an NGINX server/reverse proxy, among other elements, making its deployment and execution simple in different cases.

What is a Dockerfile and why does it hold significant importance?

When developing your application, creating a Dockerfile is essential. This text document contains some vital commands that, when executed, quickly mount a Docker image. The Docker daemon, commonly called “Docker”, is the background service that manages the Docker containers on a system, processing all instructions from top to bottom in the Dockerfile.

The Dockerfile plays a crucial role in contemporary software development and deployment strategies. It serves as a blueprint for constructing Docker images, defining precisely how applications should be configured, built, and executed within containers. This standardized approach ensures consistency across environments and enhances reproducibility and scalability, making Dockerfile an indispensable tool for streamlining the software development lifecycle.

The Dockerfile structure

Now that you know what is a Dockerfile, let’s explore its structure. The Dockerfile is made up of instructions that are executed in the order they appear. Among the main instructions are:

  • FROM: It is mandatory, it defines the base image to be used.
  • RUN: Executes commands during the build to mount the image.
  • CMD: Executed when creating the container, it should only appear once.
  • EXPOSE: Defines the ports to be released in the container.
  • COPY: Copies local files and folders to the image.
  • ADD: Copies files and folders (remote or compressed).
  • VOLUME: Specifies a mount point shared between container and host.
  • WORKDIR: Specifies the working directory that the container will use.
  • USER: Defines the user to execute commands.
  • BUILD: Builds the image from the DockerFile.

How to build a Dockerfile

The build process involves three steps:

  1. Create the DockerFile.
  2. Generate the image.
  3. Build the container.

In the first step, build your Dockerfile with the container configuration instructions, preferring official images to ensure quality, security and efficiency. See a simple example using Ubuntu as a base:

# Defining the base image to be used
FROM ubuntu:20.04

# Maintainer information (optional)
LABEL maintainer="your_name <your_email@email.com>"

# Update packages and install dependencies
RUN apt-get update \
    && apt-get install -y \
       software-properties-common \
       python3 \
       python3-pip \
    && rm -rf /var/lib/apt/lists/*

# Specifies the working directory that the container will use
WORKDIR /app

# Copies files from the local directory to the working directory in the container
COPY . .

# Installs the Python dependencies defined in requirements.txt
RUN pip3 install --no-cache-dir -r requirements.txt

# Exposes port 8000 for external access
EXPOSE 8000

# This is a standard command that the container executes when starting
CMD ["python3", "app.py"]


With the Dockerfile created, generate the image with:

docker build -t nome_da_imagem .

Then, create the container from this image:

docker run nome_da_imagem

Best practices when creating DockerFile

To create efficient Dockerfiles, follow these tips:

  • Pay attention to the order of the instructions.
  • Containers must be immutable.
  • Consolidate instructions to minimize layers.
  • Install only necessary packages.
  • Use the .dockerignore file.
  • Declare elements subject to change at the end of the file.
  • Use as few instructions as possible.

Want to learn more about what is a Dockerfile and Docker in general? I highly recommend the video below:

To understand more about the concept of a Dockerfile, what it is and more details, see the official Docker documentation:

What is docker compose used for?

Docker Compose makes it easy to define and manage multiple containers as a single application. It uses a YAML file (docker-compose.yml) to describe the services, networks, and volumes required for your application. This file uses the Compose format.

With the simple commands up, down and ps, you can start, stop and list the status of the services defined in the docker-compose.yml file, making it easier to develop, test and deploy applications made up of several containers, you can create and start your application with a single command: docker compose up.

So, what is docker compose used for? In a nutshell, Docker Compose is a tool for managing Docker Containers.

Three main Docker Compose commands:

  1. docker-compose up: Raises the services that have been declared in the Compose file (docker-compose.yml). If the necessary images are not present locally, Compose will download all of them.
  2. docker-compose down: Stops and removes all containers, networks and volumes created by docker-compose up.
  3. docker-compose ps: Lists all running services specified in the docker-compose.yml file, showing their status (running, stopped, etc.).

Example of docker-compose.yml:

version: '3' # Docker Compose format version

services: # Definition of docker-compose services

  webapp: # Service name
    image: nginx:latest # Docker image to be used for this service
    ports: # Port mapping
      - "8080:80" # Maps port 8080 of the host to port 80 of the Docker Container
    volumes: # Mapping volumes
      - ./html:/usr/share/nginx/html # Mount the local 'html' directory inside the container
    networks: # Networks to be connected
      - backend-network # Connects this service to the 'backend-network' network

networks: # Definition of networks
  backend-network: # Network name
    driver: bridge # Type of network driver

Installing Docker and Docker Compose

It may seem trivial, but it’s important to note that Docker Compose and Docker are different things! However, in some cases, we need to install Docker and Docker Compose separately.

Below are the installation details for each scenario:

On macOS:

  • Docker: Download and install Docker Desktop for macOS from the official Docker website. Open the .dmg file, drag the icon to the Applications folder and follow the instructions to complete the installation.
  • Docker Compose: Docker Desktop includes Docker Compose, so after installing Docker Desktop, Docker Compose will be available.

On Linux (Ubuntu as an example):

Docker

Follow the detailed instructions on the official Docker website to install the Docker Engine on your Linux system or follow the detailed steps below:

Add repositories

Before installing Docker Engine on a fresh host machine, it’s essential to configure the Docker repository settings. You can then proceed to install or update Docker based on this repository.

# Add the official Docker GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to the Apt source:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Note: If you use a distribution derived from Ubuntu, such as Linux Mint, you may need to use UBUNTU_CODENAME instead of VERSION_CODENAME.

Installing Docker packages

To install the latest version of Docker CE, Docker CE CLI, Containerd.io, Docker Buildx plugin and Docker Compose plugin, run the following command in the terminal: sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Confirm the successful installation of Docker Engine by executing the hello-world image

sudo docker run hello-world

This command downloads a test image and starts it in a container. When it starts, the container displays a confirmation message before shutting down.

Docker Compose

Install Docker Compose following the instructions for Linux on the official Docke website or follow the steps below:

Step 1: Download the current version of Docker Compose

  1. Open a terminal.
  2. Use the curl command to download the current version of Docker Compose. You can check the latest version on the Docker Compose releases page on GitHub.
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Make sure you replace 1.29.2 with the latest version available, if necessary.

Step 2: Give Docker Compose permission to run

Grant execute permissions to the downloaded file using the chmod command.

sudo chmod +x /usr/local/bin/docker-compose

Step 3: Verify the installation

Check that the installation was successful by typing:

docker-compose --version

This will show the installed version of Docker Compose, ensuring that the installation was successful.

On Windows:

  • Docker: Download and install Docker Desktop for Windows from the official Docker website. Run the downloaded installer and follow the steps shown on the screen.
  • Docker Compose: Docker Desktop for Windows includes Docker Compose, so after installing Docker Desktop, Docker Compose will be available.

Installing Docker and Docker Compose on different operating systems is relatively simple, following the steps provided by the respective official Docker websites for each platform. These tools are essential for developers and operators who want to build, test and manage container-based applications in an efficient and scalable way.

Main Docker commands

Here’s a list of the main, basic Docker commands that are important for getting started with containers:

Docker Run

The docker run command is your gateway to starting containers from Docker images. It allows you to specify image names, options and runtime settings.

docker run -d -p 8080:80 nginx

-d: Runs the container in detached mode (in the background).

-p: Maps ports from the host to the container.

Docker Pull

Before running a container, it is often necessary to download the Docker image from a registry such as Docker Hub. The docker pull command performs this task.

docker pull ubuntu:latest

Docker PS

To display a list of containers that are running, you can use the docker ps command, which is the command we use most often in our day-to-day work with Docker. It provides information on container IDs, names, status and ports.

To view all containers, including those that are stopped, you can utilize the command docker ps -a.

docker ps

Docker Stop e Docker Start

These two commands provide the capability to manage the operational status of a container efficiently. docker stop terminates a running container (leaving it with a status of stopped), while docker start resumes the execution of a container that was stopped.

docker stop {container_name_or_id}
docker start {container_name_or_id}

Docker Logs

The docker logs command is indispensable for troubleshooting and monitoring purposes, allowing retrieval of the logs generated by a specific container.

docker logs {container_name_or_id}

Docker Exec

You can effectively interact with and manage a running container by using the docker exec command. This command enables you to execute commands directly within the container’s environment, providing a convenient way to inspect its state, install software, or troubleshoot issues without needing to stop or restart the container. It’s particularly useful for tasks such as modifying configurations, checking application logs, or performing debugging operations in real time.

docker exec -it {container_name_ou_id} {bash}

-it: Interactive mode with a terminal. bash: The shell of your choice that you wish to utilize within the container environment.

Docker Build

When you need to create a customized Docker image, the docker build command is your ally. It uses a Dockerfile to define the image instructions.

docker build -t custom_image_name .

Docker Images

To list all Docker images currently available on your local system, execute the command docker images. This command provides comprehensive details such as image names, sizes, and tags associated with each image.

docker images

Docker RMI

To delete Docker images that are no longer used and free up some disk space. The docker rmi command enables you to delete Docker images based on their name or ID. This functionality is essential for managing disk space and cleaning up unused or outdated images from your local repository.

docker rmi image_name_or_id

Docker CLI Cheat Sheet

For a list of the main commands and their examples, I highly recommend the Cheat Sheet below:

https://docs.docker.com/get-started/docker_cheatsheet.pdf

Conclusion

We explore the fundamental concepts of Docker(what is Docker Container, image, etc), Dockerfile(what is a Dockerfile and why to use it) and Docker Compose(what is docker compose used for), which have revolutionized the way we develop, package and run modern applications. Docker provides an efficient and consistent approach to creating isolated development and production environments, ensuring portability and scalability for our applications.

By mastering these essential tools, teams of developers, DevOps and other relevant teams are prepared to face the modern challenges of software development, taking advantage of the benefits of isolated, scalable and highly controlled environments provided by the Docker ecosystem.

FAQs

1. What is Docker container?
A Docker container is a piece of software that contains all the essential elements for running an application, such as code, execution environment, libraries and dependencies.

2. Is Docker free?
Yes, Docker is a free open source platform, but it also offers paid versions with additional features.

3. What is the difference between Docker and virtual machines?
Docker containers share the kernel of the host operating system and are lighter and faster to start compared to virtual machines, which include a complete operating system.

4. Can I use Docker in production?
Yes, Docker is used on a large scale in production environments, large companies and other highly critical environments that demand reliability and efficiency.

5. What is Docker Compose?
Docker Compose is a tool that facilitates the definition and management of multi-container Docker. It is useful for configuring and launching applications that require several services.


Want to learn more?

Don’t forget to subscribe to our newsletter to receive the latest news and tips on DevOps/SRE directly in your e-mail:

Receive news by email

* campo obrigatório

Also, read our other posts on Docker, to learn beyond What is Docker Container, What is a Dockerfile and continue improving your DevOps skills.


cover image from tawatchai07 in Freepik

Compartilhe / Share
Fernando Müller Junior
Fernando Müller Junior

I am Fernando Müller, a Tech Lead SRE with 16 years of experience in IT, I currently work at Appmax, a fintech located in Brazil. Passionate about working with Cloud Native architectures and applications, Open Source tools and everything that exists in the SRE world, always looking to develop and learn constantly (Lifelong learning), working on innovative projects!

Articles: 33

Receba as notícias por email / Receive news by email

Insira seu endereço de e-mail abaixo e assine nossa newsletter / Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *