Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Docker Tip #10: Project Structure with Multiple Dockerfiles and Docker Compose

docker-tips-and-tricks.jpg

When it comes to organizing your larger projects with Docker and Docker Compose there are a number of options. Here's how I do it.

Quick Jump:

Smaller projects are easy to organize right? You have 1 application which typically has a single Dockerfile and docker-compose.yml file.

Organizing a simple project with Docker on your file system could look like this:
myapp/
  - appcode/
  - Dockerfile
  - docker-compose.yml

Then your docker-compose.yml file could look like this:

version: '3'

services:
  myapp:
    build: '.'

But what happens when you have a few apps that each have their own Dockerfile? Chances are you’ll want a single docker-compose.yml file to control your entire stack of apps and services.

Here’s how I typically organize my micro-services based projects:
myapp/
  - auth/
    - Dockerfile
  - billing/
    - Dockerfile
  - contact/
    - Dockerfile
  - user/
    - Dockerfile
  - docker-compose.yml

In this case, each service has its own Dockerfile because each of them are separate applications that belong to the same project. These services could be written in any language, it doesn’t matter.

The docker-compose.yml file for that might look like this:

version: '3'

services:
  auth:
    build: './auth'
  billing:
    build: './billing'
  contact:
    build: './contact'
  user:
    build: './user'

That’s going to build the Dockerfile for each of the services.

An alternative approach with multiple docker-compose.yml files:

If you’d rather have a separate docker-compose.yml file for each service you can run them together in 1 docker-compose command. Docker Tip #87 goes over the details.

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.



Comments