Docker Image learning (2)

Following the previous article “about the operation of Docker mirror, read this article is enough! (1)”, in the next chapter!!

As one of the most important keywords in the three core concepts of Docker, image has many operations that you have to master if you want to learn container technology. This article will take you step by step, both graphic and hands-on operation to learn it.

Directory:

  • Docker delete image
    • 1.1 Deleting a Mirror Using a Label
    • 1.2 Deleting a Mirror by ID
    • 1.3 Restrictions on Deleting mirrors
    • 1.4 Clearing a Mirror
  • Docker create image
    • 2.1 Creating an Image Based on an Existing Image
    • 2.2 Creation based on Dockerfile
  • Docker export & load image
    • 3.1 Exporting an Image
    • 3.2 Loading an Image
  • 4. Docker upload image
    • 4.1 Obtaining a Docker ID
    • 4.2 Creating a Mirror Vault
    • 4.3 Uploading an Image
  • Five, the summary

Docker delete image

1.1 Deleting a Mirror Using a Label

You can delete a mirror using either of the following methods:

docker rmi [image]
Copy the code

Or:

docker image rm [image]
Copy the code

The following subcommands are supported:

  • -f, -force: Forcibly deletes an image, even if the image is referenced by a container.
  • -no-prune: Do not delete untagged parent mirrors;
Docker View image information

For example, if we wanted to delete the allen_mysql:5.7 mirror created in the previous section, we would run the following command:

Docker rmi allen_mysql: 5.7Copy the code

Docker delete image

Allen_mysql :5.7 and docker. IO /mysql:5.7 actually refer to the same image. Docker. IO /mysql:5.7 will be deleted as well?

In fact, when the same image has multiple tags, executing the docker RMI command will only delete the tag you specified in the image, and will not affect the original image file.

Docker. IO /mysql:5.7

Docker View image information

Docker. IO /mysql:5.7

So, if a mirror does not have more than one label, be careful when executing a delete command if and only if there is only one label, which will delete the mirror completely.

Docker rmi docker. IO /mysql:5.7

Docker delete image

As you can see from the image above, we have removed all file layers of the docker.io/mysql:5.7 image. The mirror no longer exists locally!

1.2 Deleting a Mirror by ID

In addition to deleting an image by its tag name, we can also delete an image by specifying its ID, for example:

docker rmi ee7cbd482336
Copy the code

Once an image is removed by ID, it tries to remove all labels pointing to the image before removing the image itself.

1.3 Restrictions on Deleting mirrors

Deleting an image is simple, but it can’t be done anytime, anywhere, and there are some limitations.

The image cannot be deleted if the container created with the image has not been destroyed. To test this, let’s do an experiment. First, we use the docker pull alpine command to pull a new Alpine image, and then launch the image to output Hello, Docker! :

Docker run alpine

Next, let’s try deleting the mirror:

Docker delete image

You can see the message, cannot delete the image, because a container is referencing it! The mirror can only be removed by adding the -f subcommand, forcing it to be deleted.

docker rmi -f docker.io/alpine
Copy the code

However, we generally do not recommend such a violent approach, the correct approach should be:

  1. Delete the container that references the image;
  2. Then delete the image;

In other words, execute the delete command according to the container ID (9d59e2278553) that references the image as prompted in the figure above:

docker rm 9d59e2278553
Copy the code

Then, run the command to delete the mirror:

docker rmi 5cb3aa00f899
Copy the code

Docker delete image

At this time, it can be deleted normally!

1.4 Clearing a Mirror

After we use Docker for a period of time, the system will generally have some temporary and unused image files, which can be cleaned by the following command:

docker image prune
Copy the code

The subcommands it supports are:

  • -a, --allDelete all unused images, not just temporary files.
  • -f, --force: Forcibly deletes the image file without prompting for confirmation.

In addition, after executing the Docker Image prune command, it still tells us how much storage space was freed!

Docker create image

In this section, you will learn how Docker creates an image. There are three main types of Docker image creation:

  1. Create an image based on an existing image.
  2. Create from Dockerfile;
  3. Import based on local template;

We will mainly introduce the commonly used 1,2.

2.1 Creating an Image Based on an Existing Image

Run the following command to create it:

docker container commit
Copy the code

The following subcommands are supported:

  • -a, --author“=””: author information;
  • -c, --change=[]: Dockerfile commands can be executed when submitting, such as CMD, ENTRYPOINT, ENV, EXPOSE, LABEL, ONBUILD, USER, VOLUME, WORIR, etc.
  • -m, --message“=”;
  • -p, --pause=true: Suspends the container while committing.

Next, create a new image based on the existing Ubuntu image:

Docker creates an image

First, let me run it and create a test.txt file in it:

Docker creates an image

The command is as follows:

docker run -it docker.io/ubuntu:latest /bin/bash
root@a0a0c8cfec3a:/# touch test.txt
root@a0a0c8cfec3a: / #exit
Copy the code

After creating the test.txt file, remember the labeled container ID: A0A0C8CFec3a, and use it to submit a new image (PS: you can also submit an image by name, but here only by ID).

Execute command:

docker container commit -m "Added test.txt file" -a "Allen" a0a0c8cfec3a test:0.1
Copy the code

After the submission, the ID of the newly created image is displayed, as shown in the following figure:

Docker submits the newly created image

Test :0.1 = test:0.1 = test:0.1

Docker View image information

2.2 Creation based on Dockerfile

Dockerfile is the most common and recommended way to create an image. Dockerfile is a text instruction file that describes how to create a new image based on a parent image.

Let’s write a simple Dockerfile that describes a Python3 environment based on the Ubuntu parent image:

FROM docker.io/ubuntu:latest

LABEL version="1.0" maintainer="Allen <weiwosuo@github>"

RUNapt-get update && \ apt-get install -y python3 && \ apt-get clean && \ rm -rf /var/lib/apt/lists/*Copy the code

To build a new image from the Dockerfile file, run the following command:

docker image build -t python:3 .
Copy the code

Note: There is a dot at the end of the command, if not added, the build will fail!

Docker builds an image from a Dockerfile

Once compiled, check the local image information again to see the newly built Python :3 image.

Docker View image information

Docker export & load image

In this section, you will learn how Docker exports & loads images.

If you need to share an image with others, you can export the image as a tar package, and others can quickly import the image into the local image library by loading the tar package.

To use these two functions, use the following two commands:

  1. docker save
  2. docker load

3.1 Exporting an Image

View the local image as follows:

Docker View image information

For example, if we want to export the Python :3 image, run the following command:

docker save -o python_3.tar python:3
Copy the code

After the command is executed successfully, view the current directory:

Docker export file

You can see that the python_3.tar image file has been generated. Then you can copy it and share it with others!

3.2 Loading an Image

How do you import the tar file into your local image library?

Run the following command:

docker load -i python_3.tar
Copy the code

Or:

docker load < python_3.tar
Copy the code

After the import is successful, view the local image information, you can get others to share the image! How about it? Is it convenient?!

4. Docker upload image

We will upload the above to Docker Hub as an example, demonstrating how Docker to upload images.

4.1 Obtaining a Docker ID

To upload the image to Docker Hub, we need to register a Docker Hub account first. Open the Docker Hub website at hub.docker.com and start registering:

Docker Hub register account

Fill in your Docker ID, password, Email, and click Continue.

Next, Docker Hub will send a verification email to the email you fill in:

Docker Hub validates messages

Click verify. Next, return to Docker Hub’s official website again and log in with your Docker ID and password!

Docker Hub login page

4.2 Creating a Mirror Vault

After successful login, the following page is displayed:

Welcome to Docker Hub

Select create a mirror repository:

Creating a Python repository

After filling in the warehouse name, description, public or not, click Create.

Warehouse image display page

We see that the repository has been created successfully, but there are no images in it yet, so we start uploading images to the newly created repository.

4.3 Uploading an Image

Enter the command line, log in with the Docker ID and password we just obtained, and run the following command:

docker login
Copy the code

Command line login Docker ID

After login, we are ready to upload the local Python :3 image:

Python: 3 images

First, we label it with a new prefix that is the same as the newly created Docker ID and warehouse name:

docker tag python:3 weiwosuoai1991/python:3
Copy the code

Python :3 Label mirroring

View the local information, and you can see that the label is successfully labeled. Open and upload! Execute command:

docker push weiwosuoai1991/python:3
Copy the code

Upload the Python :3 image

Upload successful! Check the information page of the newly created warehouse on the official website of Docker Hub to see if it is really successful:

Warehouse image display page

Work done!!

Five, the summary

In this article, we focus on learning how to delete images, create images, export & load images, and upload images to Docker Hub. This article is the next part of Docker image learning. More docker-related learning articles will be brought in the future. Wish you a happy learning!

Gifts | interview & learning welfare resources

Recently found a good PDF resource on the Internet “Java core interview knowledge. PDF” to share with you, not only interview, learning, you are worth having!!

How to obtain: pay attention to the public account: Xiahalearning Java, background reply resources, both can obtain the resource link, the following is the directory and some screenshots:

Screenshot of Welfare Resources

Screenshot of Welfare Resources

Screenshot of Welfare Resources

Screenshot of Welfare Resources

Screenshot of Welfare Resources

Screenshot of Welfare Resources

Screenshot of Welfare Resources

Important things to say twice, access: pay attention to the public number: ha learning Java, background reply resources, both can get resource links!!

Welcome to pay attention to personal public number: small ha learning Java