运行和启动Docker容器之间的区别

msk*_*skw 260 docker

在实践中我开始一个容器:

docker run a8asd8f9asdf0
Run Code Online (Sandbox Code Playgroud)

如果是这样的话,那会是什么:

docker start
Run Code Online (Sandbox Code Playgroud)

做?

在手册中说

启动一个或多个已停止的容器

dan*_*004 289

这是一个非常重要的问题,答案非常简单,但基本:

  1. 运行:创建图像的新容器,然后执行容器.您可以创建相同图像的N个克隆.命令是: docker run IMAGE_ID 而不是 docker run CONTAINER_ID

在此输入图像描述

  1. 开始:启动先前停止的容器.例如,如果使用该命令停止了数据库,则docker stop CONTAINER_ID可以使用该命令重新启动同一容器docker start CONTAINER_ID,并且数据和设置将相同.

在此输入图像描述

  • 是否需要为停止的容器创建卷以使数据持久化? (7认同)
  • @LoganPhillips Lifecycle 写入容器默认 _union_ 文件系统层的文件与写入卷的文件之间的区别是:删除容器时,容器的联合文件系统层数据总是丢失(`docker rm container_id`)。另一方面,除非在命令行中明确提供了“-v”选项,否则卷数据在容器被移除后仍然存在。可以直接检查主机系统上的卷位置。[见本文](https://container-solutions.com/understanding-volumes-docker/) (2认同)

Von*_*onC 95

  • run运行图像
  • start开始一个容器.

docker run文件确实提到:

docker run命令首先在指定的图像上创建一个可写容器层,然后使用指定的命令启动它.

也就是说,码头工人跑相当于API /containers/create,然后/containers/(id)/start.

没有运行现有的容器,你可以使用docker exec(因为docker 1.3).
您可以重新启动已退出的容器.

  • 似乎有人有空间写一个更精细的回复.答案看起来不太清楚. (8认同)
  • 对我来说,下一个问题是容器和图像之间的差异http://stackoverflow.com/questions/21498832/in-docker-whats-the-difference-between-a-container-和一个图像 (4认同)

y.s*_*hyk 16

run command creates a container from the image and then starts the root process on this container. Running it with run --rm flag would save you the trouble of removing the useless dead container afterward and would allow you to ignore the existence of docker start and docker remove altogether.

在此处输入图片说明

run command does a few different things:

docker run --name dname image_name bash -c "whoami"
Run Code Online (Sandbox Code Playgroud)
  1. Creates a Container from the image. At this point container would have an id, might have a name if one is given, will show up in docker ps
  2. Starts/executes the root process of the container. In the code above that would execute bash -c "whoami". If one runs docker run --name dname image_name without a command to execute container would go into stopped state immediately.
  3. Once the root process is finished, the container is stopped. At this point, it is pretty much useless. One can not execute anything anymore or resurrect the container. There are basically 2 ways out of stopped state: remove the container or create a checkpoint (i.e. an image) out of stopped container to run something else. One has to run docker remove before launching container under the same name.

How to remove container once it is stopped automatically? Add an --rm flag to run command:

docker run --rm --name dname image_name bash -c "whoami"
Run Code Online (Sandbox Code Playgroud)

How to execute multiple commands in a single container? By preventing that root process from dying. This can be done by running some useless command at start with --detached flag and then using "execute" to run actual commands:

docker run --rm -d --name dname image_name tail -f /dev/null
docker exec dname bash -c "whoami"
docker exec dname bash -c "echo 'Nnice'"
Run Code Online (Sandbox Code Playgroud)

Why do we need docker stop then? To stop this lingering container that we launched in the previous snippet with the endless command tail -f /dev/null.


小智 15

用例子说明:

假设您的计算机中有游戏(iso)图像.

当您run(将图像作为虚拟驱动器安装)时,将创建一个虚拟驱动器,其中包含虚拟驱动器中的所有游戏内容,并自动启动游戏安装文件.[运行您的泊坞窗图像 - 创建容器然后启动它.]

但是当你stop(类似于docker stop)它时,虚拟驱动器仍然存在但停止所有进程.[由于容器存在,直到它不被删除]

当你这样做start(类似于docker start)时,从虚拟驱动器开始执行游戏文件.[启动现有容器]

在此示例中 - 游戏图像是您的Docker镜像,虚拟驱动器是您的容器.

  • @ChamithraThenuwara:因为“docker run”默认在前台运行(使用“-d”将其分离),而“docker start”默认在后台运行(使用“-a”在前台运行)。因此,“docker start -a 27c833038489”将打印 _Hell from Docker_ 消息。一致性就这么多了,是吗? (5认同)

Bow*_*wen 6

daniele3004的答案已经很不错了。

只是一个快速和肮脏的公式我这样的人谁混淆了run,并start时不时:

docker run [...]= docker pull [...]+docker start [...]

  • 这并不完全正确。根据官方文档,“...docker run 相当于 API /containers/create 然后 /containers/(id)/start”。(来源:https://docs.docker.com/engine/reference/命令行/运行/) (3认同)