美文网首页
Docker Images and Containers

Docker Images and Containers

作者: two漾two森破 | 来源:发表于2018-04-22 14:02 被阅读0次

    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

    • Docker Images:

      A Docker image is the basis of a series of Docker containers. Once an image is run, a Docker container is generated. An image can be run several times, thus a series of similar but isolated containers can be generated.

      A Docker image consists of a stack of ordered read-only layers which have no state and can not be changed.

    • Docker Containers:

      A Docker container is a runtime instance of a Docker image. A container consists of a Docker image, an execution environment and a standard set of instructions. You can run a Docker image several times and get several containers. Changes in one container will not influence the others.

    • Dockerfile:

      A Dockerfile is a list of commands that indicates how to build a Docker image, including which image will the current one depends on, which ports will be exposed to the outside, what command will be executed when the image is run and so on. Each command in the Dockerfile will become a layer of the Docker image in the future.

    • Repository and Registry:

      A repository is a set of Docker images. A repository can be shared by pushing it to a registry server. The different images in the repository can be labeled using tags.

      A Registry is a hosted service containing repositories of images which response to the Registry API.

      The Docker Hub is an official registry server maintained by Docker Incorporated. Of course, you can maintain your very own registry as well.

    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

    • A Docker image is read-only, while a container contains a read-only image as well as a writable layer.
    • Once you run a Docker image, you get a container, which is a runtime instance of the image. You can commit it and then get a read-only image again.
    • You can run an image several times, but those containers will be isolated, which means changes in one will not influence the others.
    • A running container might be stopped due to some reasons. To start it again, use docker start [OPTIONS] CONTAINER
    • Another way to get an image is to build one with a Dockerfile.
    Suammry_Flowchart.jpg

    相关文章

      网友评论

          本文标题:Docker Images and Containers

          本文链接:https://www.haomeiwen.com/subject/nxfclftx.html