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

Introduction to Docker Image and Container Operation Command

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains the basic usage after installing docker and the introduction of commands, mainly docker image operation and container operation command.

Of course, there are a lot of commands related to docker, and you can use the docker help command to view the corresponding directories and related prompt commands.

Docker image operation command [root@docker ~] # which docker/usr/bin/docker1, image search: docker search

Command description: search from Docker Hub and display the image you need

Command format: docker search [options] term

Options:-f-- filter filter Filter output based on conditions provided--format string Pretty-print search using a Go template--limit int Max number of search results (default 25)-- no-trunc Don't truncate output

-s:-s 100 # indicates images with more than 100 favorites

Demonstration of example operation:

[root@docker] # docker search nginxNAME DESCRIPTION STARS OFFICIAL AUTOMATEDnginx Official build of Nginx. 12968 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con... 1772 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of … 764 [OK]... / / omit part [root@docker ~] # docker search-s 100 nginxFlag-- stars has been deprecated, use-- filter=stars=3 insteadNAME DESCRIPTION STARS OFFICIAL AUTOMATEDnginx Official build of Nginx. 12968 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con... 1772 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of … [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS... one hundred and four

Brief description:

NAME: image name DESCRIPTION: description information of the image STARS: star. The higher the image, the more people you like. OFFICIAL: whether it is an official release AUTOMATED: automatically created

This command is used as a retrieval command. Next, we need to get each image you selected after retrieval.

2. Image acquisition: docker pull

Command description: pull from the image warehouse or update the specified image

Command format: docker pull [OPTIONS] NAME [: TAG | @ DIGEST]

Options:-a,-- all-tags Download all tagged images in the repository-- disable-content-trust Skip image verification (default true)-Q,-- quiet Suppress verbose output

OPTIONS description:

-a: pull all tagged images-- disable-content-trust: ignore the verification of images. Default is enabled.

Example demonstration: pull nginx image as an example

[root@docker ~] # docker pull nginxUsing default tag: latestlatest: Pulling from library/nginxc499e6d256d6: Pull complete74cda408e262: Pull completeffadbd415ab7: Pull completeDigest: sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596Status: Downloaded newer image for nginx:latestdocker.io/library/nginx:lates

So we pulled a nginx image, so how do we check it?

3. View image information: docker images

Command description: lists local images

Command format: docker images [OPTIONS] [REPOSITORY [: TAG]]

OPTIONS description:

-a: list all local images (including the middle image layer, which is filtered by default);-- digests: display the summary information of the image;-f: display the image that meets the criteria;-- format: the template file with the specified return value;-- no-trunc: display the complete image information;-Q: display only the image ID.

Example demonstration:

[root@docker ~] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest ed21b7a8aee9 6 days ago 127MB# is equivalent to the following command [root@docker ~] # docker image ls REPOSITORY TAG IMAGE ID CREATED SIZEnginx latest Ed21b7a8aee9 6 days ago 127MB

Brief description:

REPOSITORY: warehouse name TAG: tag IMAGE ID: image idCREATE: creation status (by time) SIZE: file size

PS: add docker inspect IMAGE ID to view the details of the image.

Let's take a look at the role of this TAG.

4. Image tag operation: docker tag

Command description: Mark the local image and put it in a warehouse

Command format: docker tag SOURCE_IMAGE [: TAG] TARGET_IMAGE [: TAG]

Example demonstration:

[root@docker ~] # docker tag nginx:latest nginx:lokott [root@docker ~] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest ed21b7a8aee9 6 days ago 127MBnginx lokott ed21b7a8aee9 6 days ago 127MB

Suppose we find an error when we create it, or how do we delete the image when we don't need it? Keep looking!

5. Delete image: docker rmi

Command description: delete one or more local mirrors

Command format: docker rmi [OPTIONS] IMAGE [IMAGE...]

Options:

-f,-- force Force removal of the image-- no-prune Do not delete untagged parents

Example demonstration:

[root@docker ~] # docker rmi nginx:lokott Untagged: nginx:lokott [root@docker ~] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest ed21b7a8aee9 6 days ago 127MB [root@docker ~] # docker rmi nginx:latestUntagged: nginx:latestUntagged: nginx@sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596Deleted: sha256:ed21b7a8aee9cc677df6d7f38a641fa0e3c05f65592c592c9f28c42b3dd89291Deleted: sha256:8a305f371a6c3c445a1dfc500c1364743868a269ab8cdaf95902692e82168352Deleted: sha256:d079ef06ec1f10a8050887365f9a940b39547ba6bcc46b16a463e740984f3223Deleted: sha256:c3a984abe8a88059915bb6c7a1d249fd1ccc16d931334ac8816540b0eb686b45

Deleting a tagged image is essentially an untagged operation, which is different from deleting the original image file.

Try not to delete the image by deleting the ID number, and be careful to delete all containers that depend on the image before deleting the image.

So we need to consider how to save the image after we have pulled the image. This needs to be done using the following command.

6. Check out image: docker save

The so-called save-out, we imagine such a scenario: when you need to migrate an image from one machine to another, you need to save the image to the cost of the file. this process is called saving out the image.

You can simply understand that it is easy to use the image into a compressed package.

Command description: saves the specified image as a tar archive file

Command format: docker save [OPTIONS] IMAGE [IMAGE...]

OPTIONS description:

-o: the file to which you output.

Example demonstration:

[root@docker] # docker save-o / opt/nginx nginx:latest [root@docker] # ll / opt/ total dosage 127560drwx--x--x. 4 root root 28 April 6 09:45 containerd-rw-. 1 root root 130618880 April 6 14:04 nginxdrwxr-xr-x. 2 root root 6 October 31 2018 rh

So if there is a check out, there must be a corresponding load operation. We will copy a copy of the saved file locally to another machine. We need to use this image. We can load the image with the following command

7. Load the image: docker load

Command description: load image file

Command format: docker load [OPTIONS]

Options:

-I,-- input string Read from tar archive file, instead of STDIN-Q,-- quiet Suppress the load output

Example demonstration:

[root@docker ~] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE# one: [root@docker ~] # docker load

< /opt/nginx c3a984abe8a8: Loading layer 72.48MB/72.48MB99134ec7f247: Loading layer 58.11MB/58.11MBd37eecb5b769: Loading layer 3.584kB/3.584kBLoaded image: nginx:latest[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest ed21b7a8aee9 6 days ago 127MB#二:从tar存档文件中读取,而不是从STDIN中读取[root@docker ~]# docker load --input /opt/nginx #或者使用-i代替--input也是一样的c3a984abe8a8: Loading layer 72.48MB/72.48MB99134ec7f247: Loading layer 58.11MB/58.11MBd37eecb5b769: Loading layer 3.584kB/3.584kBLoaded image: nginx:latest8、上传镜像:docker login (不做细说以及演示) 命令描述:将自己本地的镜像制作好后上传到仓库中 命令格式:docker login 结合docker push NAME[:TAG]使用 Docker容器操作命令 关于Docker容器的操作笔者换一种风格来介绍,但是还是会通过实例来方便大家理解。 首先,容器简单而言,是基于镜像的一个运行实例,是独立运行的一个或一组应用以及其所必需的运行环境,包括文件系统、系统类库、 shell环境等。镜像是只读模板,而容器会给这个只读模板一个额外的可写层。 接下来就容器来讲述具体的操作命令。 1、容器创建与启动 容器创建就是将镜像加载到容器的过程,因为是docker是轻量级的应用工具,用户可以随时进行创建或者删除。新建的容器默认处于停止状态,不允许任何程序,需要在其中发起一个进程来启动容器,这个进程就是这个容器的唯一进程,因此在该进程结束的时候,容器也会完全停止。停止的容器可以重新启动并且保留原来的修改。 容器的创建 容器创建命令:docker create 命令格式:docker create [OPTIONS] IMAGE [COMMAND] [ARG...] 常用的options: -i:让容器的输入保持打开-t:让docker分配一个伪终端 实例演示: [root@docker ~]# docker create -it nginx:latest /bin/basha1a2e8468189b146b0215971cf76947987d8fc3f42c58e77371e8d48afb030d1[root@docker ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa1a2e8468189 nginx:latest "/bin/bash" 7 seconds ago Created gallant_feynman 相关说明: CONTAINER ID:容器ID号IMAGE:使用的镜像COMMAND :CREATED:显示操作时间STATUS:显示状态,created表示已创建PORTS:端口 信息和使用的连接类型(tcp/udp)NAMES:自动分配容器的名称 容器的启动 命令:docker start 命令格式:docker start [OPTIONS] CONTAINER [CONTAINER...] 实例演示: [root@docker ~]# docker start a1a2e8468189a1a2e8468189[root@docker ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa1a2e8468189 nginx:latest "/bin/bash" 10 minutes ago Up 2 seconds 80/tcp gallant_feynman 启动后,发现端口信息出来了,并且显示是up状态,表示容器已经是启动状态了。 下面看另一种启动容器的方式: [root@docker ~]# docker run CentOS /usr/bin/bash -c ls /Unable to find image 'centos:latest' locallylatest: Pulling from library/centos8a29a15cefae: Pull complete Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700Status: Downloaded newer image for centos:latestbindevetchomeliblib64lost+foundmediamntoptprocrootrunsbinsrvsystmpusrvar[root@docker ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4835176a7d8e centos "/usr/bin/bash -c ls…" About a minute ago Exited (0) About a minute ago affectionate_keldysha1a2e8468189 nginx:latest "/bin/bash" 28 minutes ago Up 17 minutes 80/tcp gallant_feynman -c参数表示的是需要执行的命令,这种方式适用于一次执行,Exited (0) 表示正常正确退出,及终止运行。 docker run的命令执行流程为:先从本地查找镜像(本地没有image不表示容器不依赖image),如果没有会从公共仓库中下载;再通过镜像创建并且启动一个容器;分配一个文件系统给容器,在只读的镜像层外面挂载一层可读写层;从宿主机配置的网桥接口中桥接一个虚拟接口到容器中;分配一个地址池中的IP地址给容器;执行用户指定的应用程序;执行之后容器被终止(后台运行除外)。 后台运行:run 后面加参数-d即可。 2、容器运行与终止 想要在运行中的容器停下来,使用docker stop 命令 命令格式:docker stop [OPTIONS] CONTAINER [CONTAINER...] 实例演示: [root@docker ~]# docker stop a1a2e8468189a1a2e8468189[root@docker ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4835176a7d8e centos "/usr/bin/bash -c ls…" 9 minutes ago Exited (0) 9 minutes ago affectionate_keldysha1a2e8468189 nginx:latest "/bin/bash" 36 minutes ago Exited (0) 3 seconds ago gallant_feynman3、容器的进入 想要进入容器进行相应操作时可以使用docker exec命令进入运行着的容器。 命令格式:docker exec [OPTIONS] CONTAINER COMMAND [ARG...] [root@docker ~]# docker ps -a #检查容器是否是运行状态(up)CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4835176a7d8e centos "/usr/bin/bash -c ls…" 12 minutes ago Exited (0) 12 minutes ago affectionate_keldysha1a2e8468189 nginx:latest "/bin/bash" 38 minutes ago Up 4 seconds 80/tcp gallant_feynman[root@docker ~]# docker exec -it a1a2e8468189 /bin/bash #进入伪终端root@a1a2e8468189:/# lsbin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr varroot@a1a2e8468189:/# exit #使用exit退出伪终端exit[root@docker ~]#4、容器的导出和导入 类似镜像,用户也可以将容器从一台机器迁移到另一台机器。在迁移过程中,首先需要将已经创建好的容器导出为文件,使用docker export实现,无论这个容器是处于运行状态还是停止状态均可以导出。导出之后可将导出文件传输到其他机器,通过对应的导入命令实现迁移过程。 容器导出命令:docker export 命令格式:docker export [OPTIONS] CONTAINER 实例演示:一个运行状态一个停止状态,都演示 [root@docker ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4835176a7d8e centos "/usr/bin/bash -c ls…" 21 minutes ago Exited (0) 21 minutes ago affectionate_keldysha1a2e8468189 nginx:latest "/bin/bash" 48 minutes ago Up 9 minutes 80/tcp gallant_feynman 运行中的容器导出: [root@docker ~]# docker export a1a2e8468189 >

Nginx_ containment [root @ docker ~] # lsanaconda-ks.cfg initial-setup-ks.cfg nginx nginx_contain public template video picture document download music desktop [root@docker] # ll total usage 253448. 1 root root 1954 April 6 08:49 anaconda-ks.cfg-rw-r--r--. 1 root root 2002 April 6 08:50 initial-setup-ks.cfg-rw-. 1 root root 130618880 April 6 14:04 nginx # image checked out file-rw-r--r--. 1 root root 128899072 April 6 16:04 nginx_contain # container export file with different permissions

Terminated container export:

[root@docker ~] # docker export 4835176a7d8e > centos_ containment [root @ docker ~] # lsanaconda-ks.cfg initial-setup-ks.cfg nginx_contain template picture download desktop centos_contain nginx public video document music

The exported file is copied locally to the remote server, and then imported using the docker import command to become a mirror

Container import command: docker import

Command format: docker import [OPTIONS] file | URL |-[REPOSITORY [: TAG]]

Example demonstration:

[root@docker ~] # docker import centos_contain centos_new:54sha256: f32b5354c81de72a71af50468d09803aa9e62e08741e3cd3e84c10c9a74e2204 [root @ docker ~] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos_new 54 f32b5354c81d 10 seconds ago 0Bnginx latest ed21b7a8aee9 6 days ago 127MBcentos latest 470671670cac 2 months ago 237MB

Or use cat file name | docker import-Image name: tag can execute the command.

5. Deletion of containers

Deletion of containers: docker rm

Command format: docker rm [OPTIONS] CONTAINER [CONTAINER...]

Example demonstration:

[root@docker] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4835176a7d8e centos "/ usr/bin/bash-c ls..." 31 minutes ago Exited (0) 31 minutes ago affectionate_keldysha1a2e8468189 nginx:latest "/ bin/bash" 58 minutes ago Up 19 minutes 80/tcp gallant_feynm

Delete terminated containers:

[root@docker ~] # docker rm 4835176a7d8e4835176a7d8e [root@docker ~] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa1a2e8468189 nginx:latest "/ bin/bash" 59 minutes ago Up 20 minutes 80/tcp gallant_feynman

Deleted successfully!

Delete containers that are still running:

[root@docker ~] # docker rm a1a2e8468189 Error response from daemon: You cannot remove a running container a1a2e8468189b146b0215971cf76947987d8fc3f42c58e77371e8d48afb030d1. Stop the container before attempting removal or force remove

Error has been found. You can't delete the container in the running state like this. Your action is to stop the container before you try to delete it or force it to be deleted.

The former is recommended, and the latter only needs the following operations, but try not to use it casually!

[root@docker ~] # docker rm a1a2e8468189-fa1a2e8468189 [root@docker ~] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Note: the default storage directory of docker is in the / var/lib/docker directory, where all the images, containers, logs and other contents of docker are stored. Of course, you can also use large-capacity partitions to store these contents, and generally choose to build on LVM logical volumes to facilitate subsequent expansion operations.

Summary

This article describes the operation of docker basic commands (mirrors and containers), which are easy to understand through real cases. For the introduction and installation of docker, you can refer to the author's previous article.

Thank you for reading!

The original text is from: https://www.linuxprobe.com/docker-help.html

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