This is a crosspost from is a tool that helps you define and run multi-container applications. Instead of long docker commands, you write a YAML file that describes your entire application stack.
Let's convert our previous setup to a Docker Compose file. Create a new file called docker-compose.yml:
services:
api:
build: .
ports:
- "8080:8080"
environment:
- SECRET_MESSAGE=Hello Docker!
- PORT=8080
volumes:
- mydata:/data
networks:
- myapp-network
volumes:
mydata:
networks:
myapp-network:
Now instead of that long docker command, you can simply run:
docker compose up
Docker Compose will:
- Create the network
- Create the volume
- Build the image (if needed)
- Start the container with all specified configuration
To stop everything:
docker compose down
Let's add some more complexity to our application. We will add a database and a reverse proxy.
services:
api:
build: .
ports:
- "8080:8080"
environment:
- SECRET_MESSAGE=Hello Docker!
- PORT=8080
volumes:
- mydata:/data
networks:
- myapp-network
depends_on:
- db
db:
image: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=myapp
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- myapp-network
nginx:
image: nginx
ports:
- "80:80"
networks:
- myapp-network
depends_on:
- api
volumes:
mydata:
postgres_data:
networks:
myapp-network:
What we did here is:
- Added a database service
- Added a reverse proxy service
- Added a dependency to the api service (so it starts after the database)
- Added a dependency to the nginx service (so it starts after the api)
For simplicity sake we hardcoded our secrets here. In a production environment you would of course not want to do that! How you can properly manage secrets is a topic for another day (probably tomorrow).
Basic Docker Compose Commands
Here are the most important commands you'll use:
docker compose up- Start services
docker compose up -d- Start in detached mode
docker compose down- Stop and remove containers
docker compose ps- List running services
docker compose logs- View service logs
docker compose build- Build or rebuild services
Next Steps
This is just the beginning of what Docker Compose can do! Tomorrow we will go a bit deeper and learn about some advanced features.
As always, just by looking at the code you won't learn anything. So try converting your existing Docker commands to a compose file. Play around with it and see how it works, and how to break it.
If you have any questions, feel free to reach out by email.
Happy coding! 🐳🎄
Jonas
SOCIAL SHARE CARD GENERATOR