FG
☁️ Cloud & DevOpsDocker

Define services which are not started by default

Freshabout 19 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

Users quite often define maintenance scripts, test suites, debugging systems in their Compose files which they don't want to run when they do `docker-compose up`. There should be some way of defining what services are started by default, but can be still be run manually by doing `docker-compose up servicename` or `docker-compose run servicename ...`. Possible solutions 1) Recommend users to use a separate Compose file 2) Add an option to services to make them not start by default 3) Add a top-level configuration option to define the default services 4) Add a concept of a thing like a service, but is just for one-off commands ("scripts", "tasks", etc...) (Please suggest others if you have ideas.) Data points: - https://github.com/docker/compose/issues/697 - https://github.com/docker/compose/issues/912 - https://github.com/docker/compose/issues/942 - https://github.com/docker/compose/issues/1439 - https://github.com/docker/compose/issues/1547 - #542 - "test" service in https://github.com/heroku/logplex/blob/master/docker-compose.yml - ~~@cpuguy83: "I've got a little helper service in my compose yaml that injects a bunch of stuff into redis for hipache, which of course exits when it is done. Can't use `compose up` w/o the `-d`"~~ this is fixed now that we don't exit from `compose up` until all services have exited.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Introduce 'start_by_default' Option for Docker Compose Services

Medium Risk

Currently, all services defined in a Docker Compose file are started by default when executing 'docker-compose up'. This behavior is problematic for users who have maintenance scripts, test suites, or debugging systems that they do not want to run automatically. The lack of a mechanism to specify which services should start by default leads to confusion and unintended executions.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Define 'start_by_default' Option in Compose File

    Modify the Docker Compose file schema to include a new boolean option 'start_by_default' for each service. If set to false, the service will not start when 'docker-compose up' is executed.

    yaml
    version: '3.8'
    services:
      web:
        image: my-web-app
        start_by_default: true
      test:
        image: my-test-image
        start_by_default: false
  2. 2

    Update Docker Compose Logic to Respect 'start_by_default'

    Modify the Docker Compose command logic to check the 'start_by_default' option for each service. Only include services with 'start_by_default: true' in the list of services to start when 'docker-compose up' is called.

    typescript
    // Pseudocode for Docker Compose logic
    if service.start_by_default:
        start_service(service)
  3. 3

    Document the New Option in Docker Compose Documentation

    Update the official Docker Compose documentation to include information about the new 'start_by_default' option. Provide examples and best practices for its use to ensure users understand how to implement it effectively.

    markdown
    # Documentation Example
    ## start_by_default
    
    This option allows you to specify whether a service should start by default.
    
    ```yaml
    services:
      my_service:
        image: my-image
        start_by_default: false
    ```
    
  4. 4

    Test the Implementation

    Create unit tests to verify that services with 'start_by_default: false' do not start when 'docker-compose up' is executed. Ensure that they can still be run manually using 'docker-compose up servicename' or 'docker-compose run servicename'.

    javascript
    describe('Docker Compose', () => {
      it('should not start services with start_by_default: false', () => {
        // Test logic here
      });
    });

Validation

To confirm the fix worked, create a Docker Compose file with multiple services, some with 'start_by_default: true' and others with 'start_by_default: false'. Run 'docker-compose up' and verify that only the services marked true are started. Additionally, check that services marked false can still be started individually.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

dockerdocker-composecontainerskind/feature