Understand the core concepts and installation configurations of Docker

Core concepts

Docker has three core concepts: image, container and warehouse.

Docker image

The Docker image is similar to the image of a virtual machine. It can be understood as a read-only template. For example, an image can contain a basic operating system environment in which only Apache applications are installed. It can be called an Apache image.
Image is the basis for creating Docker container.
Through version management and incremental file system, Docker provides a very simple mechanism to update the existing image. Users can even download a completed application image from the Internet and use it.

Docker container

Docker container is similar to a lightweight sandbox. Docker uses containers to run and isolate applications.
Containers are application running instances created from the image. They can start and stop deletion. These containers are isolated from each other and invisible to each other.
Think of the container as a simple Linux system environment and a box of applications running in it.

Docker warehouse

The Docker warehouse is similar to the code warehouse. It is the place where Docker stores image files in a centralized manner.
Docker warehouse can be divided into public warehouse and private warehouse according to whether the saved image is public or not.

Install Docker engine

The following example is to install docker under Windows

  1. Visit the homepage of the official website.

https://www.docker.com/

  1. Click Apply now to select the windows version to download

  1. Click project to install

  1. Install

  1. Verify successful installation

Open powershell and enter docker

In this way, the windows version of docker has been installed

Using Docker mirroring

The docker image is used as follows

Get image

To obtain the docker image, use the following command.
Enter the command to obtain the docker image

 docker pull ubuntu

From the download process, we can see that for the docker container, the image file is generally composed of several layers,

Each string is a unique id. when using the pull command of docker, the relevant images will be downloaded layer by layer, and the image from the previous layer will point to the image from the previous layer.

View mirror information

Use the images command of docker to view the image information of docker.

The image information of docker is listed in images above.

Use the tag command to add the label of the image

To facilitate the use of docker images, use the docker tag command to add the docker image label.

 docker tag ubuntu:latest myubuntu:laster

The effect is as follows

Use the inspect command to view the docker details

 docker image inspect ubuntu

See the following information

PS C:\Users\Administrator\Desktop> docker image inspect ubuntu
[
    {
        "Id": "sha256:74435f89ab7825e19cf8c92c7b5c5ebd73ae2d0a2be16f49b3fb81c9062ab303",
        "RepoTags": [
            "myubuntu:laster",
            "ubuntu:latest"
        ],
        "RepoDigests": [
            "ubuntu@sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2020-06-17T01:20:56.323568224Z",
        "Container": "9477bb1d88721b63e0c6a359eb48a081741a0992480bf34bbcebf4e5734fcf67",
        "ContainerConfig": {
            "Hostname": "9477bb1d8872",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:5412cfe47ff528df823761788f99a11568e2d25f48a4245a3d67cdc3e564c839",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "18.09.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:5412cfe47ff528df823761788f99a11568e2d25f48a4245a3d67cdc3e564c839",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 73856440,
        "VirtualSize": 73856440,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/63e1c7f313e414bd5465953da0ceb615cc9c5906c9b539ed164560b18020bc85/diff:/var/lib/docker/overlay2/09f0332099bc97d0052e07879f18271f2c7ea879501498a1eca00711065c9e9a/diff:/var/lib/docker/overlay2/8fef100fbab32d7aa7a3cc70740ef97b45cc9dcfce91453dd10cc502a3fa10f7/diff",
                "MergedDir": "/var/lib/docker/overlay2/3ec5eeae7aa3781aa605fa1a1dc598c39d7774796600b3c1f86e8190c24d5a47/merged",
                "UpperDir": "/var/lib/docker/overlay2/3ec5eeae7aa3781aa605fa1a1dc598c39d7774796600b3c1f86e8190c24d5a47/diff",
                "WorkDir": "/var/lib/docker/overlay2/3ec5eeae7aa3781aa605fa1a1dc598c39d7774796600b3c1f86e8190c24d5a47/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:e1c75a5e0bfa094c407e411eb6cc8a159ee8b060cbd0398f1693978b4af9af10",
                "sha256:9e97312b63ff63ad98bb1f3f688fdff0721ce5111e7475b02ab652f10a4ff97d",
                "sha256:ec1817c93e7c08d27bfee063f0f1349185a558b87b2d806768af0a8fbbf5bc11",
                "sha256:05f3b67ed530c5b55f6140dfcdfb9746cdae7b76600de13275197d009086bb3d"
            ]
        },
        "Metadata": {
            "LastTagTime": "2020-07-06T13:37:35.064486339Z"
        }
    }
]

Use the history command to view the mirror history

docker history ubuntu
PS C:\Users\Administrator\Desktop> docker history ubuntu
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
74435f89ab78        2 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>           2 weeks ago         /bin/sh -c mkdir -p /run/systemd && echo 'do...   7B
<missing>           2 weeks ago         /bin/sh -c set -xe   && echo '#!/bin/sh' > /...   811B
<missing>           2 weeks ago         /bin/sh -c [ -z "$(apt-get indextargets)" ]     1.01MB
<missing>           2 weeks ago         /bin/sh -c #(nop) ADD file:b2342c7e6665d5ff3...   72.8MB
PS C:\Users\Administrator\Desktop>

Search for related images

Search the image of official keyword with nginx

PS C:\Users\Administrator\Desktop> docker search --filter=is-official=true nginx
NAME                DESCRIPTION                STARS               OFFICIAL            AUTOMATED
nginx               Official build of Nginx.   13426               [OK]
PS C:\Users\Administrator\Desktop>

Deleting and cleaning up mirrors

Image related deletion

Deleting a mirror using a label

 docker rmi ubuntu
PS C:\Users\Administrator\Desktop> docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce
PS C:\Users\Administrator\Desktop>

Delete a mirror using the mirror ID

PS C:\Users\Administrator\Desktop> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myubuntu            laster              74435f89ab78        2 weeks ago         73.9MB
ubuntu              latest              74435f89ab78        2 weeks ago         73.9MB
PS C:\Users\Administrator\Desktop> docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myubuntu            laster              74435f89ab78        2 weeks ago         73.9MB
PS C:\Users\Administrator\Desktop> docker rmi 74435f89ab78
Untagged: myubuntu:laster
Deleted: sha256:74435f89ab7825e19cf8c92c7b5c5ebd73ae2d0a2be16f49b3fb81c9062ab303
Deleted: sha256:8a8d1f0b34041a66f09e49bdc03e75c2190f606b0db7e08b75eb6747f7b49e11
Deleted: sha256:f1b8f74eff975ae600be0345aaac8f0a3d16680c2531ffc72f77c5e17cbfeeee
Deleted: sha256:27d46ebb54384edbc8c807984f9eb065321912422b0e6c49d6a9cd8c8b7d8ffc
Deleted: sha256:e1c75a5e0bfa094c407e411eb6cc8a159ee8b060cbd0398f1693978b4af9af10
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
PS C:\Users\Administrator\Desktop>

Clean up mirror

Clean up using docker images

PS C:\Users\Administrator\Desktop> docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
PS C:\Users\Administrator\Desktop>

create mirror

Create a mirror based on an existing container

View the list of running containers

PS C:\Users\Administrator\Desktop> docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
ff7a974b9771        nginx               "/docker-entrypoint...."   About a minute ago   Up About a minute   80/tcp              fervent_curie

Create related containers

 docker container commit -m "ming" -a "mingming" ff7a974b9771  test:0.1

View again

PS C:\Users\Administrator\Desktop> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                0.1                 54a6eb8d4483        5 seconds ago       132MB
ubuntu              latest              74435f89ab78        2 weeks ago         73.9MB
nginx               latest              2622e6cca7eb        3 weeks ago         132MB
PS C:\Users\Administrator\Desktop>

The complete process is shown in the following figure

Create using template

https://download.openvz.org/t...

Download related templates

Import related images

cat centos-6-x86-minimal.tar.gz|docker import - centos6

Create using dockerFile

# Construction phase
FROM node
# assign work directory
WORKDIR /
# Specify work port
EXPOSE 4000
# Installing gitbook
RUN npm install gitbook -g;
RUN npm install gitbook-cli -g;
# Pull file
RUN git clone https://github.com/mySoul8012/SE-Basic-knowledge.git
# Enter directory
WORKDIR /SE-Basic-knowledge
# Initialize mirroring
RUN gitbook init
CMD ["gitbook", "serve"]

Import built image

docker image build -t ming:0.1

Saving and loading images

Save out image

PS C:\Users\Administrator\Desktop> docker save -o ubuntu.tar ubuntu
PS C:\Users\Administrator\Desktop>

Load image

PS C:\Users\Administrator\Desktop> docker load -i ubuntu.tar
Loaded image: ubuntu:latest
PS C:\Users\Administrator\Desktop> docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
ff7a974b9771        nginx               "/docker-entrypoint...."   12 minutes ago      Up 12 minutes       80/tcp              fervent_curie
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                0.1                 54a6eb8d4483        10 minutes ago      132MB
ubuntu              latest              74435f89ab78        2 weeks ago         73.9MB
nginx               latest              2622e6cca7eb        3 weeks ago         132MB
PS C:\Users\Administrator\Desktop>

Upload image

Upload image to docker

PS C:\Users\Administrator\Desktop> docker push test
The push refers to repository [docker.io/library/test]
53d99160caf2: Preparing
f978b9ed3f26: Preparing
9040af41bb66: Preparing
7c7d7f446182: Preparing
d4cf327d8ef5: Preparing
13cb14c2acd3: Waiting
denied: requested access to the resource is denied
PS C:\Users\Administrator\Desktop>

You can see the rejection. Log in to the account first

 docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: melovemingming
Password:
Login Succeeded

Then upload the file

 docker push melovemingming/nginx

Note that the uploaded image format must be user name / image name

View the docker hub to see the uploading of docker images
You can see that the docker image has been uploaded

Tags: Java

Posted by Yanayaya on Wed, 01 Jun 2022 03:31:09 +0530