前言
Hai 我们又见面了,上篇文章给大家讲了是什么Docker,以及Docker的安装部署,本篇文章之主要是给大家下常用的命令(重点建议收藏)。
看完本文章可以掌握以下内容:
- 会使用帮助文档(重点)
- 掌握常用的镜像命令(重点)
- 掌握常用的容器命令(重点)
一、Docker常用命令
1.1 帮助命令
docker version #显示docker版本信息
docker info #显示docker的系统信息,包括镜像和容器数量
docker 命令 --help #帮助文档
1.2 镜像命令
1.2.1 docker imager 查看所有本地的主机上的镜像
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 10 months ago 13.3kB
#解释
REPOSITORY 镜像的仓库源
TAG 镜像标签
IMAGE ID 镜像的Id
CREATED 镜像的创建时间
SIZE 镜像的大小
#可选项
[root@192 ~]# docker images --help
-a, --all # 列出所有镜像
-q, --quiet # 只显示镜像Id
1.2.2 docker search 搜镜镜像
[root@192 ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10174 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3747 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 743 [OK]
percona Percona Server is a fork of the MySQL relati… 511 [OK]
#可选项
--filter STARS=3000 #会去搜索大于3000的即镜像
[root@192 ~]# docker search mysql --filter STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10174 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3747 [OK]
1.2.3 docker pull 下载镜像
#默认下载最新版
[root@192 ~]# docker pull mysql
Using default tag: latest #如果不写tage,默认就是latest
latest: Pulling from library/mysql
bb79b6b2107f: Pull complete #分层下载 docker iamge的核心 联合文件系统
49e22f6fb9f7: Pull complete
842b1255668c: Pull complete
9f48d1f43000: Pull complete
c693f0615bce: Pull complete
8a621b9dbed2: Pull complete
0807d32aef13: Pull complete
a56aca0feb17: Pull complete
de9d45fd0f07: Pull complete
1d68a49161cc: Pull complete
d16d318b774e: Pull complete
49e112c55976: Pull complete
Digest: sha256:8c17271df53ee3b843d6e16d46cff13f22c9c04d6982eb15a9a47bd5c9ac7e2d # 签名 唯一表示
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
#等价
docker pull mysql
docker pull docker.io/library/mysql:latest
#指定版本下载
[root@192 ~]# docker pull mysql:5.7
1.2.4 docker rmi 删除镜像
[root@192 ~]# docker rmi -f 镜像Id 删除指定镜像
[root@192 ~]# docker rmi -f 镜像Id 镜像Id 镜像Id 删除多个镜像
[root@192 ~]# docker rmi -f $(docker images -qa) 删除所有镜像
1.3 容器命令
说明: 我们有了镜像才可以创建容器,Linux下载一个centos
docker pull centos
新建容器命令
docker run [可选参数] image
# 参数说明
--name 容器名 tomcat1/java/ 用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
-p 随机指定端口
#测试案例 启动容器
[root@192 ~]# docker run -it centos /bin/bash
[root@c1afa7ccf7d6 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#退回容器到主机
[root@c1afa7ccf7d6 /]# exit
exit
1.3.1 查看Docker当前运行的容器
#docker -ps 命令
#参数介绍
# 列出当前运行的容器
-a # 列出当前在运行的容器+历史容器
-n=n # 显示容器的n个值
-q # 只显示容器编号
[root@192 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c1afa7ccf7d6 centos "/bin/bash" 2 minutes ago Exited (0) About a minute ago lucid_mclean
536b2397c88f bf756fb1ae65 "/hello" 4 days ago Exited (0) 4 days ago competent_lamport
1.3.2 退出容器
exit # 直接退出容器不后台运行
Ctrl + P + Q #退出如容器 后台运行
1.3.3 删除容器
docker rm 容器Id #删除指定的容器,不能删除在运行过程也中的容器,如果要强制删除加-f
docker rm -f $( docker ps -aq) #删除所有容器器
docker ps -a -q | xargs docker rm #删除所有容器器
1.3.4 启动和停止容器命令
docker start 容器 id #启动容器
docker restart 容器 id # 重启容器
docker stop 容器 id #停止当前正在运行的容器
docker kill 容器 id #强制停止当前容器
1.3.5 其他命令
#命令 docker run -d 镜像名!
root@192 ~]# docker run -d centos
66cee4f4493944e503edbcd4477bed9b778d7a41cb5698f8334a7a23118f1c9c
# 问题 docker ps 发现 cnetos停止了
#常用的坑:
#docker 容器使用后台运行,就必须要有一个前台运行进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就会立刻停掉,就没有程序了
1.3.6 查看日志
docerk logs -f -t --tail 容器 没有日志
#自己编写一个shell脚本
[root@192 ~]# docker run -d centos /bin/sh -c "while true;do echo hello 大数据老哥; sleep 1 ;done"
88c647580c3226618e458e878be90b82f142e09876d3b08c28ce43a01a4b1a64
[root@192 ~]# docker ps
CONTAINER ID
88c647580c32
#显示日志
-tf #显示日志
--tail number #显示日志条数
[root@192 ~]# docker logs -tf --tail 10 88c647580c32
2020-11-21T06:54:44.902712929Z hello 大数据老哥
2020-11-21T06:54:45.904301943Z hello 大数据老哥
2020-11-21T06:54:46.906363448Z hello 大数据老哥
2020-11-21T06:54:47.908508313Z hello 大数据老哥
1.3.7 查看容器进程信息
docker top 容器ID
[root@192 ~]# docker top 88c647580c
UID PID PPID C STIME
root 2704 2688 0 14:52
root 3072 2704 0 14:57
1.3.8 查看镜像的元数据新信息
#命令
docker inspect 容器Id
#测试
[root@192 ~]# docker inspect 88c647580c32
[
{
"Id": "88c647580c3226618e458e878be90b82f142e09876d3b08c28ce43a01a4b1a64",
"Created": "2020-11-21T06:52:21.329138725Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo hello 大数据老哥; sleep 1 ;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2704,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-11-21T06:52:21.566992956Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
"ResolvConfPath": "/var/lib/docker/containers/88c647580c3226618e458e878be90b82f142e09876d3b08c28ce43a01a4b1a64/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/88c647580c3226618e458e878be90b82f142e09876d3b08c28ce43a01a4b1a64/hostname",
"HostsPath": "/var/lib/docker/containers/88c647580c3226618e458e878be90b82f142e09876d3b08c28ce43a01a4b1a64/hosts",
"LogPath": "/var/lib/docker/containers/88c647580c3226618e458e878be90b82f142e09876d3b08c28ce43a01a4b1a64/88c647580c3226618e458e878be90b82f142e09876d3b08c28ce43a01a4b1a64-json.log",
"Name": "/cool_nightingale",
"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,
"Capabilities": null,
"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/4b7ce116b43405a39e12c1a816b1d7c034410ed8a50491b50ec994bffdd61802-init/diff:/var/lib/docker/overlay2/d08d9c2126b9989feb820ace4e0779542fe32f6f5423787aa50f571b459161a2/diff",
"MergedDir": "/var/lib/docker/overlay2/4b7ce116b43405a39e12c1a816b1d7c034410ed8a50491b50ec994bffdd61802/merged",
"UpperDir": "/var/lib/docker/overlay2/4b7ce116b43405a39e12c1a816b1d7c034410ed8a50491b50ec994bffdd61802/diff",
"WorkDir": "/var/lib/docker/overlay2/4b7ce116b43405a39e12c1a816b1d7c034410ed8a50491b50ec994bffdd61802/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "88c647580c32",
"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/sh",
"-c",
"while true;do echo hello 大数据老哥; sleep 1 ;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200809",
"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": "f5887061ecca23559bfc1c860c5b5133d4fae2f51a1be487ec8b3850fddcc32d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/f5887061ecca",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "123de981bb9d0e01c73ebd538b584b63f216b9cfec368be568c45252ceef16ba",
"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": "4c16d7d1e5efc8197db13862f7416453510904d989db0488798f2b4a709cefb0",
"EndpointID": "123de981bb9d0e01c73ebd538b584b63f216b9cfec368be568c45252ceef16ba",
"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
}
}
}
}
]
1.3.9 进入当前正在云行的容器
# 我们通常容器都是后台运行的,需要进入容器进行修改一些配置
#命令
#方式一
[root@192 ~]# docker ps
CONTAINER ID
88c647580c32
[root@192 ~]# docker exec -it 88c647580c /bin/bashh
[root@88c647580c32 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@88c647580c32 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 06:52 ? 00:00:00 /bin/sh -c while true;do echo hello ???????????????; sleep 1 ;done
root 1121 0 0 07:10 pts/0 00:00:00 /bin/bash
root 1138 1 0 07:10 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 1139 1121 0 07:10 pts/0 00:00:00 ps -ef
[root@88c647580c32 /]# exit
exit
#方式二
[root@88c647580c32 /]# docker attach 88c647580c
正在运行的代码
# attach 与 exec 区别
docker exec #进容器后开启一个新的后端,可以直接在面进行操作
docker attach #进入容器正在执行终端,不会启动新的进程
docker 与本地系统文件进行拷贝
# 进入容器
[root@192 ~]# docker exec -it 88c647580c /bin/bash
[root@88c647580c32 /]# cd home/
#创建文件
[root@88c647580c32 home]# touch test.txt
[root@88c647580c32 home]# exit
exit
#拷贝文件
[root@192 ~]# docker cp 88c647580c:/home/test.txt /home/
[root@192 ~]# ls /home/
test.txt
[root@192 ~]#
二、总结
上述给大家讲解了一些docker常用的命令,还不快多多的练起来,熟能生巧。无论是做高级工程师还是大数据运维 这个一个必会的技能。后面会给大家分享一下关于docker的实战操作,如:安装 nginx、Tomcat、commit镜像的原理、MySQL数据同步、DockerFile 等。
三、 粉丝福利
以下内容是博主花费了1个星期的时间总结各大厂商的面试题与拜访大佬总结的内容那个 关注公证号【大数据老哥】回复:Docker基础面试题即可获取。
关注老哥不迷路公证号搜索【大数据老哥】可以获取可以获取 大数据面经、Java面经、超级棒的开发软件、各种书籍推荐…等(各种资源等着你呦)。