01 Containerization Fundamentals - KleberVales/Dominando-Docker GitHub Wiki
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Imagine you need to send a cake to a friend. Instead of just sending the recipe and hoping the ingredients, oven temperature, and baking skills are exactly the same as yours, you deliver the entire, finished cake in a sealed box.
Docker is this "sealed box" for software.
Technically, Docker is an open platform for developing, shipping, and running applications. It allows you to "package" an application with all its dependencies (libraries, frameworks, runtime code, configurations, etc.) into a standardized unit called a container.
-
Container: An executable instance of an image. It's lightweight, portable, and contains everything needed to run the software. Think of it as a running process, but isolated from the rest of the system.
-
Image: A read-only template, a "recipe," or "template" used to create containers. It defines what the container will hold.
-
Virtual Machine (VM): Includes a complete guest operating system (Guest OS) that runs on top of a host operating system (Host OS) using a hypervisor. It is heavy (consumes GBs of disk and RAM) and slow to start.
-
Docker Container: Shares the host operating system kernel. It only packages the application and its dependencies. It's lightweight (consumes 1 MB of storage), fast (starts in seconds), and much more efficient.
The Image is the starting point, the read-only and immutable model.
What is it? A package that contains all the dependencies and information needed to create a container. Think of it as a mold, a cake recipe, or a template.
-
Immutable: Once created, it doesn't change. To make a change, you create a new layer on top of the existing one, creating a new image.
-
Layers: Images are built in layers. Each instruction in a Dockerfile creates a new layer. This makes downloading and storage very efficient, as layers can be reused across images.
-
Storage: They are stored in a Registry, the most famous being Docker Hub. It's like GitHub for images.
The Container is the running instance of an image.
What is it? An isolated, executable process that runs from an image. It is the actual unit of execution.
-
Ephemeral: By default, everything created or modified within a container (files, data) is lost when the container is removed.
-
Isolated: Has its own file system, its own network, and its own process space, isolated from the host system and other containers.
-
Lightweight: Shares the host kernel, making it much faster to start/stop than a VM.
Volumes are the persistence mechanism for data generated and used by containers.
What is it? A way to store data outside of the container's ephemeral file system. It's the correct way to persist data.
Analogy: It's the external hard drive or flash drive that you connect to your computer. The computer (container) can be formatted or replaced, but the data on the external drive (volume) remains safe.
Networks enable communication between containers and between containers and the outside world.
What is it? An isolated communication channel managed by Docker.
Why use it? By default, containers are isolated. If you want a web application container (e.g., Nginx) to talk to an API container (e.g., Node.js), you need to connect them to the same network.
-
bridge (Default): The default network for containers. Containers on the same custom bridge network can communicate by name.
-
host: Removes network isolation between the container and the Docker host. The container uses the host network directly.
-
none: Disables all networking for the container.
🧰 Essential Docker Commands
| Comando | Função |
|---|---|
| docker --version | Verifica a versão instalada do Docker |
| docker pull | Baixa uma imagem do Docker Hub |
| docker images | Lista todas as imagens disponíveis localmente |
| docker run | Cria e executa um container a partir de uma imagem |
| docker ps | Lista os containers em execução |
| docker ps -a | Lista todos os containers (inclusive os parados) |
| docker stop | Para um container em execução |
| docker start | Inicia um container parado |
| docker rm | Remove um container |
| docker rmi | Remove uma imagem |
| docker exec -it | Executa um comando dentro de um container (ex: abrir terminal) |
| docker logs | Exibe os logs de um container |
| docker build -t . | Cria uma imagem a partir de um Dockerfile |
| docker-compose up | Inicia múltiplos containers definidos em um docker-compose.yml |
| docker-compose down | Para e remove os containers do docker-compose |
- docker run -d -p 8081:8081 --name user-service user-service:latest
- docker run -d -p 8082:8082 --name auth-service-v2 auth-service:latest
- docker-compose up --build
- docker rm -f auth-service
- docker ps -a