熬夜爆肝,docker常用命令集合!
Posted 28线不知名云架构师
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了熬夜爆肝,docker常用命令集合!相关的知识,希望对你有一定的参考价值。
一、docker的基本操作命令
#查看正在运行容器列表
[root@docker docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#查看所有容器(包含正在运行和已停止的)
[root@docker docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#查看镜像列表
[root@docker docker]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker docker]# docker images(常用)
REPOSITORY TAG IMAGE ID CREATED SIZE
#查看docker版本
[root@docker docker]# docker -v
Docker version 20.10.8, build 3967b7d
[root@docker docker]# docker version #查看详尽信息
Client: Docker Engine - Community #客户端
Version: 20.10.8 #docker引擎版本
API version: 1.41 #API版本
Go version: go1.16.6 #Go语言版本
Git commit: 3967b7d
Built: Fri Jul 30 19:55:49 2021 #创建时间
OS/Arch: linux/amd64 #操作系统/版本
Context: default #连接方式(默认)
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.8
API version: 1.41 (minimum version 1.12)
Go version: go1.16.6
Git commit: 75249d8
Built: Fri Jul 30 19:54:13 2021
OS/Arch: linux/amd64
Experimental: false
containerd: #容器
Version: 1.4.9 #容器版本
GitCommit: e25210fe30a0a703442421b0f60afac609f950a3
runc: 运行时容器
Version: 1.0.1
GitCommit: v1.0.1-0-g4144b63
docker-init: #初始化
Version: 0.19.0
GitCommit: de40ad0
[root@docker docker]# docker info #查看docker详细信息(此命令可以查看到docker中容器运行个数以及镜像个数等等)
二、镜像操作
2.1、运行镜像
[root@docker docker]# docker run hello-world #运行holle-world镜像
ps:run有两层操作:①pull拉取镜像 ②开启镜像
[root@docker docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8412ff6e97a hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago distracted_chatelet
[root@docker docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 6 months ago 13.3kB
2.2、删除镜像
[root@docker docker]# docker rm `docker ps -aq` #批量删除容器,-a表示全部;-q表示只显示ID
a8412ff6e97a
[root@docker docker]# docker rmi d1165f221234 #删除镜像
Untagged: hello-world:latest
Untagged: hello-world@sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
Deleted: sha256:f22b99068db93900abe17f7f5e09ec775c2826ecfe9db961fea68293744144bd
2.3、搜索镜像
[root@docker docker]# docker search nginx #搜索Nginx镜像
[root@docker docker]# docker search centos:7 #搜索centos7镜像
2.4、下载镜像
[root@docker docker]# docker pull nginx
2.5、查看镜像详尽信息
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.231.128/test-nginx/nginx-lnmp latest dd34e67e3371 2 weeks ago 133MB
nginx latest dd34e67e3371 2 weeks ago 133MB
hello-world latest d1165f221234 6 months ago 13.3kB
goharbor/chartmuseum-photon v0.9.0-v1.9.3 c7ccd1b8e8df 22 months ago 131MB
goharbor/harbor-migrator v1.9.3 b0ff59a8111a 22 months ago 362MB
goharbor/redis-photon v1.9.3 33aaebc86b13 22 months ago 111MB
goharbor/clair-photon v2.1.0-v1.9.3 ccf50ab9d2c8 22 months ago 170MB
goharbor/notary-server-photon v0.6.1-v1.9.3 605e9ad80cf4 22 months ago 146MB
goharbor/notary-signer-photon v0.6.1-v1.9.3 4441a47e8f76 22 months ago 143MB
goharbor/harbor-registryctl v1.9.3 27af14c21462 22 months ago 103MB
goharbor/registry-photon v2.7.1-patch-2819-2553-v1.9.3 4c51bdb781e2 22 months ago 85.7MB
goharbor/nginx-photon v1.9.3 c6934119da35 22 months ago 44MB
goharbor/harbor-log v1.9.3 00a3acdb5d11 22 months ago 82.3MB
goharbor/harbor-jobservice v1.9.3 a3288107fff4 22 months ago 141MB
goharbor/harbor-core v1.9.3 9d394b9f6b49 22 months ago 155MB
goharbor/harbor-portal v1.9.3 6f5b0504c96b 22 months ago 51.4MB
goharbor/harbor-db v1.9.3 6004d1d5f272 22 months ago 148MB
goharbor/prepare v1.9.3 272365739d13 22 months ago 149MB
[root@192 ~]# docker inspect dd34e67e3371
[
{
"Id": "sha256:dd34e67e3371dc2d1328790c3157ee42dfcae74afffd86b297459ed87a98c0fb",
"RepoTags": [
"192.168.231.128/test-nginx/nginx-lnmp:latest",
"nginx:latest"
],
"RepoDigests": [
"192.168.231.128/test-nginx/nginx-lnmp@sha256:5e95e5eb8be4322e3b3652d737371705e56809ed8b307ad68ec59ddebaaf60e4",
"nginx@sha256:4d4d96ac750af48c6a551d757c1cbfc071692309b491b70b2b8976e102dd3fef"
],
"Parent": "",
"Comment": "",
"Created": "2021-08-17T11:46:11.370722453Z",
"Container": "c8f4640bdac21c018ff68bfb015ab66f87de0ed066b73eb6607a832a8abf3581",
"ContainerConfig": {
"Hostname": "c8f4640bdac2",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.1",
"NJS_VERSION=0.6.1",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\\"nginx\\" \\"-g\\" \\"daemon off;\\"]"
],
"Image": "sha256:6a804910f5d3b3726b00e8c4595374929fb86dd75f18175f2846bfb1fb66801b",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"DockerVersion": "20.10.7",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.1",
"NJS_VERSION=0.6.1",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "sha256:6a804910f5d3b3726b00e8c4595374929fb86dd75f18175f2846bfb1fb66801b",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"Architecture": "amd64",
"Os": "linux",
"Size": 133175493,
"VirtualSize": 133175493,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/10dce9ac9cb9802f00df21bb4f833ee4f70268cd16452450a5bfdf09977b52a5/diff:/var/lib/docker/overlay2/a3ac52568dc19fec42ced9a87a9cf86fd7ecc0dbe2657f731b151496c280bb0c/diff:/var/lib/docker/overlay2/a4986de653933fa7d160b715f19e927e10e1e88db16b54d8a720892a8e3ff067/diff:/var/lib/docker/overlay2/6682fde0f5f0b2133207b46b19d5eac4817ebd60d9528dcde50fab1471277f2c/diff:/var/lib/docker/overlay2/f265a11c2d270895b4c091e778b34ba8bfd775d8253dcda8bc4a4703b7f1676e/diff",
"MergedDir": "/var/lib/docker/overlay2/66dc0f0b817a733de2f786780632f58548dd1eb59fa9138acb3f599832b0dae5/merged",
"UpperDir": "/var/lib/docker/overlay2/66dc0f0b817a733de2f786780632f58548dd1eb59fa9138acb3f599832b0dae5/diff",
"WorkDir": "/var/lib/docker/overlay2/66dc0f0b817a733de2f786780632f58548dd1eb59fa9138acb3f599832b0dae5/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:f68ef921efae588b3dd5cc466a1ca9c94c24785f1fa9420bea15ecc2dedbe781",
"sha256:d1279c519351f33534aecf6d849e6b4b1b6b344079f4da49507f40de63d6917b",
"sha256:678bbd796838432e8db17f2128b7a06eae49adba2991ebf9ed8ebf3b8cbe2d4e",
"sha256:009f1d338b5732c48b5a643326d4724797ab786f8fcb3412403f8feab8509fd3",
"sha256:8f736d52032f7a4b7d828c0e119571cc3c047514c568f30ad52b6a503d050d85",
"sha256:fb04ab8effa8ecf93d0d9b3a13041f95d4566b6e52d3251c1b94097e34b53e89"
]
},
"Metadata": {
"LastTagTime": "2021-09-01T22:30:40.646859147+08:00"
}
}
]
2.6、创建镜像标签
[root@docker docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 822b7ec2aaf2 25 hours ago 133MB
[root@docker docker]# docker tag nginx:latest nginx:lnmp
[root@docker docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 822b7ec2aaf2 25 hours ago 133MB
nginx lnmp 822b7ec2aaf2 25 hours ago 133MB
2.7、删除镜像
[root@docker docker]# docker rmi nginx:lnmp
Untagged: nginx:lnmp
[root@docker docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 822b7ec2aaf2 25 hours ago 133MB
2.8、镜像导出
[root@docker docker]# docker save -o nginx_latest nginx:latest
2.9、镜像导入
[root@docker docker]# scp nginx_latest root@192.168.231.128:/opt
#另一台docker server
[root@localhost yum.repos.d]# cd /opt
[root@localhost opt]# ls
containerd nginx_latest rh
[root@localhost opt]# docker load < nginx_latest
三、容器相关操作
3.1、查询容器
docker ps -a ##显示所有的容器,包括未运行的
a:all;全部
docker ps -aq ##查询容器的id
q:代表过滤;只过滤容器ID
3.2、删除容器
docker rm -f `docker ps -aq` ##强制批量删除容器;不建议使用,若需要删除删选出id进行删除
3.3、启动运行容器
3.3.1、使容器开启并持续性运行
①:创建容器
docker create -it nginx:latest /bin/bash
-i:让容器的标准输入保持打开
-t:分配一个伪终端
-d:后台守护进程的方式运行
②:启动容器
docker start 容器id
3.3.2、启动一次性运行容器
持续性运行浪费资源,那么一次性执行如下操作
docker run centos:7 /usr/bin/bash -c ls /
-c:传递命令参数
3.3.3、持续后台运行
docker run -d centos:7 /usr/bin/bash -c "while true;do echo hello; done" ##后台运行centos:7的容器
while true;do echo hello; done:代表给予一个死循环
3.4、停止容器
docker stop 容器ID ##停止容器
docker stop b7ebbe9f86ed ##停止容器
状态码137:表示主动退出停止容器
3.5、进入/退出容器
3.5.1. 使用run
docker run -it nginx:latest /bin/bash
容器内没有的命令如何操作:
①:yum下载命令工具
②:以不同的环境运行容器,即环境中存在需要使用的命令即可
比如进入容器没有systemctl
命令解决:添加–privileged=true;指定此容器是否为特权容器,使用此参数,则不能用attach
示例:
docker run -itd --name test3 --privileged=true centos /sbin/init
/sbin/init内核启动时主动呼叫的第一个进程
docker ps -a ##查询容器是否开启
docker exec -it 容器ID /bin/bash #进入容器
docker exec -it 0ee4e0bf35e0 /bin/bash
验证:
yum -y install httpd ##随便安装一个服务
systemctl status httpd ##使用systemctl命令查询服务
docker inspect test3 ##查询test镜像详细信息
3.5.2、exec(容器必须为开启状态)
docker exec -it 容器ID /bin/bash
docker run -it:会创建前台进程,但是会在输入exit后终止进程。
docker attach:会通过连接stdin,连接到容器内输入输出流,会在输入exit后终止进程.
docker exec -it 会连接到容器,可以像SSH-样进入容器内部,进行操作,可以通过exit退出容器,不影响容器运行。
3.5.3、退出容器
ctrl+d 或者 exit
3.6、容器导入/导出
3.6.1、容器导出
docker export 容器ID >文件名
示例:
docker export b054125b9481 > hell02 ##导出hello-world到文件hello2内
3.6.2、容器导入(生成镜像)2种方式
①:docker import 导出的文件名(容器) 指定镜像名称(打上标签)
示例:
docker import hello2 hello-world:latest
②:cat 文件名(容器) | docker import - hello-world:latest
示例:
cat centos_02 | docker import - centos:7
3.7、删除/强制删除容器
docker rm 容器ID
docker rm -f 容器ID ##强制删除容器(正在运行的容器)
docker ps -a | awk '{print "docker rm " $1'} | bash ##批量删除容器;$1:是容器的id
for i in `docker ps -a | grep -i exit | awk '{print$1}'`; do docker rm -f $i;done ##批量删除"exit"状态的容器
docker rm -f `docker ps -aq` ##强制批量删除容器;不建议使用,若需要删除删选出id进行删除
以上是关于熬夜爆肝,docker常用命令集合!的主要内容,如果未能解决你的问题,请参考以下文章
❤️ 熬夜爆肝3万字!史上最硬核的Linux命令大全,还不收藏? ❤️通俗易懂,小白一看就会
❤️ 熬夜爆肝3万字!史上最硬核的Linux命令大全,还不收藏? ❤️通俗易懂,小白一看就会
熬夜爆肝!C++核心STL容器知识点汇总整理3W字干货预警 建议收藏