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-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces you how to use Docker image, the content is very detailed, interested friends can refer to, hope to be helpful to you.

When running the container, if the image used does not exist locally, docker is automatically downloaded from the docker image repository. By default, it is downloaded from the Docker Hub public image source.

List a list of images

We can use docker images to list the images that exist on the host.

Root@ubuntu:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 055936d39205 3 weeks ago 5.53MB mysql 5.7 7faa3c53e6d6 3 weeks ago 373MB ubuntu 15.04 d1b55fd07600 3 years ago 131MB ubuntu 16.04 2a697363a870 2 weeks ago 119MB

Description of each option:

REPOSITORY: indicates the warehouse source of the image

TAG: the label of the image

IMAGE ID: mirror ID

CREATED: time when the image was created

SIZE: mirror siz

There can be multiple TAG in the same warehouse source, which represents different versions of the warehouse source. For example, in the ubuntu repository source, there are different versions such as 16.04,15.04 and so on. We use REPOSITORY:TAG to define different images.

Start the container with the appropriate image

If we want to use the ubuntu system image with version 16.04 to run the container, the command is as follows:

Root@ubuntu:~# docker run-it ubuntu:16.04 sh

If you want to use a ubuntu system mirror with version 15.04, the command is as follows:

Root@ubuntu:~# docker run-it ubuntu:15.04 sh

Each parameter is resolved:

Docker: the binary execution file for Docker.

Run: combined with the previous docker to run a container.

-it: it's actually made up of two parameters,-I and-t STDIN: allow you to interact with the standard input in the container. -t: specify a pseudo terminal or terminal in the new container.

Ubuntu:15.04 specifies the image to run. Docker first looks up whether the image exists on the local host. If it does not exist, Docker downloads the public image from the image repository Docker Hub.

Sh: execute the command.

Get a new image

Docker automatically downloads the image when we use a non-existent image on the host. If we want to download the image in advance, we can use the docker pull command to download it.

Root@ubuntu:~# docker pull ubuntu:18.04 18.04: Pulling from library/ubuntu 6abc03819f3e: Pull complete 05731e63f211: Pull complete 0bd67c50d6be: Pull complete Digest: sha256:f08638ec7ddc90065187e7eabdfac3c96e5ff0f6b2f1762cf31a4f49b53000a5 Status: Downloaded newer image for ubuntu:18.04

After the download is complete, we can directly use this image to run the container.

Find Mirror

We can search for images from the Docker Hub website, and the Docker Hub address is: https://hub.docker.com/. We can also use the docker search command to search for images. For example, we need an image of httpd as our web service. We can search httpd with the docker search command to find the right image for us.

NAME: name of the image repository source DESCRIPTION: description of the image OFFICIAL: whether it is officially released by docker

Pull httpd image

We decided to use the image of the official version of httpd in the image above and use the command docker pull to download the image.

Root@ubuntu:~# docker pull httpd Using default tag: latest latest: Pulling from library/httpd 743f2d6c1f65: Already exists c92eb69846a6: Pull complete 2211b052800a: Pull complete aed180197314: Pull complete 7c472a4980a7: Pull complete Digest: sha256:a35ad614c1ffc6fe931f12dc42b682edbdcc62cf78d8edc41499dd90ef0f8003 Status: Downloaded newer image for httpd:latest

After the download is complete, we can use this image.

Create a mirror

When the image we downloaded from the docker image repository does not meet our needs, we can change the image in the following two ways.

1. Update the image from the container that has been created and submit the image

two。 Use the Dockerfile directive to create a new mirror

Update Mirror

Before updating the image, we need to use the image to create a container.

Root@ubuntu:~# docker run-it ubuntu:16.04 sh

Use the apt-get update command to update within the running container.

# apt-get update Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB] Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB] Get:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB] Get:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB] Get:5 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB] Get:6 http:/ / security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [844 kB] Get:7 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB] Get:8 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB] Get:9 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB] Get:10 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [1237 kB] Get:11 http: / / security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.7 kB] Get:12 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [556 kB] Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.1 kB] Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [967 kB] Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [19.1 kB] Get:16 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [7942 B] Get:17 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [8532 B] Get:18 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [6113 B] Fetched 15.8 MB in 8s (1933 kB/s) Reading package lists... Done

After completing the operation, type the exit command to exit the container.

View all containers through docker ps-a:

Root@ubuntu:~# docker ps-a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 12847d9f5071 ubuntu:16.04 "sh" 2 minutes ago Exited (0) 43 seconds ago peaceful_edison 3cec6f5e47c0 httpd "httpd-foreground" 5 minutes ago Exited (0) 5 minutes ago vigilant_heisenberg

At this point, ID is the container of 12847d9f5071, which is changed according to our requirements. We can submit a copy of the container by commanding docker commit.

Root@ubuntu:~# docker commit-m = "has update"-a = "yeqing112" 12847d9f5071 yeqing112/ubuntu:v2sha256:5cce1167ee4c083ff9b87e61a58081805e76d015a12a4811bd152263aa013860

Description of each parameter:

-m: the description information submitted.

-a: specify the author of the image.

12847d9f5071: container ID.

Yeqing112/ubuntu:v2: specifies the name of the target image to be created.

We can use the docker images command to view our new mirror yeqing112/ubuntu:v2:

Root@ubuntu:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEyeqing112/ubuntu v2 5cce1167ee4c 2 minutes ago 144MBubuntu 16.04 2a697363a870 2 weeks ago 119MB

We can see from the above information that we have updated the image, and the file size is obviously larger than before.

Build an image

In addition to the above method of updating the image, we can also use the command docker build to build a new image from scratch. To do this, we need to create a Dockerfile file that contains a set of instructions to tell Docker how to build our image.

Root@ubuntu:~# cat Dockerfile FROM ubuntu:16.04MAINTAINER Fisher "service@urlos.com" RUN apt-get updateEXPOSE 22EXPOSE 80CMD / bin/bash

Each instruction creates a new layer on the mirror, and each instruction must be prefixed in uppercase.

The first FROM, which specifies which mirror source to use

The RUN instruction tells docker to execute the command in the image and what is installed

Here we are the same, execute an apt-get update update command

Then, we use the Dockerfile file, through docker build-t yeqing112/ubuntu:v3. Command to build a new image.

Root@ubuntu:~# root@ubuntu:/home# docker build-t yeqing112/ubuntu:v3 .Sending build context to Docker daemon 422.4kBStep 1 RUN apt-get update 6: FROM ubuntu:16.04-- > 2a697363a870Step 2 xenial InRelease 6: MAINTAINER Fisher "service@urlos.com"-> Running in 92229e7a091aRemoving intermediate container 92229e7a091a-- > 9a89f4debc7dStep 3 xenial InRelease 6: RUN apt-get update-- > Running in d709ef335784Get:1 xenial InRelease [247 kB] Get:2 http : / / security.ubuntu.com/ubuntu xenial-security InRelease [109kB] Get:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109kB] Get:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107kB] Get:5 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [844kB] Get:6 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB] Get:7 http://archive.ubuntu .com / ubuntu xenial/restricted amd64 Packages [14.1 kB] Get:8 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB] Get:9 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.7 kB] Get:10 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [556 kB] Get:11 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB] Get:12 http:// Archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [1237 kB] Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.1 kB] Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [967 kB] Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [19.1 kB] Get:16 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [7942 B] Get:17 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [8532 B] Get:18 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [6113 B] Fetched 15.8 MB in 5s (3051 kB/s) Reading package lists...Removing intermediate container d709ef335784-> 540751496556Step 4 Reading package lists...Removing intermediate container d709ef335784 6: EXPOSE 22-> Running in 9ae528bc083fRemoving intermediate container 9ae528bc083f-> cfccc621a52dStep 5 Running in 9ae528bc083fRemoving intermediate container 9ae528bc083f 6: EXPOSE 80-> Running in efd88e9349c1Removing intermediate container efd88e9349c1-- > 428cb5221a06Step 6 bin/bash 6: CMD / bin/bash-- > Running in 2bc08b664f97Removing intermediate container 2bc08b664f97-- > 9c55ea9b9ee8Successfully built 9c55ea9b9ee8Successfully tagged yeqing112/ubuntu:v3

Parameter description:

-t: specify the name of the target image to be created

.: the directory where the Dockerfile file resides. You can specify the absolute path of Dockerfile. After the construction is successful, use docker images to view the image:

Root@ubuntu:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEyeqing112/ubuntu v3 9c55ea9b9ee8 9 seconds ago 144MByeqing112/ubuntu v2 5cce1167ee4c 16 minutes ago 144MBubuntu 16.04 2a697363a870 2 weeks ago 119MB

As can be seen from the above information, the new image v3 is the same size as the v2 file, and both images only perform apt-get update.

Set image label

We can use the docker tag command to add a new label to the image.

Root@ubuntu:/home# docker tag 9c55ea9b9ee8 yeqing112/ubuntu:v3-1

Docker tag image ID, here is 9c55ea9b9ee8, user name, image source name (repository name), and new tag signature (tag).

Using the docker images command, you can see that ID has an extra v3-1 label for the image of 9c55ea9b9ee8.

Root@ubuntu:/home# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEyeqing112/ubuntu v3 9c55ea9b9ee8 21 minutes ago 144MByeqing112/ubuntu v3-1 9c55ea9b9ee8 21 minutes ago 144MByeqing112/ubuntu v2 5cce1167ee4c 38 minutes Ago 144MBubuntu 16.04 2a697363a870 2 weeks ago 119MB on how to use Docker images, so much for sharing here. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report