Master Docker Compose PART 2: Automate Your Containers Easily

🧠 What is Docker Compose and why is it important?

Docker Compose is a tool that allows you to define, configure and build multiple Docker containers using a single file called docker-compose.yml.

Instead of running a lot of docker run commands with complicated configurations, Compose allows you to define your entire application architecture in a clean, readable and reusable file.

✅ Why is it so important?

  • It automates and simplifies the deployment of complex projects.
  • Define all your services, networks, volumes and variables in One place.
  • Makes your development environment portable and replicable.
  • It accelerates the onboarding of new developers to the project.

📄 What does a file look like? docker-compose.yml?

Minimal example:

👉 This snippet launches a container based on the Nginx image, exposing container port 80 to host port 8080. Useful for accessing the web server from your browser at localhost:8080.

From Docker Compose v2, it is no longer necessary to declare version, since Docker automatically detects the appropriate scheme.

🔍 Detailed file structure docker-compose.yml

👉 This structure defines how a container should run. You can choose an image or build one from a Dockerfile, exposing ports, defining environment variables, mounting volumes, and declaring dependencies between services.

👨 ‍ 💻 Real case: Node.js app + PostgreSQL

🗂 Project structure

📦 docker-compose.yml

👉 This file defines two services: app, which is built from a Dockerfile and listens on port 3000, and db, a persistent PostgreSQL database. It uses environment variables for the configuration of the base and a volume to hold the data.

🧱 Dockerfile

👉 This Dockerfile create a lightweight Node.js image. Define /app as a working directory, install dependencies and launch the app on port 3000.

▶️ How do you use Docker Compose?

🧪 Lift all services:

👉 Raise all the containers defined in the file. Useful for local development.

💻 In the background:

👉 Same as above, but runs in the background (mode Detached).

🧹 To stop and clean:

👉 Stops all containers and removes associated networks.

🔄 To rebuild:

👉 Rebuild images from the Dockerfile. Useful if you made changes to the source code or the Dockerfile.

Docker Compose v2 is used directly with the docker compose command (without hyphen). It's no longer necessary to have docker-compose as a separate binary.

🧩 How does it align with your project?

Compose allows you to replicate the production architecture locally.

🌟 Good Practices

  • Usa .env for sensitive variables.
  • Version your file docker-compose.yml.
  • Usa volumes for persistence.
  • Usa restart: always if necessary.
  • Comment and document if you work as a team.
  • Keep your services on customized networks if you need more control.

📌 Conclusion

Docker Compose is the Next level after mastering Docker. It gives you control, order and ease to scale your development environment or even bring it to production. An indispensable tool for any modern developer.

🚀 Do you want to run Docker Compose on your projects and simplify your workflow? 💬 Write to us! We help you integrate Docker into your infrastructure and optimize your development. Contact us

Karla Cabañas

April 14, 2025