skip to Main Content

My docker-compose.yml:

services:
  service1:
    image: image1
  service2:
    image: image2
  service3:
    image: image3
    deploy:
      replicas: 0

The replicas: 0 ensures the service only starts on demand: I don’t want to it to start automatically.

Assuming that compose project isn’t running; I want to start service3 only:

docker compose up -d --scale service3=1

That starts service3, but also service1 and service2.

How can I start service3 only?

Note:

  • docker swarm is not an option in this case
  • docker profiles are not an option in this case (it has some issues)

2

Answers


  1. You have to use the docker compose up command:

    docker compose up service3
    

    or, docker compose start command if the container already exist:

    docker compose start service3 
    

    In case you don’t want to re-create services, as workaround, you can either set the replicas also on the other services by adding to service1 and service2:

    deploy:
      replicas: 0
    

    or use the –scale option

    docker compose up --scale service3=1 --scale service1=0 --scale service2=0
    
    Login or Signup to reply.
  2. If you want to only start one service when using docker compose up, you have to specify the said service as an argument of your command, as shown in the help of the command:

    $ docker compose up –help

    Usage: docker compose up [OPTIONS] [SERVICE…]

    So, in your case you have to do:

    docker compose up --detach --scale service3=1 service3
    

    If we split it down:

    docker compose up 
      --detach 
      --scale service3=1 
    #         ^-- overrides the scaling of the compose file
      service3
    # ^-- specify which service should start
    

    Which gives:

    $ docker compose up --detach --scale service3=1 service3
    
    [+] Running 1/1
     ✔ Container stackoverflow-service3-1  Started                 0.3s 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search