Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use docker image

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Docker Mirror

Mirroring is the most important of the three core concepts of Docker. The running process is

Docker operation-whether the corresponding image exists locally-if there is no Docker trying to download from the default image repository, you can also customize the configuration of the image repository.

Get the image

The format is docker [image] pull NAME [: TAG]

Where NAME is the name of the image repository, and TAG is the label of the image (often used to represent the version). Usually, the description of an image needs to include "name + tag" information.

Test to get a ubuntu 18.04system

[root@docker01 ~] # docker pull ubuntu:18.0418.04: Pulling from library/ubuntu423ae2b273f4: Downloading 15.25MB/26.69MBde83a2304fa1: Download complete f9a83bce3af0: Download complete 423ae2b273f4: Pull complete de83a2304fa1: Pull complete f9a83bce3af0: Pull complete b6b53be908de: Pull complete Digest: sha256:04d48df82c938587820d7b6006f5071dbbffceb7ca01d2814f81857c631d44dfStatus: Downloaded newer image for ubuntu:18.04docker.io/library/ubuntu:18.04

If you do not customize TAG, the latest tag is selected by default

[root@docker01 ~] # docker pull centosUsing default tag: latestlatest: Pulling from library/centos8a29a15cefae: Pull complete Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700Status: Downloaded newer image for centos:latestdocker.io/library/centos:latest

Note: generally speaking, the mirror latest tag will change with the latest version of the mirror content, and the content is unstable, so for the sake of stability, do not use the latest tag in the production environment.

Using the official Docker Hub prefix can be ignored, and the completion is

Docker pull registry.hub.docker.com/ubuntu:18.04

Use unofficial nests such as NetEase

Docker pull hub.c.163.com/public/ubuntu:18.04

Test and run a bash application

[root@docker01 ~] # docker run-it centos:latest bash [root@894350cbee44 /] # cat / etc/redhat-release CentOS Linux release 8.1.1911 (Core) [root@894350cbee44 /] # echo "hello world" hello world [root@894350cbee44 /] # exit View Image Information

List Mirror

[root@docker01 ~] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEubuntu 18.04 72300a873c2c 11 days ago 64.2MBcentos latest 470671670cac 6 weeks ago 237MB

From the above information, we can see

Which warehouse do you come from-- centos

Image tag-latest

Mirror ID

Creation time

Mirror Siz

Add an image label

To facilitate the use of feature images in subsequent work, add a new label to the local image. The docker tag command acts as a link, and the ID number is the same after the link.

[root@docker01 ~] # docker tag centos:latest mycentos:latest [root@docker01 ~] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEubuntu 18.04 72300a873c2c 11 days ago 64.2MBcentos latest 470671670cac 6 weeks ago 237MBmycentos latest 470671670cac 6 weeks ago 237MB

View image details

[root@docker01 ~] # docker inspect centos:latest

View the mirror history. If the command is too long, it will be truncated.-- no-trunc option outputs the complete command.

[root@docker01 ~] # docker history centos:latestIMAGE CREATED CREATED BY SIZE COMMENT470671670cac 6 weeks ago / bin/sh-c # (nop) CMD ["/ bin/bash"] 0B 6 weeks ago / bin/sh-c # (nop) LABEL org.label-schema.sc... 0B 7 weeks ago / bin/sh-c # (nop) ADD file:aa54047c80ba30064 … 237MB searches for images

Search for images officially provided with the nginx keyword

[root@docker01] # docker search-- filter=is-official=true nginxNAME DESCRIPTION STARS OFFICIAL AUTOMATEDnginx Official build of Nginx. 12745 [OK]

Search for images with more than 4 favorites as tensorflow

[root@docker01] # docker search-- filter=stars=4 tensorflowNAME DESCRIPTION STARS OFFICIAL AUTOMATEDtensorflow/tensorflow Official Docker images for the machine learn... 1631 jupyter/tensorflow-notebook Jupyter Notebook Scientific Python Stack w / … 201 tensorflow/serving Official images for TensorFlow Serving (http … 77 xblaster/tensorflow-jupyter Dockerized Jupyter with tensorflow 52 [OK]

Includes image name, description, number of favorites, official creation, etc.

Delete Mirror

Delete a mirror using a label

[root@docker01 ~] # docker rmi mycentos:latestUntagged: mycentos:latest

Force the image to be deleted, even if a container depends on it

[root@docker01 ~] # docker rmi-f mycentos:latestUntagged: mycentos:latest

Use Mirror ID to delete Mirror

[root@docker01 ~] # docker rmi 72300a873c2c

When a container created by an image exists, the image file cannot be deleted by default

[root@docker01 ~] # docker run myubuntu:18.04 echo 'hello~'hello~ [root@docker01 ~] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES8029f804939ff centos:latest "echo hello~" About a minute ago Exited (0) About a minute ago compassionate_ Wescoff [root@docker01 ~] # docker rmi centos:latestError response from daemon: conflict: unable to remove repository reference "centos:latest" (must force)-container 029f804939ff is using its referenced image 470671670cac

At this point, you can force the deletion with the-f parameter, but this is usually not recommended. The correct approach is

Delete Container

[root@docker01 ~] # docker rm 029f804939ff029f804939ff

Delete Mirror

[root@docker01 ~] # docker rmi 470671670cacUntagged: centos:latestUntagged: centos@sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700Deleted: sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1eeDeleted: sha256:0683de2821778aa9546bf3d3e6944df779daba1582631b7ea3517bb36f9e4007 cleans the image

After using docker for a period of time, the system may leave behind some temporary image files and some unused images

Automatically clean up temporary legacy image file layer

[root@docker01 ~] # docker image prune-fTotal reclaimed space: 0B create an image

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

1. Create a docker [container] commit command based on an existing container

Common parameters

-a ~ the author's information.

Execute Dockerfile command when submitting, including CMD | ENTRYPOINT | ENV | EXPOSE | LABEL | ONBUILD | USER | VOLUME | WORKDIR, etc.

-mmam Murray message = "" # submit information

-pmam Meltel pauseworthy true # suspend container operation when submitting

Start an image and create a file in it

[root@docker01 ~] # docker run-it ubuntu:18.04 / bin/bashroot@734364a15c55:/# touch testroot@734364a15c55:/# exit

Remember that the ID number of the container is 734364a15c55, which has changed from the original image, and a new image is submitted.

[root@docker01] # docker commit-m "add a file"-a "tcw" 734364a15c55 testfile:0.1sha256:f14661e2c5eeaf03d14ce4ec0d9e963daeddc3c93a85de959029acf0b1278b4d

The newly created image ID information is returned: sha256:f14661e2c5eeaf03d14ce4ec0d9e963daeddc3c93a85de959029acf0b1278b4d

[root@docker01 ~] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEtestfile 0.1 f14661e2c5ee 5 minutes ago 64.2MBmyubuntu 18.04 72300a873c2c 11 days ago 64.2MBubuntu 18.04 72300a873c2c 11 days ago 64.2MB

2. Import based on local template

We can also import an image docker [image] import [OPTIONS] file directly from an operating system template file | URL |-[REPOSITORY [: TAG]]

Here we directly use the template provided by OpenVZ to create

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

[root@docker01 ~] # wget http://download.openvz.org/template/precreated/ubuntu-14.04-x86_64-minimal.tar.gz[root@docker01 ~] # cat ubuntu-14.04-x86_64-minimal.tar.gz | docker import-ubuntu:14.04 [root@docker01 oglab] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEubuntu 14.04 8fc8ce69135d 36 seconds ago 215MB

3. Create based on Dockerfile

Based on Dockerfile is the most common way, Dockerfile is a text file, using the instructions given to describe the process of creating a new image based on a parent image.

Create an image of python

[root@docker01 ~] # vim Dockerfile FROM debian:stretch-slim LABEL version= "1.0" maintainer= "docker user" RUN apt-get update & &\ apt-get install-y python3 & &\ apt-get clean & &\ rm-rf / var/lib/apt/lists/* [root@docker01 ~] # docker build. -t python:3 save and load images

The save and load commands of the Docker image docker [image] save | docker [image] load to save and load the image.

1. Save to the image

[root@docker01] # docker save-o ubuntu_18.04.tar ubuntu:18.04

2. Load the image

[root@docker01 ~] # docker load-I ubuntu_18.04.tarLoaded image: ubuntu:18.04 [root@docker01 ~] # docker load < ubuntu_18.04.tarLoaded image: ubuntu:18.04 upload mirror image

Push command for Docker image. Docker [image] push is uploaded to the image repository. By default, it is uploaded to the official Docker Hub repository.

Docker tag testfile:0.1 user/test:latest [root@docker01 ~] # docker push user/test:latest The push refers to repository [docker.io/user/test]

User is your user name. Login information will be entered when uploading for the first time, and then the login will be saved in the local ~ / .docker directory.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report