如何在docker-compose.yml中重建docker容器?

问题描述 投票:113回答:6

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 docker-compose docker-container
6个回答
162
投票

docker-compose up

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

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

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


103
投票

用docker-compose 1.19

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

从帮助菜单中

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.

42
投票

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

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 

29
投票

正如@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.

2
投票

问题是:

$ 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

1
投票

只需使用:

docker-compose build [yml_service_name]

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


-4
投票

只要:

$ docker-compose restart [yml_service_name]
© www.soinside.com 2019 - 2024. All rights reserved.