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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With