Docker 常用命令

1.Docker run 基本原理

2.Docker的底层原理

Docker 是如何工作的?

Docker 是一个Client -Server 架构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问。

Docker-Server 接收到 Docker-Client的指令后,就会执行这个指令!

底层原理

3.Docker 为什么 比 虚拟机 快

Docker 有着比虚拟机更少的 抽象层

Docker 利用的是宿主机的内核,而vm 是需要guest os

img

所以说,新建一个容器时,docker 不需要想虚拟机那样重新加载一个操作系统内核,避免引导。虚拟机是加载guest os,虚拟机启动是分钟级别的,而docker是直接利用宿主机操作系统,是秒级别的,省略了这个复杂的过程


4.Docker 命令

docker version # 显示Docker的版本
docker info # 显示docker的系统信息,包括镜像和容器的数量
docker --help # 帮助文档

4.1 镜像命令

4.1.1 docker images

查看所有本地主机上的镜像

[root@qiaoba ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 8 days ago 13.3kB

#解释
# REPOSITORY 镜像的仓库
# TAG 镜像的标签
# IMAGE ID 镜像的Id
# CREATED 镜像创建时间
# SIZE 镜像的大小

# 可选项
-a ,--all #列出所有的镜像
-q,--quiet #只显示镜像的Id

去 dockerhub上搜素镜像

[root@qiaoba /]# docker search mysql --filter=stars=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11496 [OK]
mariadb MariaDB Server is a high performing open sou… 4362 [OK]

4.1.3 docker pull

下载镜像

# 下载镜像 dockers pull 镜像名称:<tag> (如果不写tag ,默认就是latest)
[root@qiaoba /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
07aded7c29c6: Already exists # 分层下载
f68b8cbd22de: Already exists
30c1754a28c4: Already exists
1b7cb4d6fe05: Already exists
79a41dc56b9a: Already exists
00a75e3842fb: Already exists
b36a6919c217: Already exists
5e11fe494f45: Pull complete
9c7de1f889a7: Pull complete
cf6a13d05a76: Pull complete
fc5aa81f393a: Pull complete
Digest: sha256:360c7488c2b5d112804a74cd272d1070d264eef4812d9a9cc6b8ed68c3546189 #签名
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7 # docker的真实地址

4.1.4 docker rmi

删除镜像

docker rmi -f <容器Id> #删除单个镜像
docker rmi -f <镜像Id> <镜像Id> <镜像Id> # 删除多个镜像(用空格隔开)
docker rmi -f $(docker iamges -aq) # 删除所有镜像

4.2 容器命令

4.2.1 下载一个centos 镜像

docker pull centos

4.2.2 新建容器并启动

docker run [可选参数] image

# 参数说明
--name ="name" #容器名称,用来区分容器
-d #后台运行方式
-it #使用交互方式运行,进入容器查看内容
-p # 指定容器的端口 -p 8080:8080
-p ip :主机端口:容器端口
-P 主机端口:容器端口
-p 容器端口

# 测试 启动并进入容器
[root@qiaoba ~]# docker run -it centos /bin/bash
[root@8418f3caf1d4 /]#

#从容器中退回主机
[root@8418f3caf1d4 /]# exit
exit
[root@qiaoba ~]#

4.2.3 列出所有运行中的容器

#docker ps
#参数说明
-a #列出所有正在运行的容器,顺带列出历史运行过的容器
-n=? # 显示最近?个运行过的容器

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

# 列出曾经运行过的容器
#dockerps -a
[root@qiaoba ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8418f3caf1d4 centos "/bin/bash" 4 minutes ago Exited (0) 4 minutes ago musing_poincare
beefbd5d7c9e centos "/bin/bash" 31 minutes ago Exited (0) 22 minutes ago objective_robinson
7aaf98ea1cc1 feb5d9fea6a5 "/hello" 17 hours ago Exited (0) 17 hours ago
[root@qiaoba ~]#


4.2.4 退出容器

exit #退出并停止容器
Ctrl + P+Q #仅仅退出容器

[root@qiaoba /]# docker run -it centos /bin/bash
[root@71d02cb57b70 /]# [root@qiaoba /]#
[root@qiaoba /]#
[root@qiaoba /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
71d02cb57b70 centos "/bin/bash" 20 seconds ago Up 18 seconds zen_grothendieck

4.2.5 删除容器

docker rm 容器Id #删除指定容器
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -a -q |xargs docker mr #删除所有的容器

4.2.6 启动和停止容器

docker start <容器Id> #启动容器
docker restart <容器Id> #重启容器
docker stop <容器Id> #停止当前正在运行的容器
docker kill <容器Id> #强制停止当前容器

4.3 常用其他命令

4.3.1 后台启动命令

# docker run -d <镜像名>
[root@qiaoba /]# docker run -d centos
ad6997dd92a7214145f2bbc619e82ffa324055fa2f39ecb26cc9b2faa7bc2fb5
[root@qiaoba /]#

#问题 发现 docker ps 没有哪个centos进程
[root@qiaoba /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 常见的坑,容器使用后台运行 就必须要有一个前台进程,docker发现没有应用,就会自动停止
# 如 nginx 容器启动后,发现自己没有提供服务,就回立即停止进程,就没有程序了

4.3.2 查看日志

docker logs -f -t --detail <容器Id>

#参数说明
-tf #显示日志
--tail number #显示日志条数
#首先运行镜像
[root@qiaoba /]# docker run -d centos /bin/bash -c "while true;do echo fangtao;sleep 1;done"
9196b775af92ab76711c06ba01a7edb37c978c3179e4edd80b5a1233adad1237
[root@qiaoba /]#
#查看容器日志
[root@qiaoba /]# docker logs -tf --tail 5 9196b775af92ab7671
2021-10-03T02:23:49.871672439Z fangtao
2021-10-03T02:23:50.880792692Z fangtao
2021-10-03T02:23:51.892953069Z fangtao
2021-10-03T02:23:52.903638624Z fangtao
2021-10-03T02:23:53.912244596Z fangtao
2021-10-03T02:23:54.917903650Z fangtao
2021-10-03T02:23:55.926644943Z fangtao

4.3.3 查看容器中的进程信息

# 命令 docker top <容器Id>
[root@qiaoba /]# docker top 9196b775af92ab7671
UID PID PPID C STIME TTY TIME CMD
root 70992 70973 0 10:23 ? 00:00:00 /bin/bash -c while true;do echo fangtao;sleep 1;done
[root@qiaoba /]#

4.3.4 查看容器的元数据

# 命令 docker inspect <容器Id>
[root@qiaoba /]# docker inspect 9196b775af92ab7671
#如下输出:
[
{
"Id": "9196b775af92ab76711c06ba01a7edb37c978c3179e4edd80b5a1233adad1237",
"Created": "2021-10-03T02:23:39.063246554Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo fangtao;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 70992,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-10-03T02:23:39.777514963Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/9196b775af92ab76711c06ba01a7edb37c978c3179e4edd80b5a1233adad1237/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/9196b775af92ab76711c06ba01a7edb37c978c3179e4edd80b5a1233adad1237/hostname",
"HostsPath": "/var/lib/docker/containers/9196b775af92ab76711c06ba01a7edb37c978c3179e4edd80b5a1233adad1237/hosts",
"LogPath": "/var/lib/docker/containers/9196b775af92ab76711c06ba01a7edb37c978c3179e4edd80b5a1233adad1237/9196b775af92ab76711c06ba01a7edb37c978c3179e4edd80b5a1233adad1237-json.log",
"Name": "/goofy_jackson",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/01b73f3669effc94b765bb581a60e5d5d461ca6e76708c47eb5c5f2a628be02b-init/diff:/var/lib/docker/overlay2/86561cf93b73316988bbcc6aa9182cb6dd8b4818a556f4e733367b88f4c745f3/diff",
"MergedDir": "/var/lib/docker/overlay2/01b73f3669effc94b765bb581a60e5d5d461ca6e76708c47eb5c5f2a628be02b/merged",
"UpperDir": "/var/lib/docker/overlay2/01b73f3669effc94b765bb581a60e5d5d461ca6e76708c47eb5c5f2a628be02b/diff",
"WorkDir": "/var/lib/docker/overlay2/01b73f3669effc94b765bb581a60e5d5d461ca6e76708c47eb5c5f2a628be02b/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "9196b775af92",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash",
"-c",
"while true;do echo fangtao;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "e6fa0edbd22c10ad34ad97b0556249429b1acf868b7a6b1d8c4f320c042fdf99",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/e6fa0edbd22c",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "163e2cb4291cadfbec96eea645f860b2a25e7cd30dd108fa0bbe5af61ba44bbc",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "ba370f9a5b3cc21eac53a2d93184397506430301e8731b7ab2f63d018a8f4e18",
"EndpointID": "163e2cb4291cadfbec96eea645f860b2a25e7cd30dd108fa0bbe5af61ba44bbc",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]

4.3.4 进入当前正在运行的容器

# 我们通常都是使用后台方式运行的,需要进入容器,修改一些配置

#方式一 docker exec -it 容器Id bashShell

#测试
[root@qiaoba /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9196b775af92 centos "/bin/bash -c 'while…" 6 minutes ago Up 6 minutes goofy_jackson
[root@qiaoba /]# docker exec -it 9196b775af92 /bin/bash
[root@9196b775af92 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@9196b775af92 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 02:23 ? 00:00:00 /bin/bash -c while true;do echo fangtao;sleep 1;done
root 766 0 0 02:36 pts/0 00:00:00 /bin/bash
root 787 1 0 02:36 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 788 766 0 02:36 pts/0 00:00:00 ps -ef

#方式二 docker attach <容器Id>
#测试
[root@qiaoba ~]# docker attach 9196b775af92
#正在执行的代码

#区别
#docker exec #进入容器后开启一个新的终端,可以在里面操作
#docker attach #进入容器正在执行的终端,不会开启新的终端

4.3.5 从容器拷贝文件到主机

#命令 docker cp <容器Id>:文件目录 本地文件目录

#启动容器
root@qiaoba ~]# docker run -it centos /bin/bash
[root@3b054cdc2238 /]# docker ps
bash: docker: command not found
[root@3b054cdc2238 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@3b054cdc2238 /]# cd home
[root@3b054cdc2238 home]# ls
[root@3b054cdc2238 home]# touch fangtao.java
[root@3b054cdc2238 home]# vi fangtao.java
[root@3b054cdc2238 home]# ls
fangtao.java
[root@3b054cdc2238 home]# exit
exit
[root@qiaoba ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@qiaoba ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3b054cdc2238 centos "/bin/bash" 2 minutes ago Exited (0) 5 seconds ago fervent_swirles
#拷贝容器中指定的文件到本地目录
[root@qiaoba ~]# docker cp 3b054cdc2238:/home/fangtao.java /home
#去本地目录查看拷贝的文件
[root@qiaoba ~]# cd /home
[root@qiaoba home]# cat fangtao.java
public class fangtao{
public static void main(String[] args){
System.Out.println("hello world");
}
}
[root@qiaoba home]#
#拷贝是一个手动过程 ,后面可以使用 -v 卷技术,可以实现 容器目录 到本地目录的数据同步

5.总结

image-20211003110124567

5.1 命令小结

attach #当前shell下attach连接指定运行镜像
build #通过Dockerfile定制镜像
commit #提交当前容器为新的镜像
cp #从容器中拷贝指定文件或者目录到宿主机中
create #创建一个新的容器,同run 但不启动容器
diff #查看docker容器变化
events #从docker服务获取容器实时事件
exec #在已存在的容器上运行命令
export #导出容器的内容流作为一个tar归档文件(对应import)
history #展示一个镜像形成历史
images #列出系统当前镜像
import #从tar包中的内容创建一个新的文件系统映像(对应export)
info #显示系统相关信息
inspect #查看容器详细信息
kill #kill指定docker容器
load #从一个tar包中加载一个镜像(对应save)
login #注册或者登陆一个docker源服务器
logout #从当前Docker registry退出
logs #输出当前容器日志信息
pause #暂停容器
port #查看映射端口对应的容器内部源端口

ps #列出容器列表
pull #从docker镜像源服务器拉取指定镜像或者库镜像
push #推送指定镜像或者库镜像至docker源服务器
rename #重命名容器
restart #重启运行的容器
rm #移除一个或者多个容器
rmi #移除一个或多个镜像(无容器使用该镜像才可以删除,需要删除相关容器才可以继续或者-f强制删除)
run #创建一个新的容器并运行一个命令
save #保存一个镜像为一个tar包(对应load)
search #在docker hub中搜索镜像
start #启动容器
stats #统计容器使用资源
stop #停止容器
tag #给源中镜像打标签
top #查看容器中运行的进程信息
unpause #取消暂停容器
version #查看容器版本号
wait #截取容器停止时的退出状态值
posted @ 2021-10-03 18:37  旧城已空旧梦已逝  阅读(130)  评论(0编辑  收藏  举报