首页 > 解决方案 > Docker Compose 总是强制构建一个服务

问题描述

这是我的 docker-compose 文件的一部分:

version: "3.2"
services:
  cachable_service:
    image: my/cachable_service:x.x
    build:
      context: .
      dockerfile: cachable_service_Dockerfile

  service_in_development:
    image: my/service_in_development:latest
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - cachable_service

如何仅强制service_in_development始终从头开始构建图像,以便在那里获取最新代码?

我已经查看cache_from了文件参考中的选项docker-compose,但我找不到任何关于它到底做什么的明确解释。

说明:

在这个问题中,我问的是使用docker-compose.yml文件本身中的选项强制仅重建特定服务的选项。但是,如果命令行选项让我这样做,我现在也会很高兴。

另外,我说的是强制“重建”图像。不仅仅是服务的“娱乐”。

标签: dockerdocker-compose

解决方案


您可以使用以下命令重建所有图像:

docker-compose up --build --force-recreate

强制重建一项服务:

docker-compose up --build --force-recreate service-name-here

文档中更有用的参数

Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...]

Options:
    -d, --detach               Detached mode: Run containers in the background,
                               print new container names. Incompatible with
                               --abort-on-container-exit.
    --no-color                 Produce monochrome output.
    --quiet-pull               Pull without printing progress information
    --no-deps                  Don't start linked services.
    --force-recreate           Recreate containers even if their configuration
                               and image haven't changed.
    --always-recreate-deps     Recreate dependent containers.
                               Incompatible with --no-recreate.
    --no-recreate              If containers already exist, don't recreate
                               them. Incompatible with --force-recreate and -V.
    --no-build                 Don't build an image, even if it's missing.
    --no-start                 Don't start the services after creating them.
    --build                    Build images before starting containers.
    --abort-on-container-exit  Stops all containers if any container was
                               stopped. Incompatible with -d.
    -t, --timeout TIMEOUT      Use this timeout in seconds for container
                               shutdown when attached or when containers are
                               already running. (default: 10)
    -V, --renew-anon-volumes   Recreate anonymous volumes instead of retrieving
                               data from the previous containers.
    --remove-orphans           Remove containers for services not defined
                               in the Compose file.
    --exit-code-from SERVICE   Return the exit code of the selected service
                               container. Implies --abort-on-container-exit.
    --scale SERVICE=NUM        Scale SERVICE to NUM instances. Overrides the
                               `scale` setting in the Compose file if present.

推荐阅读