在不停止容器的情况下复制/更新Docker容器中的代码 [英] Copy / update the code in docker container without stopping container

查看:213
本文介绍了在不停止容器的情况下复制/更新Docker容器中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个docker-composer设置,其中我正在上传服务器的数据,例如flask api.现在,当我更改python代码时,我必须遵循这样的步骤

I have a docker-composer setup in which i am uploading source code for server say flask api . Now when i change my python code, I have to follow steps like this

  1. 停止正在运行的容器(docker-compose stop)
  2. 在容器中构建并加载更新的代码(docker-compose up --build)

这需要一些时间.有什么更好的办法吗?像在正在运行的docker中更新代码,然后在不停止整个容器的情况下重新启动Apache服务器?

This take a bit long time . Is there any better way ? Like update code in the running docker and then restarting Apache server without stopping whole container ?

推荐答案

您可以在旧容器仍在运行时运行 docker build ,并且停机时间仅限于转换期.

You can run the docker build while the old container is still running, and your downtime is limited to the changeover period.

将负载平衡器放在容器前面可能有两个原因,这可能会有所帮助.根据您的应用程序,它可能是像HAProxy这样的哑"负载平衡器,或是像nginx这样的完整Web服务器,或者是您的云提供商可以使用的东西.这使您可以一次在多个主机上运行该应用程序的多个副本(有助于扩展和提高可靠性).在这种情况下,序列变为:

It can be helpful for a couple of reasons to put a load balancer in front of your container. Depending on your application this could be a "dumb" load balancer like HAProxy, or a full Web server like nginx, or something your cloud provider makes available. This allows you to have multiple copies of the application running at once, possibly on different hosts (helps for scaling and reliability). In this case the sequence becomes:

  1. docker build 新映像
  2. docker run
  3. 将其连接到负载平衡器(现在,流量将流向新旧容器)
  4. 测试新容器是否正常工作
  5. 从负载均衡器上卸下旧容器
  6. docker stop&&docker rm 旧容器
  1. docker build the new image
  2. docker run it
  3. Attach it to the load balancer (now traffic goes to both old and new containers)
  4. Test that the new container works correctly
  5. Detach the old container from the load balancer
  6. docker stop && docker rm the old container

如果您不介意使用重量级的基础架构,那么此顺序基本上就是您更改Kubernetes部署对象中的图像标签时发生的事情,但是采用Kubernetes是一项实质性的承诺.

If you don't mind heavier-weight infrastructure, this sequence is basically exactly what happens when you change the image tag in a Kubernetes Deployment object, but adopting Kubernetes is something of a substantial commitment.

这篇关于在不停止容器的情况下复制/更新Docker容器中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆