防止容器启动

时间:2021-01-18 17:46:02

标签: docker-compose

我有这个码头工人:

  myservice:
    restart: "no"

使用 no 服务无论如何都会启动(但不会重新启动)

如何完全阻止服务启动?

注意:对于那些好奇的人,我想这样做的原因是我想通过 env var 使这个标志可配置:

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

然后传递正确的值来启动服务。

1 个答案:

答案 0 :(得分:1)

<块引用>

Docker 提供重启策略来控制您的容器是在退出时自动启动,还是在 Docker 重启时自动启动。

所以只有当容器退出或Docker重启时。

但是您有两种选择:

首先只启动你想要的服务:

docker-compose up other-service

这不会随心所欲地使用 ENV(除非您有运行 docker-compose up 的脚本)。

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

但正如前面提到的 herehere,您可以覆盖 entrypoint

因此您可以执行以下操作:

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

和 start.sh 类似:

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