Best practice - having multiple docker-compose files in a repo

Viewed 434

I'am currently working on a fullstack web project that consists of the following components:

  • Database (MariaDB)
  • Frontend (Angular)
  • Backend (NodeJS)

Every component should be deployable through docker. For that I have a Dockerfile for each of them. I also defined a docker-compose in the repository root to deploy all of them together.

# current repo structure
|frontend/
 |src/
 |docker/
  -Dockerfile
  -docker-compose.yml
|backend/
 |src/
 |docker/
  -Dockerfile
  -docker-compose.yml
|database/
 |src/
 |docker/
  -Dockerfile
  -docker-compose.yml
-docker-compose.yml

Do you think this is good practice? I am unsure because I think this my current structure is kind of confusing. How do you handle it in similar projects?

2 Answers

docker-compose is designed to orchestrate multiple components of a project in one single place: docker-compose file.

In your case, and as m303945 said, you don't need multiple docker-compose files. Indeed, your main docker-compose.yml should call the Dockerfile of each of your component. This file could contain something like this:

services:

  frontend:
    build:
      context: frontend
      dockerfile: docker/Dockerfile

  backend:
    build:
      context: backend
      dockerfile: docker/Dockerfile

  database:
    build:
      context: database
      dockerfile: docker/Dockerfile

you dont need multiple docker-compose files. if you want to run specific app together, for example only database and backend just run this command.

docker-compose -f docker-compose-file.yml up -d database backend 

which database and backend is service name in the docker-compose file.

Related