In Docker, what's the difference between a con

2019-01-08 02:49发布

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.

Can anybody please shed some light?

13条回答
该账号已被封号
2楼-- · 2019-01-08 03:31

Image is the photo made from your phone.
Container is the phone.

查看更多
乱世女痞
3楼-- · 2019-01-08 03:32

In Docker, it all begins with an image. An image is every file that makes up just enough of the operating system to do what you need to do. Traditionally you'd install a whole operating system with everything for each application you do. With Docker you pair it way down so that you have a little container with just enough of the operating system to do what you need to do, and you can have lots and lots of these efficiently on a computer.

Use docker images to see the installed images and docker ps to see the running images. When you type docker run it takes the image, and makes it a living container with a running process. I tend to use:

docker run -ti <image>:<tag> bash

Lastly, images have their own set of ids and containers have their own set of ids - they don't overlap.

查看更多
劫难
4楼-- · 2019-01-08 03:35

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. 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:

  1. Create a Dockerfile with the following contents:

    FROM httpd:2.4
    
  2. Install Apache server

    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

    sudo docker images
    
  5. List the running Docker containers

    docker ps
    
  6. List all containers

    docker ps -a
    
  7. List latest created containers

    docker ps -l
    
查看更多
神经病院院长
5楼-- · 2019-01-08 03:36

In easy words.

Images -

The file system and configuration(read-only) application which is used to create containers. More detail.

Containers -

These are running instances of Docker images. Containers run the actual applications. A container includes an application and all of its dependencies. It shares the kernel with other containers and runs as an isolated process in user space on the host OS. More detail.


Other important terms to notice:


Docker daemon -

The background service running on the host that manages the building, running and distributing Docker containers.

Docker client -

The command line tool that allows the user to interact with the Docker daemon.

Docker Store -

Store is, among other things, a registry of Docker images. You can think of the registry as a directory of all available Docker images.

A picture is worth a thousand words.

Enter image description here

(For deeper understanding please read this.)

Summary:

  • Pull image from Docker hub or build from a Dockerfile => Gives a Docker image (not editable).
  • Run the image (docker run image_name:tag_name) => Gives a running Image i.e. container (editable)
查看更多
Summer. ? 凉城
6楼-- · 2019-01-08 03:37

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.

查看更多
成全新的幸福
7楼-- · 2019-01-08 03:37

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. These settings include network options (IP, port, etc.), name, ID, and any resource limits (CPU, memory). If the Docker engine has been asked to run the container it will also initialise a process inside it. 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.

查看更多
登录 后发表回答