It’s been over a decade since Docker appeared on the software development scene, and today, almost no one can imagine a software project without using Docker at some stage. The emergence of Docker has revolutionized how we develop, deploy, and run our applications.
In this post, we'll explain what Docker is, its advantages, and how to get started with it. We'll also provide practical examples to help you make the most of this powerful technology.
Docker is an open-source technology that simplifies deploying applications across different environments using containers. The main features of these containers are that they are lightweight, portable, and self-sufficient. This allows developers to focus on their code rather than the multitude of configurations needed to replicate their applications in different environments.
Using Docker in any environment offers numerous benefits, including:
The installation process can vary depending on the operating system used. Below, we describe the installation for the most common systems.
You can verify the installation by running:
docker --version
To test Docker, you can run a test image:
docker run hello-world
Aunque los contenedores y las máquinas virtuales (VM) permiten ejecutar aplicaciones en entornos aislados, existe una diferencia clave entre ellos: Los contenedores comparten el sistema operativo (kernel) del host, lo que los hace más eficientes y ligeros. En cambio, las VM incluyen su propio sistema operativo completo lo que las hace menos eficientes y más pesadas.
To summarize: the image defines what's needed and how it should run, while the container is the practical, executable manifestation of that definition.
Find a complete list of available commands here.
A Dockerfile is a text file containing the instructions needed to create a Docker image. It defines how to build the image, including the base, dependencies, files, and configuration.
Here's a basic example of the usual structure of a Dockerfile:
FROM ubuntu:latest
RUN apt-get update && apt-get install -y python3
COPY . /app
WORKDIR /app
CMD ["python3", "app.py"]
Creating a Dockerfile is straightforward and follows a series of instructions. Here are the most common ones:
FROM
command.RUN
to execute commands like package installations.COPY
or ADD
to copy files from the host to the container.WORKDIR
to set the working directory.CMD
to define the command that will run when the container starts.Explore all instructions in Docker's official documentation.
Each instruction in a Dockerfile creates a new layer that is cached to speed up rebuilds. If an instruction is modified, all previous layers remain cached, but the modified instruction and those that follow must be rebuilt, increasing build time.
Using the VOLUME
instruction:
FROM python:3.9-slim
WORKDIR /app
COPY . /app
VOLUME /app/data
CMD ["python", "app.py"]
Once defined, you can use these variables in other instructions in the Dockerfile:
FROM ubuntu:20.04
ENV APP_ENV=production
ENV APP_PORT=8080
RUN echo "Running in ${APP_ENV} mode on port ${APP_PORT}"
First, declare the argument with ARG in the Dockerfile:
FROM ubuntu:20.04
ARG APP_ENV
ARG APP_PORT
ENV APP_ENV=${APP_ENV}
ENV APP_PORT=${APP_PORT}
RUN echo "Running in ${APP_ENV} mode on port ${APP_PORT}"
During build: Using “–build-arg”:
docker build --build-arg APP_ENV=production --build-arg APP_PORT=8080 -t myapp .
At runtime: Using the -e option with “docker run”:
docker run -e APP_ENV=production -e APP_PORT=8080 myapp
Docker Compose is a tool designed to define and manage multi-container applications. Through a YAML file, you can configure all the services that make up your application.
It's especially useful for applications that depend on multiple services, such as databases and caching systems. This tool simplifies the management and orchestration of these services, allowing them to work together in a coordinated way in containers.
Docker Desktop installs Docker Compose by default, so no additional installation is required.
You can verify the installation with:
docker-compose --version
version: '3'
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./app:/var/www/html
db:
image: mariadb:10
ports:
- "6033:3306"
environment:
MYSQL_ROOT_PASSWORD: example
Explanation of Each Section:
Once the development environment is defined in Docker Compose, several commands can be used to build, deploy, and manage the containers.
To build and deploy the containers specified in the docker-compose.yml file, use the command:
docker-compose up
This command creates and runs the containers according to the configurations, displaying the output in the terminal to monitor their status and detect possible errors.
Docker Compose offers several useful commands for managing and monitoring the development environment:
These commands make controlling and monitoring the development environment efficient.
Docker has transformed software development by allowing applications and their dependencies to be packaged into portable, lightweight containers, improving efficiency and consistency across various environments.
Docker Compose simplifies the management and orchestration of multi-container applications, offering significant benefits like portability, isolation, and scalability.
Adopting Docker and following recommended best practices optimizes the development and deployment of complex applications.