preface

Images are one of Docker’s three core concepts (the other two are containers and repositories).

Docker requires a local image before running the container. If no local image exists, Docker will try to pull the image from the remote repository. Image is a big core of Docker, we will understand the operation of Docker image today.

Original statement

This article was published on Happyjava. Happy nuggets address: juejin.cn/user/398428… , Happy’s personal blog :(blog.happyjava.cn)[blog.happyjava.cn] Welcome to reprint, but keep this statement.

Access to the mirror

Command format:

docker pull <name:tag>
Copy the code

If you do not explicitly specify a tag, the latest tag is selected by default, which is to download the latest version of the image from the repository.

For example: Pull an Ubuntu image:

If you do not specify a tag, the latest version is pulled by default

Viewing Mirror Information

Docker images can be used to view local image information:

You can list: mirror warehouse, label, mirror ID, creation time, and size.

Docker inspect <image_id>

[{"Id": "sha256:4c108a37151f54439950335c409802e948883e00c93fdb751d206c9a9674c1f6"."RepoTags": [
            "ubuntu:latest"]."RepoDigests": [
            "ubuntu@sha256:9b1702dcfe32c873a770a32cfd306dd7fc1c4fd134adfb783db68defc8894b3c"]."Parent": ""."Comment": ""."Created": "The 2019-06-18 T22:51:38. 340092056 z"."Container": "fdea049ea807b599050e885a88784e009ed78ebcc4d498be93744bb9374c6134"."ContainerConfig": {
            "Hostname": "fdea049ea807"."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:ae950a0376fe6c4d08fa7ff395f50f4a909e26e9f2d865d8641cda024161c6ee"."Volumes": null."WorkingDir": ""."Entrypoint": null."OnBuild": null."Labels": {}},"DockerVersion": "18.06.1 - ce"."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:ae950a0376fe6c4d08fa7ff395f50f4a909e26e9f2d865d8641cda024161c6ee"."Volumes": null."WorkingDir": ""."Entrypoint": null."OnBuild": null."Labels": null
        },
        "Architecture": "amd64"."Os": "linux"."Size": 64184378."VirtualSize": 64184378."GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/fc1c2e7f7d583af0ddaa86f2d10be50371a966dd564c9d95b1816c33296520c7/diff:/var/lib/docker/overlay2 /615da971b39e6d5121eb018b14f350b1466ef9445870ddba03d2d2da3ecd25d7/diff:/var/lib/docker/overlay2/e508ef7c05f3bdcd784d1f59 064574d92f11516e6592cd5df24cfdac7ae13768/diff"."MergedDir": "/var/lib/docker/overlay2/f6b9b965a5e2714452cf355e1a530b8b2f8c5d818e73f723debd7b27d955ae5e/merged"."UpperDir": "/var/lib/docker/overlay2/f6b9b965a5e2714452cf355e1a530b8b2f8c5d818e73f723debd7b27d955ae5e/diff"."WorkDir": "/var/lib/docker/overlay2/f6b9b965a5e2714452cf355e1a530b8b2f8c5d818e73f723debd7b27d955ae5e/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers"."Layers": [
                "sha256:ba9de9d8475e7f5e40086358a1353b3cc080994fc6d31e4272dd3acb69b0151e"."sha256:fbd2732ad777cb5db2515fa62b6122b797be233d01db02e0a19e5d894688cad6"."sha256:dda1518598187bf87704acc22aa0ec2a67d9e7835c24346dfca118ab42c5cd0b"."sha256:75e70aa52609fdbd63b58d46d6f7c20470062e1c9bb75616f7703a358a61e5ca"]},"Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"}}]Copy the code

Modifying a Mirror TAG

We use the default tag to pull the image. Locally, the tag is displayed as latest, which is really bad because we may forget the version number. However, we can change the mirror TAG by using the following command:

docker tag <iamges_name>:<images_tag> <new_iamges_name>:<new_images_tag>
Copy the code

Such as:

We can see that ubuntu 18.09 and latest have the same image ID, indicating that this is the same image.

Search the mirror

docker search name
Copy the code

The OFFICIAL image has an OFFICIAL identifier

Remove the mirror

Delete using the mirror ID

docker rmi <iamge_id>
Copy the code

Delete by name and TAG

docker rmi <name>:<tag>
Copy the code

The interim tag field is optional.

If a mirror cannot be deleted, for example, if it is in use, you can use the -f parameter to forcibly delete it

Create a mirror image

There are three ways to create an image: container based, local template based import, and Dockerfile based.

Container-based creation

Create an Ubuntu container

Docker run -it --name ubuntu ubuntu:18.09 /bin/bashCreate a file inside the container
touch happyjava.txt

# exit
exit
Copy the code

To create the image, run the following command

docker commit -m "commit message" -a "author" <container_id> <image_name>:<tag>
Copy the code

Such as:

docker commit -m "create a new file happyjava.txt" -a "Happy" d51e6f5e99d4 personalrepo:99

Copy the code

Docker Images to view images:

This is similar to GIT’s commit operation, where changes can be committed. We can create a new container with our own image and see the happyjava.txt file we created.

Created using a local template

Recommended template download address:

https://wiki.openvz.org/Download/template/precreated

Copy the code

Create the following command:

cat centos-5-x86.tar.gz |docker import - <name>:<tag>

Copy the code

Dockerfile way

Create an image based on Dockerfile. This will be explained later

Save and load the image

Save the image

docker save -o <filename> <name:tag/id>

Copy the code

Such as:

Load the image

docker load --input <filename>

Copy the code

or

docker load < <filename>

Copy the code

conclusion

This article summarizes the common operations of images in Docker, including image search, pull, create, delete, etc. Image is the premise of using Docker, and its basic operation needs to be mastered.