A container is a lightweight environment that allows applications to run in isolation on the same server, preventing interference between them. Compared to virtual machines, it occupies fewer resources and starts up more quickly.
A Docker image is a file that contains everything needed to run an application in a container, including code, libraries, and dependencies. It acts as a template for creating containers and is essential in using Docker for developing and deploying applications.
In Docker, only the application layer is virtualized, meaning it uses the host machine's operating system kernel. This helps save storage space and makes containers lighter.
Docker Desktop is an application that facilitates the management of Docker containers by providing a graphical interface for creating, running, and managing containers and images, simplifying the development and deployment of applications. Additionally, it includes tools like Docker Compose to define and run multi-container applications and an integrated development environment to ease working with Docker.
Installation page: Docker Desktop
We can download images from Docker Hub, which is a place where Docker container images are stored. This platform allows us to access a wide variety of images for our applications and services.
Here’s the translation to English:
List the images we have downloaded on our machine
docker images
It will display the repository name, tag, image ID, creation date, and size.
Download an image
docker pull mariadb
docker pull mysql:9.1
Note
If the version is null, the latest version will be downloaded.
Delete an image
docker rmi NAME:TAG
docker rmi IMAGE_ID
Note
You can also use the following command:
docker image rm NAME:TAG / IMAGE_ID
Create a container
docker create IMAGE_NAME
Note
It will return the container ID.
Create a container with port mapping.
docker create -pHOST_PORT:CONTAINER_PORT --name MyDB IMAGE_NAME
Run a container
docker start ID/NAME
View the status of containers
docker ps
It will display the container IDs, the image used, the command each container is running, the creation date, current status, the ports in use, and the container name.
docker ps -a
It will also show inactive containers.
Stop a container
docker stop ID/NAME
Rename a container
docker create --name MyDB ID/CURRENT_NAME
Tip
Using the names you assign to your containers instead of the ID makes management easier and commands more readable and understandable.
Delete a container
docker rm ID/NAME
docker logs ID/NAME
With real-time following
docker logs -f ID/NAME
With timestamps
docker logs --timestamps ID/NAME
Here’s the translation to English:
The docker run
command combines the creation and execution of a container from an image into a single step, simplifying the deployment of applications, unlike docker create
, which only defines it.
When running docker run
, Docker follows these steps:
- Image Verification: If the image is not found locally, Docker automatically downloads it from the configured repository (by default, Docker Hub).
- Container Creation: It defines the container with the image and the provided options (ports, volumes, environment variables, etc.).
- Container Execution: It starts the container and runs the command or application specified in the image.
- Output and Monitoring: In attached (foreground) mode, it displays output in the terminal; in detached mode, the container continues running in the background.
This flow facilitates a quick and controlled deployment of applications in containers.
docker run mongo
To run it in the background
docker run -d mysql
With more options
docker run --name MyDB -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my_password -d mysql
Note
The -e
option in Docker is used to set environment variables within the container.
When creating a container, it is essential to configure environment variables to customize its operation. For example, in MySQL, the MYSQL_ROOT_PASSWORD
variable is mandatory to set the password for the root user. These variables allow initializing applications with specific configurations and facilitate container management without the need to restart it.
In Docker Hub, each image details the required and optional environment variables in its documentation. These configurations are crucial to ensure that the application functions according to user needs. Consulting this information is essential for effectively and securely deploying applications in containers.
docker create -p27017:27017 --name myMongoDB -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=p4$$w0rd mongo
A Dockerfile is a text file that contains instructions for building a Docker image. It serves to automate the image creation process, specifying the environment, dependencies, and configurations necessary to run an application.
It must be named Dockerfile (without an extension) because it is the standard name that Docker recognizes for automatically building images.
FROM node:23
RUN mkdir -p /home/app
COPY . /home/app # "." Path of the host OS where the source code is located
EXPOSE 3000
CMD ["node", "/home/app/index.js"]
FROM
: Defines the base image for building the new image.RUN
: Creates the/home/app
directory in the image to store application files or data that will be used in the container.
Note
It creates the directory in the image, NOT on your physical machine.
COPY
: Transfers files and directories from the host machine to the container in the image.EXPOSE
: Indicates that the container will listen on a specific port during its execution.CMD ["node", "/home/app/index.js"]
: Sets the default command that will be executed when starting the container, wherenode
is the executable and/home/app/index.js
is the argument passed to the command.
Warning
When using CMD
, make sure to specify the absolute path of the file to execute. This ensures that the container can find and execute the file correctly.
For more options: Dockerfile
Create an image
docker build -t IMAGE_NAME:TAG /path/to/Dockerfile
The docker network
command is used to manage networks in Docker, allowing the creation, deletion, and configuration of networks for containers. This command is essential for establishing communication between containers, facilitating their interaction in an isolated and secure environment. By creating custom networks, you can control how containers connect to each other and to the outside world, improving the organization and security of your containerized applications.
List all configured Docker networks
docker network ls
Create a network
docker network create NW_NAME
Delete a network
docker network rm NW_NAME
The container name acts as a unique identifier and can be used as a domain name to facilitate communication between containers.
Connect the container to a specific network
docker create -p 3306:3306 --name mymysql --network NW_NAME -e MYSQL_ROOT_PASSWORD=p4$$w0rd mysql
Docker Compose is a tool that allows you to define and run applications composed of multiple containers using a YAML configuration file (docker-compose.yml
). It simplifies the management of multiple services, networks, and volumes with a single command, streamlining the configuration and deployment process.
Advantages over a Dockerfile:
- Orchestration: Enables management of multiple containers as a single service, making it easier to configure complex applications.
- Ease of Use: Provides a declarative and more accessible format for defining services and their interactions.
- Simplified Configuration: Facilitates the setup of networks and shared volumes among containers without needing to create multiple Dockerfiles.
Example configuration file:
version: '3.9'
services:
web:
build: .
ports:
- "8080:80"
networks:
webnw: # Assigns an IP within the custom network
ipv4_address: 10.10.10.10
restart: always
depends_on:
- db
environment:
- NODE_ENV=production
- DATABASE_URL=postgres://user:password@db:5432/mydatabase
volumes:
- .:/usr/src/app
db:
image: postgres:latest
ports:
- "5432:5432"
networks:
webnw:
ipv4_address: 10.10.10.11
restart: always
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
- POSTGRES_DB=mydatabase
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data: # Declares the volume 'db_data'
networks:
webnw:
ipam: # IP Address Management
driver: default
config:
- subnet: "10.10.10.0/24"
-
version:
: Specifies the version of the Docker Compose syntax used. (Currently can be omitted) -
services:
: Defines the different services that make up the application, each running in a separate container. -
web:
: Name of the main service representing the web application. -
build: .
: Indicates that the container should be built from the Dockerfile in the current directory (.
). -
ports:
: Maps ports between the container and the host machine. In this case, port 8080 of the host machine connects to port 80 of the container. -
restart:
Sets when a container should restart if it stops. -
depends_on:
: Specifies that theweb
service depends on thedb
service, ensuring that it starts afterdb
is available. -
environment:
: Defines environment variables for the container. -
volumes:
: Used to define volumes that persist container data on the host system, preventing loss when stopping or removing the container. -
db:
: Name of the service representing the PostgreSQL database. -
image: postgres:latest
: Indicates that the latest PostgreSQL image from Docker Hub will be used. -
networks
:
networks:
webnw:
ipam:
driver: default
config:
- subnet: "10.10.10.0/24"
The networks
section defines a custom network with a default IP driver and a specific subnet to assign IP addresses to connected containers.
These configurations offer flexibility in assigning and managing IP addresses in container environments.
Types of drivers:
-
bridge: Creates an isolated network for containers that can communicate with each other but not with the outside world unless ports are exposed. (Default)
-
host: Allows a container to use the host's network, removing network isolation.
-
overlay: Facilitates communication between containers on different hosts, useful for distributed applications.
-
macvlan: Assigns a MAC address to a container, allowing it to appear as a physical network device on the local network.
-
none: Disables networking for the container, with no access to any network.
IPAM configurations in Docker
The config
section of IPAM allows defining various settings for managing IP addresses in Docker's custom networks. The following configurations are available:
-
subnet: Defines the range of IP addresses to be used in the network.
-
gateway: Specifies the IP address of the gateway for the network.
-
ip_range: Sets a specific range of IP addresses within the subnet for assignment to containers.
-
auxiliary_addresses: Allows reserving additional IP addresses for specific uses within the subnet.
-
ipv6: Enables IPv6 address management for the network if needed.
Types of Volumes
-
Anonymous Volumes: Automatically created and deleted with the container. Used for temporary data.
volumes: - /data # Anonymous
-
Named Volumes: Persist with a specific name. Retain data regardless of the container.
volumes: - db_data:/var/lib/postgresql/data # Named # mysql -> /var/lib/mysql # mongo -> /data/db
-
Directory Mounts: A directory from the host machine is mounted in the container, reflecting real-time changes.
volumes: - ./app:/usr/src/app # Mount
Start all the containers defined in docker-compose.yml
, creating networks and volumes as specified. If the images are not available, it automatically downloads them.
docker compose up
# docker compose up -d -> Run in the background
Stop and remove the created containers, networks, and volumes.
docker compose down
Stop running containers without removing them.
docker compose stop
Show the status of the containers managed by Docker Compose.
docker compose ps
Show the logs of the running services.
docker compose logs