在Docker中,容器和图像之间有什么区别? [重复]

本文翻译自:In Docker, what's the difference between a container and an image? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

What's the difference between a container and an image in Docker? Docker中的容器和图像有什么区别? In the Get started with Docker tutorial these terms are both used, but I do not understand the difference. 入门Docker教程中,这些术语都被使用,但我不明白其中的区别。

Can anybody please shed some light? 任何人都可以解释一下吗?


#1楼

参考:https://stackoom.com/question/1scPm/在Docker中-容器和图像之间有什么区别-重复


#2楼

An image is an ordered collection of root filesystem changes and the corresponding execution parameters for use within a container runtime. 图像是根文件系统更改的有序集合以及在容器运行时内使用的相应执行参数。 Images are read-only. 图像是只读的。

A container is an active (or inactive if exited) stateful instantiation of an image. 容器是图像的活动(或退出时不活动)状态实例化。


#3楼

Images are frozen immutable snapshots of live containers. 图像是活动容器的冻结不可变快照。 Containers are running (or stopped) instances of some image. 容器正在运行(或停止)某些映像的实例。

Start with the base image called 'ubuntu'. 从名为'ubuntu'的基本映像开始。 Let's run bash interactively within the ubuntu image and create a file. 让我们在ubuntu映像中以交互方式运行bash并创建一个文件。 We'll use the -i and -t flags to give us an interactive bash shell. 我们将使用-i-t标志为我们提供一个交互式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        
...

Let's create an image from container ID 48cff2e9be75 where we created our file: 让我们从容器ID 48cff2e9be75创建一个图像,我们在其中创建了我们的文件:

$ 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!!!!

Try the command docker images . 尝试使用命令docker images You should see your new image ubuntu-foo listed along with the ubuntu standard image we started with. 您应该看到您的新图像ubuntu-foo与我们开始使用的ubuntu标准图像一起列出。


#4楼

An image is basically an immutable template for creating a container. 图像基本上是用于创建容器的不可变模板。 It's easier to understand the difference between an image and container by considering what happens to an image to turn it into a container. 通过考虑将图像转换为容器的情况,可以更容易地理解图像和容器之间的区别。

The Docker engine takes the image and adds a read-write filesystem on top, then initialises various settings. Docker引擎获取图像并在顶部添加读写文件系统,然后初始化各种设置。 These settings include network options (IP, port, etc.), name, ID, and any resource limits (CPU, memory). 这些设置包括网络选项(IP,端口等),名称,ID和任何资源限制(CPU,内存)。 If the Docker engine has been asked to run the container it will also initialise a process inside it. 如果已经要求Docker引擎运行容器,它还将初始化其中的进程。 A container can be stopped and restarted, in which case it will retain all settings and filesystem changes (but will lose anything in memory and all processes will be restarted). 可以停止并重新启动容器,在这种情况下,它将保留所有设置和文件系统更改(但会丢失内存中的所有内容,并且将重新启动所有进程)。 For this reason a stopped or exited container is not the same as an image. 出于这个原因,停止或退出的容器是一样的图像。


#5楼

Using an object-oriented programming analogy, the difference between a Docker image and a Docker container is the same as that of the difference between a class and an object. 使用面向对象的编程类比,Docker镜像和Docker容器之间的差异与类和对象之间的差异相同。 An object is the runtime instance of a class. 对象是类的运行时实例。 Similarly, a container is the runtime instance of an image. 类似地,容器是图像的运行时实例。

An object gets created only once when it is instantiated. 实例化对象时只创建一次对象。 Similarly, a container can be running or stopped. 同样,容器可以运行或停止。 Containers are created out of an image, though this might not always be the case. 容器是从图像中创建的,但情况可能并非总是如此。 The following example creates an Apache server image, runs the image, lists the images and then lists the containers: 以下示例创建Apache服务器映像,运行映像,列出映像,然后列出容器:

  1. Create a Dockerfile with the following contents: 使用以下内容创建Dockerfile:

     FROM httpd:2.4 
  2. Install Apache server 安装Apache服务器

     sudo docker build -t my-apache2 . 
  3. Run the image 运行图像

     sudo docker run -it --rm --name my-running-app my-apache2 
  4. List Docker images 列出Docker镜像

     sudo docker images 
  5. List the running Docker containers 列出正在运行的Docker容器

     docker ps 
  6. List all containers 列出所有容器

     docker ps -a 
  7. List latest created containers 列出最新创建的容器

     docker ps -l 

#6楼

Containers are based on images. 容器基于图像。 An image needs to be passed to the Dockers run command. 需要将映像传递给Dockers run命令。

Example: 例:

BusyBox image BusyBox图像

http://i.stack.imgur.com/eK9dC.png http://i.stack.imgur.com/eK9dC.png

Here we specify an image called busybox . 这里我们指定一个名为busybox的图像。 Docker does not have this image locally and pulls it from a public registry. Docker本地没有此映像,并从公共注册表中提取它。

A registry is a catalog of Docker images that the Docker client can communicate with and download image from. 注册表是Docker客户端可以与之通信并从中下载映像的Docker映像目录。 Once the image is pulled, Docker starts a container and execute the echo hello world command. 拉出图像后,Docker启动一个容器并执行echo hello world命令。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值