Docker Compose: The Lazy Developer’s Best Friend


Feeling lazy to always run docker build
for each and every microservice?
Feeling overwhelmed thinking, What if I run the containers without taking dependencies into consideration? Then I’d have to stop them all again and rerun everything.
To make a developer’s life easier and automate things, we often put all our experience and time into writing a shell script. We hand it over to the DevOps team and the developers. But what if, after months, the script is broken? We feel broken too, right?
Wait a minute — here’s where Docker Compose comes in handy.
What is Docker Compose?
Wondering what Docker Compose is? It’s nothing but a very helpful tool to manage the lifecycle of multiple containers in a single YAML file.
It’s a standard provided with backward compatibility, so it won’t break in most cases (worst case scenario — maybe, but rarely).
It comes in very handy for developers. All they need are just two commands to start and stop containers:
docker-compose up
docker-compose down
So, let’s dive in and know more about it.
The Official Examples
There’s an official repository where you can find all examples regarding Docker Compose:
🔗 https://github.com/docker/awesome-compose
From there, we’ll take one such example: nodejs-nginx-redis.
What does this application do?
Redis acts as a cache.
Nginx (in round-robin fashion) sends requests to
web1
andweb2
.We can see how many times the requests go to
web1
andweb2
.
Important Note
Docker Compose is never a replacement for Docker.
Here we’ll still use Dockerfiles to build images, but we can also directly provide image details in the Compose file if we want.
Example docker-compose.yml
file
services:
redis:
image: 'redislabs/redismod'
ports:
- '6379:6379'
web1:
restart: on-failure
build: ./web
hostname: web1
ports:
- '81:5000'
web2:
restart: on-failure
build: ./web
hostname: web2
ports:
- '82:5000'
nginx:
build: ./nginx
ports:
- '80:80'
depends_on:
- web1
- web2
Why Docker Compose Makes Life Easier
First, we specify what services we want to run — here we have Redis, web1, web2, and Nginx.
We’ve specified the ports for each of them.
Here’s the beauty: there are four containers to build, and without Docker Compose, we would need to run docker build
and docker run
for each one manually, taking dependencies into account — a tedious task.
Instead, with the Compose file, everything is defined in one place. We’ve even specified depends_on
for web1
and web2
under the Nginx service, so they start in the right order.
Final Takeaway
If you’re tired of repetitive Docker commands and juggling dependencies, Docker Compose is your friend. It saves time, reduces human error, and keeps your containerized apps running in harmony.
So next time you feel the urge to write a giant shell script — pause, take a deep breath, and let Docker Compose handle it for you.
Try it now: Clone the Awesome Compose repo, pick an example, run docker-compose up
, and see the magic happen.
Hope you like it:)
Subscribe to my newsletter
Read articles from Sravya Bolla directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
