Docker Images and Containers

2018-04-22  本文已影响0人  two漾two森破

Image and container are two terms that are quite difficult for a new starter to distinguish. This article aimed to help you understand what exactly they are, thus you will be aware of how Docker works.

1. Basic Definitions

2. Relation Between Image and Container

Given that pure definitions are hard to digest, this section will focus on practice. By following the flowchart below (which also contains the internal structure of images and containers), the relation between image and container as well as the definitions of them will be clear.

Image_and_Container_Relation.jpg

(1) Base Image:

The majority of Docker images are based on a parent image, but a base image has NO parent. Actually, at the very beginning, every image was a base image. You can create your very own base image by following this official guide, but typically, building an image based on another one is a better choice.

(2) Build an Image:

An image could be build by using Dockerfile and docker build [OPTIONS] PATH command. There are numerous other ways but this one is highly recommended. A Dockerfile consists of all the information you need to construct an image, e.g. what the parent image of it is, what contents will be added in it, what command will be executed when the image is run and so on.

As you can see in the picture, a new image includes a series of ordered layers stacked on its parent image. They construct a new image together. A Docker image is read-only, once it is built, you can make no change on it.

You can upload your image to a registry by using docker push [OPTIONS] NAME[:TAG] or download existing image by using docker pull [OPTIONS] NAME[:TAG|@DIGEST].

(3) Run a Container

Once you have an image, you can run it by using docker run [OPTIONS] IMAGE [COMMAND] [ARG...]. As the image is run, a container is generated. A container is consists of a writable layer and the image you run. All the following operations in the container and the changes they cause will be recorded in the writable layer. With the copy-on-write technique, the process of generating a new container is fast and light.

(4) Commit It!

If you want to save all the changes in a container or you want to share it with others, you can easily create a new image from a container's change by using docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]. This process adds the writable layer of a container to the new image as layers, thus a new read-only image is generated! Then you can push it to a registry, pull it back or ask others to pull it or run it.

3. Summary

Suammry_Flowchart.jpg
上一篇 下一篇

猜你喜欢

热点阅读