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

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

最后,我得到了旧的nginx容器。 Docker-compose不会杀死所有正在运行的容器!


当前回答

docker-compose上升1.19

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.

没有缓存

为了强制重建忽略缓存层,我们必须首先构建一个新映像

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上升1.19

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.

没有缓存

为了强制重建忽略缓存层,我们必须首先构建一个新映像

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 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 

你可以使用:

docker-compose build

如果你正在使用docker配置文件:

docker-compose --profile profile_name build

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

停止docker compose: $ docker-compose down

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

删除容器:$ docker system prune -a

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

简单地使用:

docker-compose build [yml_service_name]

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