Local Development with Docker Compose
Last updated November 06, 2023
Table of Contents
Docker Compose is a tool for defining and running a multi-container Docker application. Learn how Docker Compose is great for local development, how you can push your Docker images to Heroku for deployment, and other tips and tricks.
Introduction to Docker Compose
Let’s start out with a simple Python-based multi-container application. This example app consists of Redis, a web frontend for caching, and Postgres as our database. Docker, Redis, and Postgres are each run in a separate container.
You can use Docker Compose to define your local development environment, including environment variables, ports that you need accessible, and volumes to mount. You define everything in
docker-compose.yml, which the
docker-compose CLI uses.
docker-compose.yml for the application is:
version: '2' services: web: build: . ports: - "5000:5000" env_file: .env depends_on: - db volumes: - ./webapp:/opt/webapp db: image: postgres:latest ports: - "5432:5432" redis: image: redis:alpine ports: - "6379:6379"
The Web Service
The first section defines the web service. It opens port 5000, sets environment variables defined in
.env, and mounts our local code directory as a volume.
services: web: build: . ports: - "5000:5000" env_file: .env depends_on: - db volumes: - ./webapp:/opt/webapp
The Database service
The next service is the Postgres database, which opens port 5432 and uses the latest official Postgres image on Docker Hub.
db: image: postgres:latest ports: - "5432:5432"
The Redis Service
This section defines our Redis service, which opens port 6379 and uses the official Redis image on Docker Hub.
redis: image: redis:alpine ports: - "6379:6379"
Now that you defined the local development environment in
docker-compose.yml, you can spin up all three services with one command.
$ docker-compose up
This command confirms that all three containers are running.
$ docker ps CONTAINER ID IMAGE COMMAND 8e422ff92239 python_web "/bin/sh -c 'python a" 4ac9ecc8a2a3 python_db "/docker-entrypoint.s" 2cbc8febd074 redis:alpine "docker-entrypoint.sh"
The Benefits of Docker Compose
Using Docker and defining your local development environment with Docker Compose provides several benefits:
- By running Redis and Postgres in a Docker container, you don’t install or maintain the software on your local machine.
- You can check your entire local development environment into source control, making it easier for other developers to collaborate on a project.
- You can spin up the entire local development environment with one command:
Pushing Your Containers to Heroku
$ heroku container:push web
The Python application depends on Postgres and Redis, which you don’t push to Heroku. Instead, use Heroku add-ons in production.
Use Heroku Add-ons in Production
- For local development: Use official Docker images, such as Postgres and Redis.
- For staging and production: Use Heroku add-ons, such as Heroku Postgres and Heroku Data for Redis.
Using official Docker images locally and Heroku add-ons in production provides you with the best of both worlds.
- Parity: You get parity by using the same services on your local machine as you do in production.
- Reduced ops burden: By using add-ons, Heroku or the add-on provider takes the ops burden of replication, availability, and backup.
Docker Compose Tips and Tricks
When using Docker Compose for local development, knowing these tips and tricks can help.
.env File to Avoid Checking Credentials Into Source Code Control
By using Docker and Docker Compose, you can check your local development environment setup into source code control. To handle sensitive credentials, create a .env environment file with your credentials and reference it within your Compose YAML. Add your
.env to your
.dockerignore files, so that it’s not checked into source code control or included in your Docker image.
services: web: env_file: .env
Mount Your Code as a Volume to Avoid Image Rebuilds
Any time you make a change to your code, you must rebuild your Docker image. This rebuild is a manual, potentially time-consuming step. To solve this issue, mount your code as a volume. Now, rebuilds are no longer necessary when the code changes.
services: web: volumes: - ./webapp:/opt/webapp
Use Hostnames to Connect to Containers
By default, Docker Compose sets up a single network for your app. When you name a service in your Compose YAML, it creates a hostname that you can use to connect to the service.
Our services in Compose YAML:
services: web: redis: db:
Our connection strings:
Running Compose in Background Mode
When you execute
docker-compose up, your project runs in the foreground, displaying the output of your services. You can shut down the services gracefully using
One lesser-known option is to use
docker-compose up -d to start your containers in the background. You can tear down the Compose setup using
docker-compose down. You check the logs of services running in background mode using
Multi-Dockerfile Project Structure
When you have multiple services, we suggest creating a subdirectory for each Docker image in your project, with the Dockerfile stored in each respective directory.
/web/Dockerfile /redis/Dockerfile /db/Dockerfile /worker/Dockerfile
We don’t recommend storing all Dockerfiles in the project home directory because it makes distinguishing between services harder.
Run Your Containers as a Non-Root User
It’s a good security practice to run your containers as a non-root user. Also, containers that you push to Heroku run without root access. By testing your containers locally as a non-root user, you can ensure that they work in your Heroku production environment. Learn more in the container registry documentation.