Docker Compose


Why do we nedd docker compose
Let's imagine you’re running a school project on your computer, and this project needs three things to work
A website (frontend)
A server (backend to handle logic)
A database (Source of truth)
Now, imagine each of those runs in a separate room (this is like a Docker container).
But here's the problem:
You need to start all three rooms every time you want to run your project.
They need to talk to each other.
You might have to remember a bunch of commands to get everything running correctly.
If you share this project with a friend, they’ll also have to repeat all that.
That’s where Docker Compose comes into role.
suppose a new dev join your team and you project have 2 frontend 2 backend, 1 DB now new dev have to start all the container one by one which is super weird.
docker run frontend1
docker run frontend2
docker run backend1
docker run backend2
docker run postgre
What is Docker Compose
Docker Compose is a tool for defining and running multi-container applications. You set everything up in a YAML file, where you describe the services, networks, and volumes your app needs. After that, you can launch the entire setup with just one command.
Solved the problem of how to build and describe multi-container applications.
Without Docker compose
Without Docker Compose we need to follow the below steps to start a single project with a postgress container and a backedn container.
Create a network: First of all need to create a network so that we can mount both of the container on that network so that they can communicate to each other.
docker network create <name_of_the_network>
Create a volume: To make sure data persist across the re-start of the container.
docker volume create <name_of_the_volume>
Start postgres container
docker run -d -e POSTGRES_PASSWORD=<your_secret_password> -v <name_of_the_volume>:/var/lib/postgresql/data --name postgres --network <name_of_the_network> postgres
Start backend container
docker run -d -e DATABASE_URL=postgresql://postgres:mysecretpassword@postgres:5432/postgres -p 3000:3000 --name backend --network <name_of_the_network> backend
Without docker compose we have to follow all the above steps to start a app.
To start the above app with docker compose we have to create a yaml file describing all your containers and volumes (by default all containers in a docker-compose run on the same network)
docker compose up
Subscribe to my newsletter
Read articles from Vinod Prajapati directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
