Skip Navigation
Show nav
Heroku Dev Center
  • Get Started
  • Documentation
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
  • Documentation
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
View categories

Categories

  • Heroku Architecture
    • Dynos (app containers)
    • Stacks (operating system images)
    • Networking & DNS
    • Platform Policies
    • Platform Principles
  • Command Line
  • Deployment
    • Deploying with Git
    • Deploying with Docker
    • Deployment Integrations
  • Continuous Delivery
    • Continuous Integration
  • Language Support
    • Node.js
    • Ruby
      • Rails Support
      • Working with Bundler
    • Python
      • Background Jobs in Python
      • Working with Django
    • Java
      • Working with Maven
      • Java Database Operations
      • Working with the Play Framework
      • Working with Spring Boot
      • Java Advanced Topics
    • PHP
    • Go
      • Go Dependency Management
    • Scala
    • Clojure
  • Databases & Data Management
    • Heroku Postgres
      • Postgres Basics
      • Postgres Getting Started
      • Postgres Performance
      • Postgres Data Transfer & Preservation
      • Postgres Availability
      • Postgres Special Topics
    • Heroku Data For Redis
    • Apache Kafka on Heroku
    • Other Data Stores
  • Monitoring & Metrics
    • Logging
  • App Performance
  • Add-ons
    • All Add-ons
  • Collaboration
  • Security
    • App Security
    • Identities & Authentication
    • Compliance
  • Heroku Enterprise
    • Private Spaces
      • Infrastructure Networking
    • Enterprise Accounts
    • Enterprise Teams
    • Heroku Connect (Salesforce sync)
      • Heroku Connect Administration
      • Heroku Connect Reference
      • Heroku Connect Troubleshooting
    • Single Sign-on (SSO)
  • Patterns & Best Practices
  • Extending Heroku
    • Platform API
    • App Webhooks
    • Heroku Labs
    • Building Add-ons
      • Add-on Development Tasks
      • Add-on APIs
      • Add-on Guidelines & Requirements
    • Building CLI Plugins
    • Developing Buildpacks
    • Dev Center
  • Accounts & Billing
  • Troubleshooting & Support
  • Integrating with Salesforce
  • Deployment
  • Deploying with Docker
  • Local Development with Docker Compose

Local Development with Docker Compose

English — 日本語に切り替える

Last updated July 15, 2022

Table of Contents

  • Introduction to Docker Compose
  • Pushing your containers to Heroku
  • Docker Compose tips and tricks
  • Further reading

Docker Compose is a tool for defining and running a multi-container Docker application. In this article you’ll learn why Docker Compose is great for local development, how you can push your Docker images to Heroku for deployment, and Compose tips and tricks.

Introduction to Docker Compose

Let’s start out with a simple python-based multi-container application. This example app is comprised of a web frontend, Redis for caching, and Postgres as our database. With Docker, the web frontend, Redis, and Postgres each run in a separate container.

You can use Docker Compose to define your local development environment, including environment variables, ports you need accessible, and volumes to mount. Everything is defined in docker-compose.yml, which is used by the docker-compose CLI.

The following is the docker-compose.yml for the application:


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 db 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 the local development environment is defined in docker-compose.yml, you can spin up all three services with one command:

$ docker-compose up

The following 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 you with a number of benefits:

  • By running Redis and Postgres in a Docker container, you don’t have to install or maintain the software on your local machine
  • Your entire local development environment can be checked 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: docker-compose up

Pushing your containers to Heroku

When you’re satisfied with the build, you can then push the web frontend directly to the Heroku container registry for deployment (popular CI/CD tools are also supported).

$ heroku container:push web

The python application depends on Postgres and Redis, which you do not 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, there are a few tips and tricks we think can help make you more successful.

Create an .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. Your .env should be added to your .gitignore and .dockerignore files so it is not checked into source code control or included in your Docker image, respectively.

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 need to rebuild your Docker image (which is a manual step and can be time consuming). To solve this issue, mount your code as a volume. Now rebuilds are no longer necessary when code is changed.

services:
  web:
    volumes:
      - ./webapp:/opt/webapp

Use hostnames to connect to containers

By default 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 then use to connect to the service.

Our services in Compose YAML:

services:
  web:
  redis:
  db:

Our connection strings:

postgres://db:5432
redis://redis:6379

Running Compose in background mode

When you execute docker-compose up your project is running in the foreground, displaying the output of your services. You can shut down the services gracefully using ctrl+c.

One lesser known option is to use docker-compose up -d to start your containers in the background (i.e., detached mode); you can tear down the Compose setup using docker-compose down. You check the logs of services running in background mode using docker-compose logs.

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, since 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; but more importantly, containers you push to Heroku will run without root access. By testing your containers locally as a non-root user, you can ensure they will work in your Heroku production environment. Learn more in the container registry documentation.

Further reading

  • Official Docker Compose documentation
  • Heroku container registry documentation

Keep reading

  • Deploying with Docker

Feedback

Log in to submit feedback.

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Podcasts
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing

Subscribe to our monthly newsletter

Your email address:

  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Heroku Podcasts
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Facebook
  • Instagram
  • Github
  • LinkedIn
  • YouTube
Heroku is acompany

 © Salesforce.com

  • heroku.com
  • Terms of Service
  • Privacy
  • Cookies
  • Cookie Preferences