SoFunction
Updated on 2025-03-10

Detailed introduction to mirroring in Docker

Detailed introduction to mirroring in Docker

Docker image can be understood as a component running in a Docker container. This section will lead you to learn a series of operations such as downloading, searching, viewing, adding and deleting Docker image. Docker needs to have a corresponding image locally before running the container. If the image does not exist locally, Docker will try to download it from the default remote repository Docker Hub.

1. Obtain the mirror:

1. The format of Docker downloading the mirror from the network is:

 docker pull [NAME]:[TAG]

Among them, TAG is a tag. If the tag is not specified, the latest version of the mirror will be downloaded by default.

For example, download the latest ubuntu operating system from Docker Hub:

  docker pull ubuntu

Then docker will download the latest version of ubuntu from the Docker Hub.

2. Download the mirror in the specified warehouse:

If you think the download image on Docker Hub is too slow, you can download the image in other repositories. You only need to add the URL of the specified repository in front of the downloaded image.

docker pull [REPOSITORY]/ubuntu

3. Use mirror:

Since the image has been downloaded locally, it can be used at any time. Here is how to use the ubuntu image to run the bash application in ubuntu:

  docker run -t -i ubuntu /bin/bash

Enter exit to exit the bash application.

2. View the mirror:

1. The command is:

  docker images


Use the above command to see all the image files that exist locally, among which,

REPOSITORY represents which repository comes from. For example, downloading ubuntu from Docker Hub, then directly display ubuntu. If ubuntu is downloaded from other repository, then display

    REPOSITORY/ubuntu
TAG represents the label of the mirror
IMAGE ID represents the unique ID of the image
CREATED represents the creation time
VIRTUAL SIZE represents the size of the image

2. Set labels for the mirror

In order to facilitate the use of mirrors in work, you can use the docker tag command to tag the local image. The tag can be regarded as an alias. A mirror can have multiple tags, but only one ID

  docker tag [NAME]:[Old TAG] [NAME]:[New TAG] 

A new tag will be added to the local image, namely New TAG, for example:

  docker tag ubuntu:latest ubuntu:happyheng

This puts a new tag on the local tag as the latest ubuntu for happyheng.

Of course, after tagging, the local mirror will not change at all, it is just a new tag.

3. View the detailed information of the mirror:

  docker inspect [IMAGE ID]

This allows you to view the detailed information of this image based on the id of the image.

3. Search for mirrors

  docker search [NAME]

It will return the mirrored keywords, including the image name, description, star rating, whether it is officially created, whether it is automatically created, etc.

4. Delete the mirror

1. Use the mirrored tag to delete the mirror:

  docker rmi [NAME]:[TAG]

Note that when using tags to delete, if a mirror has two tags, deleting only one will not delete the image, but if only one tag is left, deleting this tag will also delete the image.

2. Use the mirror ID to delete the mirror:

  docker rmi [ID]

Note that if a container is using a mirror, the deletion cannot be successful.

5. Create a mirror:

There are three ways to create a mirror, namely:

Container creation based on existing mirrors
Import based on local templates
Created based on Dockerfile

1. Create containers based on existing mirrors:

This method mainly uses docker commit [OPTIONS] [Container ID] [NAME]:[TAG]

Note that the CONTAINER ID is the id of the container, not the id of the mirror, because the image changes while running in the container, so it must be the id of the container.

for example

  docker run -ti ubuntu:latest /bin/bash
  root@c8ee80e08468:/# touch test
  root@c8ee80e08468:/# exit

Note that the id of the container is c8ee80e08468 at this time, so when commit submits the mirror, the command is:

docker commit -m "add a new file" -a "newbee" c8ee80e08468 ubuntu:add


This is to submit the modified image to the local repository, where the submitted tag is ubuntu:add

in:

-a  : Author information
-m : Submitted information
-p : Pause container operation during submission
It can be found that this method is very similar to Git.

6. Save and load images:

The docker save and docker load commands can be used to store and load images.

1. Save the mirror:

  docker save -o  [NAME]:[TAG]


For example, if I want to save the image tag as ubuntu:add as a tar package, then I can use it

  docker save -o ubuntu_add.tar ubuntu:add

Among them, the current terminal is stored in which directory, tar will be saved to

2. Load the mirror:

  docker load --input ubuntu_add.tar


You can load the tar image of the above directory.

7. Upload the mirror

You can use the docker push command to upload the image to the repository, and upload it to the official Docker Hub repository by default. Of course, you need to log in:

  docker push [NAME]:[TAG]

You can upload the local image

Thank you for reading, I hope it can help you. Thank you for your support for this site!