cclloyd
cclloyd

Reputation: 9205

Docker-compose exclude service by default

If I have a great many services defined in a docker-compose project, how can I exclude a service from the default docker-compose up command?

For example, I have an nginx service, and an ssl service. They conflict because they both consume port 80, so how can I make it so that the ssl service doesn't start unless I specifically run that service, and by default the up command will do everything EXCEPT the ssl service?

Upvotes: 91

Views: 47920

Answers (3)

Entity Black
Entity Black

Reputation: 3491

This answer predates the introduction of service profiles, in Docker 1.28 (released January 2021).


I stumbled upon this request for enhancement on GitHub today: Define services which are not started by default (issue 1896)

Well, you can't. But I have done this workaround since I have quite a lot of services and am adding more. I did a dummy service with just some dummy image and custom command and then I set my desired services as dependencies:

  main-services:
    image: docker4w/nsenter-dockerd # you want to put there some small image
    command: sh -c "echo start"
    depends_on:
      - postgres
      - consumer-backend
      - prisma
      - api-gateway
      - adminer
      - frontend

Then I wrote into my readme.md:

Use this to run everything
docker-compose up -d main-services

Create database structures
docker-compose up deploy

Seed database with data
docker-compose up seed

Using dummy services to group things was easier than anything else I have seen so far.

Upvotes: 26

acran
acran

Reputation: 8988

Starting with docker-compose 1.28.0 the new service profiles are just made for that! With profiles you can mark services to be only started in specific profiles, for example:

services:
  webapp:
    # ...

  nginx:
    # ...
    profiles: ["nginx"]
    ports:
      - 80:80

  ssl:
    # ...
    profiles: ["ssl"]
    ports:
      - 80:80
docker-compose up # start only your webapp services
docker-compose --profile nginx up # start the webapp and nginx service
docker-compose --profile ssl up # start the webapp and ssl service
docker-compose run ssl # run the ssl service

Depending on your exact use case/setup however it may be better to split your services into multiple docker-compose.yml files.

Upvotes: 135

b0gusb
b0gusb

Reputation: 4721

Not exactly what you're looking for but you could use the scale flag to scale your ssl service to zero:

docker-compose up --scale ssl_service=0

Another option is to have multiple compose files and run docker with the -f flag to start specific services.

Upvotes: 42

Related Questions