Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose file has become too long [closed]

Tags:

I recently started migrating my self-hosted services to docker. To simplify maintenance, I'm using docker-compose. Some of the containers depend on each other, others are independent.

With more than 20 containers and almost 500 lines of code, maintainability has now decreased.

Are there good alternatives to keeping one huge docker-compose file?

like image 385
BayerSe Avatar asked Oct 09 '18 18:10

BayerSe


1 Answers

That's a big docker-compose.yml! Break it up into more than one docker-compose file.

You can pass multiple docker-compose files into one docker-compose command. If the many different containers break up logically, one way of making them easier to work with is breaking apart the docker-compose.yml by container grouping, logical use case, or if you really wanted to you could do one docker-compose file per service (but you'd have 20 files).

You can then use a bash alias or a helper script to run docker-compose.

# as an alias
alias foo='docker-compose -f docker-compose.yml -f docker-compose-serviceA.yml -f docker-compose-serviceB-yml $@'

Then:

# simple docker-compose up with all docker-compose files
$ foo -d up

Using a bash helper file would be very similar, but you'd be able to keep the helper script updated as part of your codebase in a more straightforward way:

#!/bin/bash

docker-compose -f docker-compose.yml \
  -f docker-compose-serviceA.yml \
  -f docker-compose-serviceB.yml \
  -f docker-compose-serviceC.yml \
  $@

Note: The order of the -f <file> flags does matter, but if you aren't overriding any services it may not bite you. Something to keep in mind anyway.

like image 154
bluescores Avatar answered Nov 24 '22 20:11

bluescores