docker service. make it not start at docker compose up.

docker compose disable service auto-start configuration

Refined the query to focus on disabling the auto-start feature for a service in Docker Compose, using clearer and more specific terminology to improve search relevance.

Disabling the automatic start of a service in Docker Compose during the docker-compose up command can be crucial for various development scenarios, especially when you may want to control the specific services running at any given time. Below are detailed methods and strategies to achieve this.

Understanding Docker Compose Service Management

When you run docker-compose up, all services defined in your docker-compose.yml file are started by default. However, you may want to prevent certain services from starting automatically for reasons such as debugging, resource management, or selective development.

Options to Prevent Automatic Startup

1. Remove the Service from docker-compose.yml Temporarily

One of the simplest approaches is to comment out or temporarily remove the service from your docker-compose.yml file. For instance:

version: '3'
services:
  web:
    image: nginx
  # db:
  #   image: postgres

This way, when you run docker-compose up, the db service won’t start since it is not defined in your configuration.

2. Using Docker Compose Profiles (V2.1 and Later)

Docker Compose introduced profiles to manage services better. You can define which services to include while running the command. Here’s how you can set this up:

version: '3.9'
services:
  web:
    image: nginx
    profiles:
      - frontend

  db:
    image: postgres
    profiles:
      - backend

You can start the web service without the db service by running:

docker-compose --profile frontend up

When using profiles, you can seamlessly toggle between which services to run without needing to modify the file directly.

3. Using the --no-start Option

Another approach is using the Docker CLI directly by creating services without starting them. You could use:

docker-compose create db

This command creates the service but does not start it. It allows you to later selectively start services:

docker-compose start db

4. Modify Restart Policies

If you do not want a service to restart automatically if it exits, ensure you set appropriate restart policies. By default, services may restart under certain conditions. Setting a “never” restart policy can help control unexpected starts, thus:

version: '3.8'
services:
  web:
    image: nginx
    restart: "no"

This adjustment, however, only prevents the services from restarting under defined circumstances and not from starting during docker-compose up itself.

5. Custom Entry Scripts

In advanced scenarios, you may want to create custom entry scripts that check the conditions under which they should start, thus allowing more granular control over what begins when.

Conclusion

To prevent a service from automatically starting during docker-compose up, you can either comment out the services, utilize profiles, create without starting, adjust restart policies, or employ custom entry scripts. Each of these methods allows for varying levels of control suitable for different development needs.

If you regularly find yourself needing to enable or disable services dynamically, consider adopting profiles as a long-term strategy, as it provides a flexible and clean solution for managing service states in Docker Compose.

For further reading on managing services and understanding Docker Compose better, refer to the official Docker documentation.

Related Searches

Sources

10
1
Disable autostart of docker-compose project - Stack Overflow
Stack Overflow

I have a docker-compose project using Docker for Mac that autostarts when I boot the computer. I usually start the project with docker-compose up -d.

2
Disabling automatic boot on certain docker container in docker ...
Community

Configuring Automatic Start: For the containers you want to start automatically, use the restart policy in your docker-compose.yml file. Set it ...

3
Is there a way to disable containers start automatically when docker ...
Forums

How can I disable the container start automatically when docker-daemon not started? modify the container config file to disable? where is the ...

4
Give services an option to disable automatic startup on "up" #3996
GitHub

Suggestion: Add a docker-compose.yml service configuration option auto_up: BOOL (or whatever you want to call it) that, when false (default ...

5
Services | Docker Docs
Docs

Compose guarantees dependency services have been started before starting a dependent service. Compose waits for dependency services to be "ready" before ...

6
Disable Docker autostart at boot? - Ask Ubuntu
Askubuntu

To disable on boot, use systemctl disable as @Burak said: sudo systemctl disable docker.service docker.socket This will have the side-effect of stopping Docker ...

7
Is there a way to stop Docker Desktop from starting containers ...
Reddit

Remove the flag or use a more specific restart policy. You can also combine restart_policy with depends_on for more options.

8
Start containers automatically - Docker Docs
Docs

Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts.

9
Is it possible to stop docker compose from creating a default network
Forums

By default docker creates a default network when I spin up my 3 containers using docker compose up . Is it possible to stop this from happening ...

10
How to Disable Auto-Start for Docker Containers - Khuong Dev
Dev

1. Check Running Containers · 2. Inspect the Restart Policy · 3. Update the Restart Policy · 4. Confirm Changes · 5. Reboot and Test.