【问题标题】:How to rebuild docker container in docker-compose.yml?如何在 docker-compose.yml 中重建 docker 容器?
【发布时间】:2016-08-21 11:11:02
【问题描述】:

在 docker-compose.yml 中定义了服务范围。这些服务已经启动。我只需要重建其中一个并在没有其他服务的情况下启动它。 我运行以下命令:

docker-compose up -d # run all services
docker-compose stop nginx # stop only one. but it still running !!!
docker-compose build --no-cache nginx 
docker-compose up -d --no-deps # link nginx to other services

最后我得到了旧的 nginx 容器。 顺便说一句,docker-compose 不会杀死所有正在运行的容器!

【问题讨论】:

  • docker-compose up --build
  • 只是为了澄清上面的评论:docker-compose up --build重建所有容器。使用@denov 评论中所述的docker-compose up --build <service_name>
  • docker-compose up --build <service_name> 如果您的 docker-compose.yml 包含来自容器注册表的容器,则无法使用
  • docker-compose build --no-cache 当您想从第一级构建时。
  • 应该很容易理解这些语义(不是你@yuklia - 撰写团队)。不知道为什么我们在这个千年里仍然在为此苦苦挣扎。

标签: docker docker-compose docker-container


【解决方案1】:

docker-compose up

$ docker-compose up -d --no-deps --build <service_name>

--no-deps - 不启动链接服务。

--build - 在启动容器之前构建镜像。

【讨论】:

  • @evgpisarchik 命令有点出,应该是docker-compose build --no-cache service1 service2
  • 为什么你们都在谈论服务?问题是重建容器而不是服务
  • @temirbek 容器内 compose 定义在 services 块下
【解决方案2】:

使用 docker-compose 1.19 up

docker-compose up --build --force-recreate --no-deps [-d] [<service_name>..]

如果没有一个或多个 service_name 参数,所有图像都会在丢失时被构建,并且所有容器都将被重新创建。

从帮助菜单

Options:
    -d, --detach        Detached mode: Run containers in the background,
                        print new container names. Incompatible with
                        --abort-on-container-exit.
    --no-deps           Don't start linked services.
    --force-recreate    Recreate containers even if their configuration
                        and image haven't changed.
    --build             Build images before starting containers.

无缓存

要强制重建忽略缓存层,我们必须首先build 一个新图像

docker-compose build --no-cache [<service_name>..]

从帮助菜单

Options:
    --force-rm              Always remove intermediate containers.
    -m, --memory MEM        Set memory limit for the build container.
    --no-cache              Do not use cache when building the image.
    --no-rm                 Do not remove intermediate containers after a successful build.

然后重新创建容器

docker-compose up --force-recreate --no-deps [-d] [<service_name>..]

【讨论】:

  • 我很困惑这如何回答这个问题。我们如何只重建一个容器?
  • 您只能通过将容器名称附加到命令末尾来重建一个容器。 docker-compose up -d --force-recreate --build container_name
  • 等一下,这个重新创建容器,不等同于build --no-cache选项,除非命令描述完全错误
【解决方案3】:

这应该可以解决您的问题:

docker-compose ps # lists all services (id, name)
docker-compose stop <id/name> #this will stop only the selected container
docker-compose rm <id/name> # this will remove the docker container permanently 
docker-compose up # builds/rebuilds all not already built container 

【讨论】:

  • 如果你想stoprm所有容器,那么你可以使用命令docker-compose down。如果您只想摆脱一些,您的解决方案会更好。
  • 此解决方案非常适合您不想停止整个 docker 应用程序而只是重新创建服务之一的设置
  • 我需要在up 之前实际运行docker-compose build &lt;id/name&gt; 来重建更改后的Dockerfile。
【解决方案4】:

正如@HarlemSquirrel 发布的那样,这是最好的,我认为是正确的解决方案。

但是,要回答 OP 特定的问题,它应该类似于以下命令,因为他不想重新创建 docker-compose.yml 文件中的所有服务,而只想重新创建 nginx 一个:

docker-compose up -d --force-recreate --no-deps --build nginx

选项说明:

Options:
  -d                  Detached mode: Run containers in the background,
                      print new container names. Incompatible with
                      --abort-on-container-exit.
  --force-recreate    Recreate containers even if their configuration
                      and image haven't changed.
  --build             Build images before starting containers.
  --no-deps           Don't start linked services.

【讨论】:

    【解决方案5】:

    也许这些步骤不太正确,但我确实喜欢这样:

    停止 docker 撰写:$ docker-compose down

    警告:以下prune -a 将删除所有图像,您可能不希望这样做,因为它可能会影响其他项目。你可以阅读更多here

    移除容器:$ docker system prune -a

    启动 docker compose:$ docker-compose up -d

    【讨论】:

    • docker system prune -a 将删除所有图像,甚至是其他项目的图像。我建议不要使用它。
    • aL_eX, flag -a(表示全部)您可以通过指定其 id 仅选择您需要的一个
    • @aL_eX 我同意这种方法有点危险。但这是肯定的。其他答案不会删除图像。
    • 我编辑了您的答案,添加了警告。
    • Brandon Bertelsen,好的,但是要删除所有图像,请使用 -a (--all) -“删除所有未使用的图像,而不仅仅是悬挂的图像”
    【解决方案6】:
    docker-compose stop nginx # stop if running
    docker-compose rm -f nginx # remove without confirmation
    docker-compose build nginx # build
    docker-compose up -d nginx # create and start in background
    

    使用 rm 删除容器是必不可少的。不移除,Docker 将启动旧容器。

    【讨论】:

      【解决方案7】:

      对我来说,它只从 Docker Hub 中获取了 --no-cache--pull 的新依赖项(可用于 docker-compose build

      # other steps before rebuild
      docker-compose build --no-cache --pull nginx # rebuild nginx
      # other steps after rebuild, e.g. up (see other answers)
      

      【讨论】:

        【解决方案8】:

        只需使用:

        docker-compose build [yml_service_name]
        

        [yml_service_name] 替换为docker-compose.yml 文件中的服务名称。您可以使用docker-compose restart 确保更改生效。您可以使用--no-cache 忽略缓存。

        【讨论】:

          【解决方案9】:

          问题是:

          $ docker-compose stop nginx
          

          没有工作(你说它仍在运行)。如果你还是要重建它,你可以尝试杀死它:

          $ docker-compose kill nginx
          

          如果还是不行,尝试直接用docker停止:

          $ docker stop nginx
          

          或删除它

          $ docker rm -f nginx
          

          如果这仍然不起作用,请检查您的 docker 版本,您可能需要升级。

          这可能是一个错误,您可以检查一个是否与您的系统/版本匹配。这是一对,例如: https://github.com/docker/docker/issues/10589

          https://github.com/docker/docker/issues/12738

          作为一种解决方法,您可以尝试终止该进程。

          $ ps aux | grep docker 
          $ kill 225654 # example process id
          

          【讨论】:

            【解决方案10】:

            仅:

            $ docker-compose restart [yml_service_name]
            

            【讨论】:

            • 这不会产生新的变化。来自手册 - 如果您对 docker-compose.yml 配置进行更改,这些更改将不会在运行此命令后反映出来。
            猜你喜欢
            • 1970-01-01
            • 2015-12-30
            • 2020-06-02
            • 1970-01-01
            • 2018-05-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-06
            相关资源
            最近更新 更多