Docker常用命令总结

news2024/11/16 8:36:03
  • 基础命令

1.启动docker

systemctl start docker

2.关闭docker

systemctl stop docker

3.设置docker为自启动

systemctl enable --now docker

4.重启docker

systemctl restart docker

3.查看docker版本信息

docker version

4.查看docker详细信息

docker info

Client:
Debug Mode: false            #client端是否开启 debug

Server:
Containers: 2                #当前主机运行的容器总数

Running: 0                   #有几个容器是正在运行的

Paused: 0                    #有几个容器是暂停的

Stopped: 2                   #有几个容器是停止的

Images: 4                    #当前服务器的镜像数

Server Version: 19.03.5      #服务端版本

Storage Driver: overlay2     #正在使用的存储引擎

Backing Filesystem: extfs    #后端文件系统,即服务器的磁盘文件系统

Supports d_type: true        #是否支持 d_type

Native Overlay Diff: true    #是否支持差异数据存储

Logging Driver: json-file    
#日志类型,每个容器的标准输出以日志存放在
/var/lib/docker/containers/<CONTAINERID>/<CONTAINER ID>-json.log
                              
Cgroup Driver: cgroupfs      #Cgroups 类型

Plugins:                     #插件

Volume: local                #卷

Network: bridge host ipvlan macvlan null overlay   #overlay 跨主机通信

Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog                                             #日志类型

Swarm: inactive              #是否支持 swarm

Runtimes: runc               #已安装的容器运行时

Default Runtime: runc        #默认使用的容器运行时

Init Binary: docker-init     #初始化容器的守护进程,即 pid 为 1 的进程

containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339 #版本

runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657 #runc版本

init version: fec3683        #init 版本

Security Options:            #安全选项

apparmor #安全模块,https://docs.docker.com/engine/security/apparmor/

seccomp #安全计算模块,即制容器操作,https://docs.docker.com/engine/security/seccomp/

Profile: default             #默认的配置文件

Kernel Version: 4.15.0-29-generic #宿主机内核版本

Operating System: Ubuntu 18.04.1 LTS #宿主机操作系统

OSType: linux #宿主机操作系统类型

Architecture: x86_64 #宿主机架构

CPUs: 1 #宿主机CPU数量

Total Memory: 962MiB #宿主机总内存

Name: ubuntu1804.wang.org #宿主机hostname

ID: IZHJ:WPIN:BRMC:XQUI:VVVR:UVGK:NZBM:YQXT:JDWB:33RS:45V7:SQWJ #宿主机ID

Docker Root Dir: /var/lib/docker 
#宿主机关于docker数据的保存目录,建议使用独立SSD的磁
盘,保证性能和空间

Debug Mode: false #server端是否开启debug

Registry: https://index.docker.io/v1/ #仓库路径

Labels:
Experimental: false #是否测试版

Insecure Registries:
127.0.0.0/8 : #非安全的镜像仓库

Registry Mirrors:
https://si7y70hh.mirror.aliyuncs.com/ #镜像仓库

Live Restore Enabled: false #是否开启活动重启 (重启docker-daemon 不关闭容器 )

WARNING: No swap limit support #系统警告信息 (没有开启 swap 资源限制 )

#解决上述SWAP报警提示:

1.修改配置文件

vim /etc/default/grub

----省略---

GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 swapaccount=1" #修改此行

---省略---

2.生效修改后的grub

#Ubuntu执行

update-grub

#Centos执行

grub2-mkconfig -o /boot/grub2/grub.cfg

3.重启系统

reboot

5.查看docker命令的帮助

docker --help

6.查看docker子命令的帮助

#比如忘记docker pull的选项和可带的参数

docker pull --help

  • 镜像命令

1.拉取镜像

#加:tag(版本号)则是拉取指定版本

#不加tag(版本号)即拉取docker仓库中 该镜像的最新版本latest

格式:docker pull [镜像名]

格式:docker pull [镜像名:tag]

#拉取最新版本
[root@centos7-temp ~]# docker pull nginx
Using default tag: latest #默认下载最新版本
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete #分层下载
a9edb18cadd1: Pull complete 
589b7251471a: Pull complete 
186b1aaa4aa6: Pull complete 
b4df32aa5a72: Pull complete 
a0bcbecc962e: Pull complete 
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 #摘要
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest #下载的完整地址

#拉取指定版本
[root@centos7-temp ~]# docker pull nginx:1.18.0
1.18.0: Pulling from library/nginx
f7ec5a41d630: Pull complete 
0b20d28b5eb3: Pull complete 
1576642c9776: Pull complete 
c12a848bad84: Pull complete 
03f221d9cf00: Pull complete 
Digest: sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0
Status: Downloaded newer image for nginx:1.18.0
docker.io/library/nginx:1.18.0

#查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              605c77e624dd        12 months ago       141MB
nginx               1.18.0              c2c45d506085        21 months ago       133MB

2.查看镜像分层历史

格式:docker image history [镜像名]

[root@centos7-temp ~]# docker image history nginx
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
605c77e624dd        12 months ago       /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  EXPOSE 80                    0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B                  
<missing>           12 months ago       /bin/sh -c #(nop) COPY file:09a214a3e07c919a…   4.61kB              
<missing>           12 months ago       /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7…   1.04kB              
<missing>           12 months ago       /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0…   1.96kB              
<missing>           12 months ago       /bin/sh -c #(nop) COPY file:65504f71f5855ca0…   1.2kB               
<missing>           12 months ago       /bin/sh -c set -x     && addgroup --system -…   61.1MB              
<missing>           12 months ago       /bin/sh -c #(nop)  ENV PKG_RELEASE=1~bullseye   0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  ENV NJS_VERSION=0.7.1        0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  ENV NGINX_VERSION=1.21.5     0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           12 months ago       /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           12 months ago       /bin/sh -c #(nop) ADD file:09675d11695f65c55…   80.4MB 

3.查看镜像的元数据(后面加容器,也可查看容器的元数据)

#以json格式显示

格式:docker inspect [镜像/容器]

#查看nginx的镜像元数据
[root@centos7-temp ~]# docker inspect nginx

4.搜索镜像

格式:docker search [镜像名]

#搜索STARS>9000的mysql镜像
[root@centos7-temp ~]# docker search --filter=STARS=9000 mysql
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql               MySQL is a widely used, open-source relation…   13639               [OK]          

5.查看本地镜像列表

docker images

#查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    605c77e624dd   12 months ago   141MB
nginx        1.18.0    c2c45d506085   21 months ago   133MB

##显示完整的ImageID
[root@centos7-temp ~]# docker images --no-trunc
REPOSITORY   TAG       IMAGE ID                                                      CREATED         SIZE
nginx        latest    sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85   12 months ago   141MB
nginx        1.18.0    sha256:c2c45d506085d300b72a6d4b10e3dce104228080a2cf095fc38333afe237e2be   21 months ago   133MB

#显示结果信息说明:

REPOSITORY      #镜像所属的仓库名称
TAG             #镜像版本号(标识符),默认为latest
IMAGE ID        #镜像唯一ID标识,如果ID相同,说明是同一个镜像有多个名称
CREATED         #镜像在仓库中被创建时间
VIRTUAL SIZE    #镜像的大小

5.导出和导入镜像

(1)导出镜像

#利用docker save命令可以将从本地镜像导出为一个打包 tar文件,然后复制到其它主机进行导入使用

格式:docker save 镜像名 -o tar文件路径

选项:-o:将输出字符串写入文件

用法:

#查看本地镜像列表
[root@docker1 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    605c77e624dd   12 months ago   141MB
nginx        1.18.0    c2c45d506085   21 months ago   133MB
[root@docker1 ~]# docker save nginx:1.18.0 -o /data/nginx.tar

#打包镜像为tar文件
[root@docker1 ~]# docker save nginx:1.18.0 -o /data/nginx.tar

#查看tar文件
[root@docker1 ~]# ls /data/nginx.tar 
/data/nginx.tar

#上传至另一台主机上
[root@docker1 ~]# scp /data/nginx.tar 10.0.0.100:/data
The authenticity of host '10.0.0.100 (10.0.0.100)' can't be established.
ECDSA key fingerprint is SHA256:ZyQrEY2+tXpQpgIXx8tA8ZSzHLXmhChf2aQrbEtX+Ls.
ECDSA key fingerprint is MD5:1b:f1:e3:71:ff:8c:66:c8:39:63:e0:47:82:df:15:cc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.100' (ECDSA) to the list of known hosts.
root@10.0.0.100's password: 
nginx.tar                                                                                  100%  131MB  78.4MB/s   00:01

#打包所有镜像的方法:

#打包所有镜像至不同文件
[root@centos7-temp ~]# docker images | awk 'NR!=1{print $1,$2}' | while read repo tag\
> ;do docker save $repo:$tag -o /opt/$repo-$tag.tar ;done

#打包所有镜像至同一个文件
方法1: 使用image ID导出镜像,在导入后的镜像没有REPOSITORY和TAG,显示为<none>
[root@centos7-temp ~]#docker save `docker images -qa` -o all.tar
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              605c77e624dd        12 months ago       141MB
<none>              <none>              3218b38490ce        12 months ago       516MB


方法2:将所有镜像导入到一个文件中,此方法导入后可以看REPOSITORY和TAG
[root@centos7-temp ~]#docker save `docker images | awk 'NR!=1{print $1":"$2}'` -o all.tar
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              605c77e624dd        12 months ago       141MB
mysql               latest              3218b38490ce        12 months ago       516MB

方法3:将所有镜像导入到一个文件中,此方法导入后可以看REPOSITORY和TAG
[root@centos7-temp ~]#docker image save `docker image ls --format "{{.Repository}}:{{.Tag}}"` -o all.tar
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              605c77e624dd        12 months ago       141MB
mysql               latest              3218b38490ce        12 months ago       516MB

(2)导入镜像

#主机ip:10.0.0.100

#利用docker load命令可以将镜像导出的打包或压缩文件再导入

格式:docker load -i tar文件路径

选项:-i:输入字符串从tar存档文件读取

用法:

#查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

#导入镜像
[root@centos7-temp ~]# docker load -i /data/nginx.tar 
7e718b9c0c8c: Loading layer [==================================================>]  72.52MB/72.52MB
f3fdf88f1cb7: Loading layer [==================================================>]   64.6MB/64.6MB
6fcbf7acaafd: Loading layer [==================================================>]  3.072kB/3.072kB
4fe7d87c8e14: Loading layer [==================================================>]  4.096kB/4.096kB
4fa6704c8474: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: nginx:1.18.0

#再次查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.18.0              c2c45d506085        21 months ago       133MB

(3)一次导出多个镜像,然后导入

#查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    605c77e624dd   12 months ago   141MB
nginx        1.18.0    c2c45d506085   21 months ago   133MB

#打包两个不同版本的nginx镜像
[root@centos7-temp ~]# docker save nginx > /all.tar

#查看tar文件
[root@centos7-temp ~]# ll -h /all.tar 
-rw-r--r-- 1 root root 270M 1月   1 16:54 /all.tar

#删除原来的两个镜像
[root@centos7-temp ~]# docker rmi -f `docker images -q`
Untagged: nginx:latest
Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
Untagged: nginx:1.18.0
Untagged: nginx@sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0
Deleted: sha256:c2c45d506085d300b72a6d4b10e3dce104228080a2cf095fc38333afe237e2be
Deleted: sha256:43d6c481a041dbcc1d8ea9c565b1b692bcb28da3414683c316703c669c012ebc
Deleted: sha256:defebc732c194dd0b5b39e20c4d014896ce120207f5dfdb41ed6696b0e8224d6
Deleted: sha256:4ea0f2550407442f808812429981c0b62d8dd6a531db8a412640293a1faf8f3c
Deleted: sha256:778ca58cf39b8fa0776ade88562750a035a24ec5afb7dc4ab2aa892b2c09769d
Deleted: sha256:7e718b9c0c8c2e6420fe9c4d1d551088e314fe923dce4b2caf75891d82fb227d

#再次查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

#导入镜像
[root@centos7-temp ~]# docker load -i /all.tar
7e718b9c0c8c: Loading layer [==================================================>]  72.52MB/72.52MB
f3fdf88f1cb7: Loading layer [==================================================>]   64.6MB/64.6MB
6fcbf7acaafd: Loading layer [==================================================>]  3.072kB/3.072kB
4fe7d87c8e14: Loading layer [==================================================>]  4.096kB/4.096kB
4fa6704c8474: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: nginx:1.18.0
2edcec3590a4: Loading layer [==================================================>]  83.86MB/83.86MB
e379e8aedd4d: Loading layer [==================================================>]     62MB/62MB
b8d6e692a25e: Loading layer [==================================================>]  3.072kB/3.072kB
f1db227348d0: Loading layer [==================================================>]  4.096kB/4.096kB
32ce5f6a5106: Loading layer [==================================================>]  3.584kB/3.584kB
d874fd2bc83b: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:latest

#再次查看本地镜像列表
[root@centos7-temp ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    605c77e624dd   12 months ago   141MB
nginx        1.18.0    c2c45d506085   21 months ago   133MB

6.删除镜像

格式:docker rmi  [选项] [镜像名]

选项:-f:强制删除

用法:

#删除指定的镜像
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              605c77e624dd        12 months ago       141MB
mysql               latest              3218b38490ce        12 months ago       516MB
[root@centos7-temp ~]# docker rmi -f nginx
Untagged: nginx:latest
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              3218b38490ce        12 months ago       516MB

#删除多个镜像
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              605c77e624dd        12 months ago       141MB
mysql               latest              3218b38490ce        12 months ago       516MB
[root@centos7-temp ~]# docker rmi nginx mysql
Untagged: nginx:latest
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
Untagged: mysql:latest
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
[root@centos7-temp ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

#删除所有的镜像
[root@centos7-temp ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
<none>       <none>    605c77e624dd   12 months ago   141MB
mysql        latest    3218b38490ce   12 months ago   516MB
[root@centos7-temp ~]# docker rmi -f `docker images -q`
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
Untagged: mysql:latest
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
[root@centos7-temp ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

7.镜像打标签

格式:docker tag 旧TAG 新TAG

用法:

  •  容器命令

1.启动容器

注意: 容器启动后,如果容器内没有前台运行的进程,将自动退出停止

格式:docker run [选项] [镜像名] [shell命令] [参数]

选项:

--name:指定容器名称

-it:生成一个交互式的终端

-h:指定容器的主机名

--rm:退出时自动删除容器

-d:后台运行容器

--restart:定义容器的重启策略

-P:随机端口映射

-p:指定端口映射

#启动容器的流程:

用法: 

#运行第一个容器
[root@centos7-temp ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

#查看本地镜像列表,生成了hello-world镜像
[root@centos7-temp ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         1.18.0    605c77e624dd   12 months ago   141MB
nginx         latest    605c77e624dd   12 months ago   141MB
mysql         latest    3218b38490ce   12 months ago   516MB
hello-world   latest    feb5d9fea6a5   15 months ago   13.3kB

#查看容器运行进程
[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
08be02244e32   hello-world   "/hello"   56 seconds ago   Exited (0) 55 seconds ago             dazzling_banzai
#一次性运行容器中的命令
[root@centos7-temp ~]#docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS                     PORTS     NAMES

[root@centos7-temp ~]# docker run nginx echo "Hello "
Hello

[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS                     PORTS     NAMES
96ccb61ea8cf   nginx         "/docker-entrypoint.…"   5 seconds ago   Exited (0) 4 seconds ago             hopeful_carver

#指定容器名称
[root@centos7-temp ~]# docker run --name a1 nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/01/04 09:35:50 [notice] 1#1: using the "epoll" event method
2023/01/04 09:35:50 [notice] 1#1: nginx/1.21.5
2023/01/04 09:35:50 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2023/01/04 09:35:50 [notice] 1#1: OS: Linux 3.10.0-1160.81.1.el7.x86_64
2023/01/04 09:35:50 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/01/04 09:35:50 [notice] 1#1: start worker processes
2023/01/04 09:35:50 [notice] 1#1: start worker process 31
2023/01/04 09:35:50 [notice] 1#1: start worker process 32
^C2023/01/04 09:37:22 [notice] 1#1: signal 2 (SIGINT) received, exiting
2023/01/04 09:37:22 [notice] 31#31: exiting
2023/01/04 09:37:22 [notice] 31#31: exit
2023/01/04 09:37:22 [notice] 32#32: exiting
2023/01/04 09:37:22 [notice] 32#32: exit
2023/01/04 09:37:22 [notice] 1#1: signal 17 (SIGCHLD) received from 31
2023/01/04 09:37:22 [notice] 1#1: worker process 31 exited with code 0
2023/01/04 09:37:22 [notice] 1#1: signal 29 (SIGIO) received
2023/01/04 09:37:22 [notice] 1#1: signal 17 (SIGCHLD) received from 32
2023/01/04 09:37:22 [notice] 1#1: worker process 32 exited with code 0
2023/01/04 09:37:22 [notice] 1#1: exit

[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS                     PORTS     NAMES
20c5cc588587   nginx     "/docker-entrypoint.…"   About a minute ago   Exited (0) 9 seconds ago             a1
#运行交互式容器
[root@centos7-temp ~]# docker run -it nginx sh
# ls
bin   dev		   docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc			 lib   media  opt  root  sbin  sys  usr
# 
#退出交互式容器,容器不在运行
# exit
[root@centos7-temp ~]# 

#查看运行的容器
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#退出交互式容器,容器继续运行
同时按三个键,ctrl+p+q

#查看运行的容器
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
d17aecdf3a46   nginx     "/docker-entrypoint.…"   19 seconds ago   Up 18 seconds   80/tcp    ecstatic_archimedes
#设置容器内的主机名
[root@centos7-temp ~]# docker run -it -h centos7 alpine
/ # hostname
centos7
/ # cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.2	centos7
/ # 
#一次性运行容器,退出后立即删除,用于测试
[root@centos7-temp ~]# docker run --rm alpine cat /etc/issue
Welcome to Alpine Linux 3.17
Kernel \r on an \m (\l)

#查看运行的容器
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#前台运行容器,会占用终端
[root@centos7-temp ~]# docker run nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/01/04 15:02:23 [notice] 1#1: using the "epoll" event method
2023/01/04 15:02:23 [notice] 1#1: nginx/1.21.5
2023/01/04 15:02:23 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2023/01/04 15:02:23 [notice] 1#1: OS: Linux 3.10.0-1160.81.1.el7.x86_64
2023/01/04 15:02:23 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/01/04 15:02:23 [notice] 1#1: start worker processes
2023/01/04 15:02:23 [notice] 1#1: start worker process 31
2023/01/04 15:02:23 [notice] 1#1: start worker process 32

#后台运行容器
[root@centos7-temp ~]# docker run -d nginx
5ede6fcb99bd6064be801beac288d2ce6de8d62e5f38dad4e2f756ee0de8dcd0

[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
5ede6fcb99bd   nginx     "/docker-entrypoint.…"   10 seconds ago   Up 10 seconds   80/tcp    modest_perlman

#设置容器开机自启动(默认不会自启动)
[root@centos7-temp ~]# docker run -d --name nginx --restart=always nginx
6aea8688df49ffd012e57a3c46bfa1e3f71dbeb3c2eca2de21a9f267e4d4c42a

[root@centos7-temp ~]# reboot

[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
6aea8688df49   nginx     "/docker-entrypoint.…"   39 seconds ago   Up 18 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx
#随机端口映射
[root@centos7-temp ~]# docker run -P nginx

[root@centos7-temp ~]# ss -ntl
State       Recv-Q Send-Q                 Local Address:Port                                Peer Address:Port              
LISTEN      0      128                                *:32769                                          *:*                  
LISTEN      0      128                                *:22                                             *:*                  
LISTEN      0      100                        127.0.0.1:25                                             *:*                  
LISTEN      0      128                             [::]:32769                                       [::]:*                  
LISTEN      0      128                             [::]:22                                          [::]:*                  
LISTEN      0      100                            [::1]:25                                          [::]:*

#此命令可以查看容器端口映射关系
[root@centos7-temp ~]# docker port 1bf6734c7b04 
80/tcp -> 0.0.0.0:32769
80/tcp -> :::32769

#测试访问
[root@centos7-temp ~]# curl 127.0.0.1:32769 -I
HTTP/1.1 200 OK
Server: nginx/1.21.5
Date: Sun, 08 Jan 2023 15:30:08 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 28 Dec 2021 15:28:38 GMT
Connection: keep-alive
ETag: "61cb2d26-267"
Accept-Ranges: bytes

#退出前台运行,容器也会停止
[root@centos7-temp ~]# docker run -P nginx
2023/01/08 15:33:42 [notice] 1#1: exit

[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

#容器启动后,默认处于预定义的NAT网络中,所以外部网络的主机无法直接访问容器中网络服务docker run -P 可以将事先容器预定义的所有端口映射宿主机的网卡的随机端口,默认从32768开始
使用随机端口时,当停止容器后再启动可能会导致端口发生变化 

#指定端口映射
[root@centos7-temp ~]# docker run -d -p 80:80 nginx
cd4108e6577e20e270510f3d087c0e6fa82ef918d2d342cc5bd5870f334c1519

[root@centos7-temp ~]# ss -ntl
State       Recv-Q Send-Q                 Local Address:Port                                Peer Address:Port              
LISTEN      0      128                                *:80                                             *:*                  
LISTEN      0      128                                *:22                                             *:*                  
LISTEN      0      100                        127.0.0.1:25                                             *:*                  
LISTEN      0      128                             [::]:80                                          [::]:*                  
LISTEN      0      128                             [::]:22                                          [::]:*                  
LISTEN      0      100                            [::1]:25                                          [::]:*

[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS          PORTS                               NAMES
cd4108e6577e   nginx     "/docker-entrypoint.…"   2 minutes ago       Up 2 minutes    0.0.0.0:80->80/tcp, :::80->80/tcp   pedantic_clarke

#docker run -p 可以将容器的预定义的指定端口映射到宿主机的相应端口
注意: 多个容器映射到宿主机的端口不能冲突,但容器内使用的端口可以相同 

2.列出容器

格式:docker ps [选项]

选项:

-a:列出所有,包括运行和停止的

-q:只显示容器ID

-s:查看容器大小

-l:查看最新创建的容器

-f:查看停止运行的容器

用法:

#查看运行的容器
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS     NAMES
6950646e97f8   nginx     "/docker-entrypoint.…"   27 minutes ago   Up 2 seconds   80/tcp    nginx

#查看所有容器
[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                     PORTS     NAMES
e506a29205b6   mysql     "docker-entrypoint.s…"   3 seconds ago    Exited (1) 2 seconds ago             kind_ardinghelli
6950646e97f8   nginx     "/docker-entrypoint.…"   28 minutes ago   Up 42 seconds              80/tcp    nginx

#只显示容器ID
[root@centos7-temp ~]# docker ps -q
6950646e97f8

#查看容器大小
[root@centos7-temp ~]# docker ps -s
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS     NAMES     SIZE
6950646e97f8   nginx     "/docker-entrypoint.…"   29 minutes ago   Up 2 minutes   80/tcp    nginx     1.09kB (virtual 141MB)

#查看最新创建的容器
[root@centos7-temp ~]# docker ps -l
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                     PORTS     NAMES
e506a29205b6   mysql     "docker-entrypoint.s…"   2 minutes ago   Exited (1) 2 minutes ago             kind_ardinghelli

#查看停止运行的容器
[root@centos7-temp ~]# docker ps -f 'status=exited'
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                     PORTS     NAMES
e506a29205b6   mysql     "docker-entrypoint.s…"   3 minutes ago   Exited (1) 3 minutes ago             kind_ardinghelli

3.查看容器内的进程

格式:docker top 容器名/容器ID

用法:

#容器名查看
[root@centos7-temp ~]# docker top nginx
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2053                2033                0                   23:42               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 2098                2053                0                   23:42               ?                   00:00:00            nginx: worker process
101                 2099                2053                0                   23:42               ?                   00:00:00            nginx: worker process

#容器ID查看
[root@centos7-temp ~]# docker top 6950646e97f8
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2053                2033                0                   23:42               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 2098                2053                0                   23:42               ?                   00:00:00            nginx: worker process
101                 2099                2053                0                   23:42               ?                   00:00:00            nginx: worker process

 4.查看容器内的资源使用情况

格式:docker stats 容器名/容器ID

#查看指定容器
[root@centos7-temp ~]# docker stats nginx
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O   PIDS
6950646e97f8   nginx     0.00%     1.957MiB / 1.934GiB   0.10%     740B / 0B   0B / 0B     3
[root@centos7-temp ~]# docker stats 6950646e97f8
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O   PIDS
6950646e97f8   nginx     0.00%     1.957MiB / 1.934GiB   0.10%     740B / 0B   0B / 0B     3

#查看所有容器
[root@centos7-temp ~]# docker stats 
CONTAINER ID   NAME            CPU %     MEM USAGE / LIMIT     MEM %     NET I/O         BLOCK I/O        PIDS
4b13c503d086   elasticsearch   0.30%     1.135GiB / 1.934GiB   58.72%    1.22kB / 984B   251MB / 1.62MB   44
6950646e97f8   nginx           0.00%     1.957MiB / 1.934GiB   0.10%     740B / 0B       0B / 0B          3

5.查看容器的详细信息

格式:docker inspect 容器名/容器ID

[root@centos7-temp ~]# docker inspect nginx

6.删除容器

格式:docker rm [选项] 容器名/容器ID

-f:强制删除正在运行的容器

-a:删除所有,包括运行和停止的

用法:

#正在运行的容器需要加上-f选项强制删除
[root@centos7-temp ~]# docker rm 6950646e97f8 
Error response from daemon: You cannot remove a running container 6950646e97f88ce338d4294c1bd6d088b5e5245709f49604a137ba5951879607. Stop the container before attempting removal or force remove
[root@centos7-temp ~]# docker rm -f 6950646e97f8 
6950646e97f8
#删除所有容器
[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS                      PORTS                                                                                  NAMES
4b13c503d086   elasticsearch:7.6.2   "/usr/local/bin/dock…"   20 minutes ago   Up 20 minutes               0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp   elasticsearch
e506a29205b6   mysql                 "docker-entrypoint.s…"   36 minutes ago   Exited (1) 36 minutes ago                                                                                          kind_ardinghelli

[root@centos7-temp ~]# docker rm -f `docker ps -a`

[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#删除所有正在运行的容器
[root@centos7-temp ~]#docker rm -f `docker ps -q -f status=running`

#删除所有停止运行的容器
[root@centos7-temp ~]#docker rm `docker ps -qf status=exited`
或
[root@centos7-temp ~]docker container prune -f

7.启动和停止容器

格式:

docker start 容器名/容器ID

docker stop 容器名/容器ID

docker restart 容器名/容器ID

docker pause 容器名/容器ID

docker unpause 容器名/容器ID

用法:

#启动容器
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@centos7-temp ~]# docker start 730a80fdf57f
730a80fdf57f
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED      STATUS         PORTS     NAMES
730a80fdf57f   nginx     "/docker-entrypoint.…"   3 days ago   Up 5 seconds   80/tcp    nginx1

#停止容器
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED      STATUS              PORTS     NAMES
730a80fdf57f   nginx     "/docker-entrypoint.…"   3 days ago   Up About a minute   80/tcp    nginx1
[root@centos7-temp ~]# docker stop 730a80fdf57f 
730a80fdf57f
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

#重启容器
[root@centos7-temp ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS                          PORTS     NAMES
54218a657176   nginx     "/docker-entrypoint.…"   About a minute ago   Exited (0) About a minute ago             nginx1
7542f9a9e4ca   busybox   "sh"                     3 days ago           Exited (0) 3 days ago                     hardcore_taussig
7912c335a5e1   mysql     "docker-entrypoint.s…"   3 days ago           Exited (1) 3 days ago                     musing_bose
[root@centos7-temp ~]# docker restart nginx1
nginx1
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
54218a657176   nginx     "/docker-entrypoint.…"   2 minutes ago   Up 5 seconds   80/tcp    nginx1

#暂停容器中的所有进程
[root@centos7-temp ~]# docker pause nginx1
nginx1

#恢复容器中的所有进程
[root@centos7-temp ~]# docker unpause nginx1
nginx1

8.给正在运行的容器发信号

格式:docker kill 容器名/容器ID

用法:

#默认发送关闭信号
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
54218a657176   nginx     "/docker-entrypoint.…"   15 minutes ago   Up 12 minutes   80/tcp    nginx1

[root@centos7-temp ~]# docker kill nginx1
nginx1

[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#强制关闭所有运行中的容器
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS     NAMES
246349e5a3a8   busybox   "sh"                     7 seconds ago    Up 5 seconds             busybox1
54218a657176   nginx     "/docker-entrypoint.…"   23 minutes ago   Up 3 minutes   80/tcp    nginx1

[root@centos7-temp ~]# docker kill `docker ps -a -q`
246349e5a3a8
54218a657176
Error response from daemon: Cannot kill container: eb90aedd7724: Container eb90aedd772498b8ca5090708f5e80a2fc6d51fc6ccbc46b6eb0438a520d4788 is not running
Error response from daemon: Cannot kill container: b02457163726: Container b02457163726d73a1951ca6fe8a806ab7d309327321157f90924895419096702 is not running
Error response from daemon: Cannot kill container: 7d9d15cdebcc: Container 7d9d15cdebccfbf257d132c62ad3011fae5fb13c12507fa0832cb9a36b10b242 is not running
Error response from daemon: Cannot kill container: 73e5f7ffbd24: Container 73e5f7ffbd246b19cf714c26a8977665e329658540dd0d6bbfe350d69ee06598 is not running
Error response from daemon: Cannot kill container: 7912c335a5e1: Container 7912c335a5e16a3d236d916eed47ea8b57738e228080d21e4a69b3c7ddf8b5cc is not running

[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

9.进入正在运行的容器

(1)

#操作会在同一个容器的多个会话界面同步显示,所有使用此方式进入容器的操作都是同步显示的,且使用exit退出后容器自动关闭,不推荐使用,需要进入到有shell环境的容器

格式:docker attach 容器名/容器ID

用法:

#进入正在运行的centos容器
[root@centos7-temp ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
14e6aa0417f5   centos    "/bin/bash"   5 seconds ago   Up 5 seconds             c1

[root@centos7-temp ~]# docker attach c1

#同时在第二个终端attach到容器,执行命令,两个终端同步显示结果
[root@14e6aa0417f5 /]# cat /etc/redhat-release 
CentOS Linux release 8.4.2105

#exit同时退出
[root@14e6aa0417f5 /]# exit
exit
[root@centos7-temp ~]# 
#退出后,容器自动停止
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
14e6aa0417f5   centos    "/bin/bash"   19 minutes ago   Up 2 minutes             c1

[root@centos7-temp ~]# docker attach c1

[root@14e6aa0417f5 /]# exit
exit
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

 (2)

#在运行中的容器启动新进程,可以执行单次命令,以及进入容器测试环境使用此方式,使用exit退出,但容器还在运行,此为推荐方式

格式:docker exec -it 容器名/容器ID sh/bash

选项:-it:在容器中生成一个交互式的终端

用法:

#执行一次性命令
[root@centos7-temp ~]# docker exec c1 cat /etc/redhat-release 
CentOS Linux release 8.4.2105

#进入容器内执行命令,exit退出,但容器不停止
[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS              PORTS     NAMES
14e6aa0417f5   centos    "/bin/bash"   19 minutes ago   Up About a minute             c1

[root@centos7-temp ~]# docker exec -it c1 bash

[root@14e6aa0417f5 /]# cat /etc/redhat-release 
CentOS Linux release 8.4.2105

[root@14e6aa0417f5 /]# exit
exit

[root@centos7-temp ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
14e6aa0417f5   centos    "/bin/bash"   19 minutes ago   Up 2 minutes             c1

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/190185.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Spring定时器超过30分钟问题

目前需要定时器做一个定时扫描任务的功能&#xff0c;原先都是定时在半个小时&#xff0c;程序跑起来也没事。但是最近公司要求定时时间加长到45分钟&#xff0c;而调整完配置完后发现&#xff0c;程序是在45分钟和整点进行的扫描。 下面是我做的示例时间缩短为45秒 spring。x…

守护进程编程流程及代码实现

概念不做阐述&#xff0c;本文主要内容为守护进程编程部分的知识说明 守护进程的编程流程&#xff1a; 1.fork退出父进程&#xff0c;保证留下的子进程是组员进程 2.利用setsid()创建新会话&#xff0c;把子进程挪到新的会话中 //获取会话是getsid() 3.fork退出父进程&#x…

完成基于Servlet的对user表的增删改查

基于Servlet的增删改查 1.开发环境 IDEAJDK1.8Tomcat8.5Mysql 8.0.12 2.数据库 2.1表创建 2.2表数据 3.JavaWeb代码 3.1目录结构 3.2util包下代码 JdbcUtil完成对数据库的连接和资源释放 JsonResult对返回前端资源的封装 JdbcUtil代码&#xff1a; /* 数据库连接板帮助类 …

Python中的垃圾回收机制

Python的垃圾回收主要以引用计数为主&#xff0c;分代回收为辅。引用计数在Python中&#xff0c;使用了引用计数这一技术实现内存管理。一个对象被创建完成后就有一个变量指向这个对象&#xff0c;那么就这个对象的引用计数为1&#xff0c;以后如果有其他变量指向这个对象&…

不吹牛,完爆ant design的定位组件,floating-ui来也

前言 因为要写react定位组件&#xff08;这不是标题党&#xff0c;就是完爆ant design的定位组件&#xff0c;你应该看到一半就会同意我的观点&#xff09;&#xff0c;如下图&#xff1a; 红框部分是用绝对定位放在按钮上面的&#xff0c;你们B端用的主流组件库都是这样实现的…

Python自动化小技巧14——自动批量发送邮件(带各种附件)

案例背景 我的博客下面评论都是各种要数据的......一个一个发其实很浪费时间的&#xff0c;每次输入评论者的邮箱&#xff0c;然后打开数据所在的文件夹&#xff0c;上传&#xff0c;填写标题正文&#xff0c;发送....... 一模一样的流程&#xff0c;所以这种重复性的劳动肯定…

Linux下 git 上传与删除 的基本指令

git的概述克隆仓库使用 git 上传文件删除 git 中的文件git的概述 Git 是一个免费并开源的分布式版本控制系统&#xff0c;可以快速高效地处理从小型到大型的各种项目。 在使用 git 应确保Linux系统中已安装有git 命令&#xff1a;git --version 作用&#xff1a;查看 git 是否…

Spring Boot 单元测试

文章目录1. 单元测试是什么2. 单元测试的优点3. 进行 Spring Boot 单元测试3.1 确认项目中已经内置了测试框架3.2 生成单元测试的类3.3 添加 SpringBootTest 注解3.4 添加单元测试的业务代码3.5 注解 Transactional4. 断言1. 单元测试是什么 单元测试&#xff0c;是指对软件中…

微信小程序022同学会学生会活动经费系统

同学会小程序采用B/S结构、java开发语言、以及Mysql数据库等技术。系统主要分为管理员端和用户端两部分&#xff0c;管理员管理主要功能包括&#xff1a;首页、个人中心、用户管理、共享账本管理、我的账本管理、经费信息管理、经费支出管理、活动信息、管理员管理、留言板管理…

分享怎么做公众号预约_美容院预约小程序开发制作功能介绍

小程序的功能首先是为美人有约客户提供更快选购、预约服务的线上工具&#xff0c;解决顾客对商品详情、线上购买、线上预约查看等各种服务需求。一、美容美发预约下单小程序主要功能有&#xff1a;首页&#xff1a;搜索、banner、金刚区、瓷片区、项目列表预约&#xff1a;单次…

Java技术栈,从入门到放弃,废了废了

Java技术路线应用框架后端Spring家族SpringIoCAOPSpring MVCSpring Boot自动配置、开箱即用整合Web整合数据库&#xff08;事务问题&#xff09;整合权限ShiroSpring Security整合中间件缓存MQRPC框架NIO框架服务器软件应用服务器TomcatJettyUndertowWeb服务器Nginx中间件缓存R…

Deathstalker的核心武器——Janicab新变种

01 概述 DeathStalker是一个专门针对金融机构和律师事务所进行攻击的组织&#xff0c;而Janicab是其所使用的比较古老的武器。 Janicab 首次在2013年被发现&#xff0c;它是能够运行在MacOS和Windows操作系统上的恶意软件。其中&#xff0c;Windows版本基于VBscript的植入作为…

【第27天】SQL进阶-查询优化- performance_schema系列实战三:锁问题排查(表级锁)(SQL 小虚竹)

回城传送–》《32天SQL筑基》 文章目录零、前言一、什么是表级锁二、什么时候适合加表级锁三、实战演练3.1 数据准备&#xff08;如果已有数据可跳过此操作&#xff09;3.2 开启第一个会话&#xff0c;执行显式加表级锁3.3 开启第二个会话&#xff0c;对该表执行update更新3.4 …

Vue 3.0 应用组件实例

#创建一个应用实例 每个 Vue 应用都是通过用 createApp 函数创建一个新的应用实例开始的&#xff1a; const app Vue.createApp({ /* 选项 */ }) 该应用实例是用来在应用中注册“全局”组件的。我们将在后面的指南中详细讨论&#xff0c;简单的例子&#xff1a; const app V…

jsp学生管理系统Myeclipse开发mysql数据库web结构java编程计算机网页项目

一、源码特点 jsp 学生管理系统 是一套完善的web设计系统&#xff0c;对理解JSP java编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。开发环境为TOMCAT7.0,Myeclipse8.5开 发&#xff0c;数据库为Mysql&#xff0c;使用ja…

【Docker】基础使用

目录 一、Docker简介 二、Docker核心概念 三、Docker安装 四、Docker常用操作 1. 镜像操作 2. 容器操作 3. 安装MySQL 一、Docker简介 Docker 是一个开源的应用容器引擎&#xff0c;基于Go 语言并遵从 Apache2.0 协议开源。 Docker 可以让开发者打包他们的应用以及依赖…

基于Springboot搭建java项目(二十二)——过滤器、监听器和拦截器的使用

过滤器、监听器和拦截器的使用 一、过滤器、监听器和拦截器总览 过滤器&#xff08;Filter&#xff09;监听器&#xff08;Listener&#xff09;拦截器&#xff08;Interceptor&#xff09;关注点web请求系统级别参数、对象Action&#xff08;部分web请求&#xff09;如何实现…

20230201在AIO-3568J开发板在原厂Android11下增加右键返回

20230201在AIO-3568J开发板在原厂Android11下增加右键返回 2023/2/1 8:37 百度搜索&#xff1a;RK3568 右键返回 Z:\rk3568_Android11.0_ap6257s\frameworks\native\services\inputflinger\reader\mapper\accumulator\CursorButtonAccumulator.cpp uint32_t CursorButtonAccumu…

【学习OpenCV4】如何学习OpenCV

OpenCV是应用非常广泛的开源视觉处理库&#xff0c;在图像处理、计算机视觉和自动驾驶中有着非常重要的作用。 废话不多说&#xff0c;我就来讲讲OpenCV的使用和学习需要怎么做吧&#xff0c;大家觉得有道理的可以参考一下。 我理解的学习和使用的重点在四个方面&#xff1a; …

PTA L1-019 谁先倒(详解)

前言&#xff1a;本期是关于谁先倒的详解&#xff0c;内容包括四大模块&#xff1a;题目&#xff0c;代码实现&#xff0c;大致思路&#xff0c;代码解读&#xff0c;今天你c了吗&#xff1f; 题目&#xff1a; 划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方…