简体   繁体   English

我应该创建一个 docker 容器还是 docker 启动一个停止的容器?

[英]Should I create a docker container or docker start a stopped container?

From the docker philosophy's point of view it is more advisable:从 docker 哲学的角度来看,它更可取:

  1. create a container every time we need to use a certain environment and then remove it after use ( docker run <image> all the time);每次我们需要使用某个环境时创建一个容器,然后在使用后将其删除( docker run <image> ); or或者
  2. create a container for a specific environment ( docker run <image> ), stop it when it is not necessary and whenever it is initialized again ( docker start <container> );为特定环境创建一个容器( docker run <image> ),在不需要时停止它,并且每当它再次初始化时( docker start <container> );

I'm talking with regards to my experience in the industry so take my answer with a grain of salt, because there might be no hard evidence or reference to the theory.我说的是我在这个行业的经验,所以我的回答持保留态度,因为可能没有确凿的证据或对理论的参考。

Here's the answer:这是答案:

TL;DR: In short, you never need the docker stop and docker start because taking this approach is unreliable and you might lose the container and all the data inside if no proper action is applied beforehand. TL;DR:简而言之,您永远不需要docker stopdocker start ,因为采用这种方法是不可靠的,如果事先没有采取适当的措施,您可能会丢失容器和里面的所有数据。

Long answer: You should only work with images and not the containers.长答案:您应该只使用图像而不是容器。 Whenever you need some specific data or you need the image to have some customization, you better use docker save to have the image for future use.每当您需要一些特定数据或需要对图像进行一些自定义时,您最好使用docker save图像以备将来使用。

If you're just testing out on your local machine, or in your dev virtual machine on a remote host, you're free to use either one you like.如果您只是在本地机器上进行测试,或者在远程主机上的开发虚拟机中进行测试,您可以随意使用您喜欢的任何一个。 I personally take each of the approaches on different scenarios.我个人在不同的情况下采用每种方法。

But if you're talking about a production environment, you'd better use some orchestration tool;但是如果你说的是生产环境,你最好使用一些编排工具; it could be as simple and easy to work with as docker-compose or docker swarm or even Kubernetes on more complex environments.在更复杂的环境中,它可以像docker-composedocker swarm甚至Kubernetes一样简单易用。

You better not take the second approach ( docker run , docker stop & docker start ) in those environments because at any moment in time you might lose that container and if you are solely dependent on that specific container or it's data, then you're gonna have a bad weekend.您最好不要在这些环境中采用第二种方法( docker rundocker stopdocker start ),因为在任何时候您都可能会丢失该容器或仅依赖于特定容器的数据有一个糟糕的周末。

If you docker rm the old container and docker run a new one, you will always get a clean filesystem that starts from exactly what's in the original image (plus any volume mounts).如果您docker rm旧容器和docker run新容器,您将始终获得一个干净的文件系统,该文件系统完全从原始映像中的内容开始(加上任何卷安装)。 You will also fairly routinely need to delete and recreate a container to change basic options: if you need to change a port mapping or an environment variable, or if you need to update the image to have a newer version of the software, you'll be forced to delete the container.您还经常需要删除并重新创建一个容器来更改基本选项:如果您需要更改端口映射或环境变量,或者如果您需要更新映像以拥有更新版本的软件,您将强制删除容器。

This is enough reason for me to make my standard process be to always delete and recreate the container.这足以让我让我的标准流程始终删除并重新创建容器。

# docker build -t the-image .  # can be done first if needed
docker stop the-container      # so it can cleanly shut down and be removed
docker rm the-container
docker run --name the-container ... the-image

Other orchestrators like Docker Compose and Kubernetes are also set up to automatically delete and recreate the container (or Kubernetes pod) if there's a change;其他编排器,如 Docker Compose 和 Kubernetes 也设置为自动删除和重新创建容器(或 Kubernetes pod),如果有更改; their standard workflows do not generally involve restarting containers in-place.他们的标准工作流程通常不涉及就地重启容器。

I almost never use docker start .我几乎从不使用docker start In a Compose-based workflow I generally use only docker-compose up -d , letting it restart things if needed;在基于 Compose 的工作流程中,我通常只使用docker-compose up -d ,让它在需要时重新启动; docker-compose down if I need the CPU/memory resources the container stack was using but not in routine work.如果我需要容器堆栈正在使用但在日常工作中不需要的 CPU/内存资源, docker-compose down

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM