首页 文章

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

提问于
浏览
105

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不会杀死所有正在运行的容器!

6 回答

  • 11

    docker-compose up

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

    --no-deps - 不要启动链接服务 . --build - 在启动容器之前构建映像 .

  • 124

    用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.
    
  • 2

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

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

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

    问题是:

    $ 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
    
  • 32

    只要:

    $ docker-compose restart [yml_service_name]
    

相关问题