Robert Strauch
Robert Strauch

Reputation: 12896

Exclude services from starting with docker-compose

Use Case

The docker-compose.yml defines multiple services which represent the full application stack. In development mode, we'd like to dynamically exclude certain services, so that we can run them from an IDE.

As of Docker compose 1.28 it is possible to assign profiles to services as documented here but as far as I have understood, it only allows specifying which services shall be started, not which ones shall be excluded.

Another way I could imagine is to split "excludable" services into their own docker-compose.yml file but all of this seems kind of tedious to me.

Do you have a better way to exclude services?

Upvotes: 8

Views: 5849

Answers (1)

anemyte
anemyte

Reputation: 20196

It seems we both overlooked a certain very important thing about profiles and that is:

Services without a profiles attribute will always be enabled

So if you run docker-compose up with a file like this:

version: "3.9"
services:
  database:
    image: debian:buster
    command: echo database

  frontend:
    image: debian:buster
    command: echo frontend
    profiles: ['frontend']

  backend:
    image: debian:buster
    command: echo backend
    profiles: ['backend']

It will start the database container only. If you run it with docker-compose --profile backend up it will bring database and backend containers. To start everything you need to docker-compose --profile backend --profile frontend up or use a single profile but several times.

That seems to me as the best way to make docker-compose not to run certain containers. You just need to mark them with a profile and it's done. I suggest you give the profiles reference a second chance as well. Apart from some good examples it explains how the feature interacts with service dependencies.

Upvotes: 17

Related Questions