Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent container startup

I have this docker compose:

  myservice:
    restart: "no"

With no the service will start anyway (but it won't restart)

How can I prevent the service to start at all?

Note: the reason I want to do this, for those curious, is that I want to make this flag configurable via an env var:

  myservice:
    restart: "${RESTART_SERVICE:-no}"

And then pass the right value to start the service.

like image 689
blueFast Avatar asked Dec 07 '25 03:12

blueFast


1 Answers

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

  • https://docs.docker.com/config/containers/start-containers-automatically/

So it's only when containers exit or when Docker restart.

But you have two options for what you want to do:

First only start the service you want:

docker-compose up other-service

This don't use ENV as you want (unless you have a script for run the docker-compose up ).

if [[ $START == true ]]; then
  docker-compose up
else
  docker-compose up other-service
fi

But as mentioned here and here, you can overwrite the entrypoint

So you can do something like:

services:
  alpine:
    image: alpine:latest
    environment:
      - START=false
    volumes:
      - ./start.sh:/start.sh
    entrypoint: ['sh', '/start.sh']

And and start.sh like:

if [ $START == true ]; then
  echo ok # replace with the original entrypoint or command
else
  exit 0
fi
# START=false in the docker-compose
$ docker-compose up 
Starting stk_alpine_1 ... done
Attaching to stk_alpine_1
stk_alpine_1 exited with code 0

$ sed -i 's/START=false/START=true/' docker-compose.yml 

$ docker-compose up 
Starting stk_alpine_1 ... done
Attaching to stk_alpine_1
alpine_1  | ok
stk_alpine_1 exited with code 0
like image 71
Dr Claw Avatar answered Dec 09 '25 19:12

Dr Claw