在 Docker 中,容器和镜像有什么区别? [英] In Docker, what's the difference between a container and an image?
问题描述
Docker 中的容器和镜像有什么区别?在 Docker 入门教程 中都使用了这些术语,但我不明白它们的区别.
What's the difference between a container and an image in Docker? In the Get started with Docker tutorial these terms are both used, but I do not understand the difference.
有人能解释一下吗?
推荐答案
图像是活动容器的冻结不可变快照.容器正在运行(或停止)某个镜像的实例.
Images are frozen immutable snapshots of live containers. Containers are running (or stopped) instances of some image.
从名为ubuntu"的基础镜像开始.让我们在 ubuntu 镜像中交互运行 bash 并创建一个文件.我们将使用 -i
和 -t
标志为我们提供一个交互式 bash shell.
Start with the base image called 'ubuntu'. Let's run bash interactively within the ubuntu image and create a file. We'll use the -i
and -t
flags to give us an interactive bash shell.
$ docker run -i -t ubuntu /bin/bash
root@48cff2e9be75:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@48cff2e9be75:/# cat > foo
This is a really important file!!!!
root@48cff2e9be75:/# exit
不要期望在您退出并重新启动映像时该文件会一直存在.您正在从与之前开始时完全相同的定义状态重新开始,而不是从您停止的地方重新开始.
Don't expect that file to stick around when you exit and restart the image. You're restarting from exactly the same defined state as you started in before, not where you left off.
$ docker run -i -t ubuntu /bin/bash
root@abf181be4379:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@abf181be4379:/# exit
但是,现在不再运行的容器具有状态并且可以保存(提交)到图像中.
But, the container, now no longer running, has state and can be saved (committed) to an image.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abf181be4379 ubuntu:14.04 /bin/bash 17 seconds ago Exited (0) 12 seconds ago elegant_ardinghelli
48cff2e9be75 ubuntu:14.04 /bin/bash About a minute ago Exited (0) 50 seconds ago determined_pare
...
让我们从创建文件的容器 ID 48cff2e9be75 创建一个图像:
Let's create an image from container ID 48cff2e9be75 where we created our file:
$ docker commit 48cff2e9be75 ubuntu-foo
d0e4ae9a911d0243e95556e229c8e0873b623eeed4c7816268db090dfdd149c2
现在,我们有了一个包含我们非常重要的文件的新图像:
Now, we have a new image with our really important file:
$ docker run ubuntu-foo /bin/cat foo
This is a really important file!!!!
尝试命令 docker images
.您应该会看到您的新图像 ubuntu-foo
与我们开始使用的 ubuntu
标准图像一起列出.
Try the command docker images
. You should see your new image ubuntu-foo
listed along with the ubuntu
standard image we started with.
这篇关于在 Docker 中,容器和镜像有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!