docker version #显示Docker的版本信息
docker info #显示Docker的系统信息,包括镜像和容器的实况
docker 命令 --help #帮助命令
1、查看所有的本地主机上的镜像
docker images
#解释
RESPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像的大小
#可选项
Options:
-a, --all #列出所有镜像
-q, --quiet #只显示镜像的id
2、搜索镜像
docker search
#可选项,通过搜索来过滤
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
#搜索STARTS在3000以上的
#搜索STARTS在5000以上的
3、下载镜像
docker pull 镜像名[:tag]
注:
(1)如果不加tag,默认就是latest
(2)如果指定了tag,需要docker支持的内容里有
拉取mysql的镜像:
Digest:签名
docker.io/library/mysql:latest:真实地址
下载镜像tag等同于下载镜像的真实地址,即:
docker pull mysql
等价于
docker pull docker.io/library/mysql:latest
注:
(1)docker使用的是分层下载,对于不同tag的相同文件会复用,而不是再次下载
(2) 分层下载是docker image 的核心 ,联合文件系统
(3)最新的mysql和5.7版本的mysql公用的部分就无需再次下载,如下图所示
1、新建容器并启动
docker run [可选参数] image
#参数说明
--name = "Name" 容器名字 tomcat01 tomcat02 ,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-P 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-P 容器端口
-p 随机指定端口
#测试,启动并进入容器2、新建容器并启动
docker run -it centos /bin/bash
#查看容器内的centos,基础版本,很多命令都是不完善的
#从容器中退回主机
#查看主机centos的根目录
2、列出所有运行中的容器
docker ps 命令
#列出当前正在运行的容器
-a #列出当前正在运行的容器+带出历史允许过的容器
-n=? #显示最近正在创建的容器
-q #只显示容器的编号
3、退出容器
exit #直接容器停止并退出
CTRL+P+Q #容器不停止退出
执行CTRL+P+Q退出后再执行docker ps查看正在运行的容器,发现容器并没有停止
4、删除容器
docker rm 容器id #删除指定的容器,不能删除运行的容器,如果要强制删除,添加-f参数
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -a -q|xargs docker rm #删除所有的容器
运行的容器不能直接删除,如果要强制删除,需要添加-f参数
删除所有的容器,包括正在运行的,如下图所示:
5、启动和停止容器
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
1、后台启动容器
docker run -d 镜像名
注:
docker容器要后台运行,就必须要有一个前台进程,docker发现没有应用了,就会自动停止
如下图所示,刚启动的应用会自动停止
2、查看日志
docker logs 容器ID
显示日志
-tf
--tail num 要显示的日志条数
对于没有日志的docker容器,可以通过运行shell脚本制造日志
docker run -d centos /bin/sh -c "while true;do echo andy;sleep 1; done";
这时候再查看日志
3、查看容器中进程信息ps
docker top 容器ID
4、查看镜像的元数据
docker inspect 容器ID
[root@localhost ~]# docker inspect cf16f0c758f5
[
{
"Id": "cf16f0c758f53312d79ef429eaa9f8cde930ad94df46883bfb6ab71b4bd7bef5",
"Created": "2020-07-02T02:46:54.879268871Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo andy;sleep 1; done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 5502,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-07-02T02:46:55.203742345Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:831691599b88ad6cc2a4abbd0e89661a121aff14cfa289ad840fd3946f274f1f",
"ResolvConfPath": "/var/lib/docker/containers/cf16f0c758f53312d79ef429eaa9f8cde930ad94df46883bfb6ab71b4bd7bef5/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/cf16f0c758f53312d79ef429eaa9f8cde930ad94df46883bfb6ab71b4bd7bef5/hostname",
"HostsPath": "/var/lib/docker/containers/cf16f0c758f53312d79ef429eaa9f8cde930ad94df46883bfb6ab71b4bd7bef5/hosts",
"LogPath": "/var/lib/docker/containers/cf16f0c758f53312d79ef429eaa9f8cde930ad94df46883bfb6ab71b4bd7bef5/cf16f0c758f53312d79ef429eaa9f8cde930ad94df46883bfb6ab71b4bd7bef5-json.log",
"Name": "/nervous_swartz",
"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/e44f693a244af37ba7f73ef37a32e987bd7e41b4156a37590211529e6f319139-init/diff:/var/lib/docker/overlay2/b065e69bf4db0ca38b42834117d35dfcb7d9beed51901c906b64a9022f77fc33/diff",
"MergedDir": "/var/lib/docker/overlay2/e44f693a244af37ba7f73ef37a32e987bd7e41b4156a37590211529e6f319139/merged",
"UpperDir": "/var/lib/docker/overlay2/e44f693a244af37ba7f73ef37a32e987bd7e41b4156a37590211529e6f319139/diff",
"WorkDir": "/var/lib/docker/overlay2/e44f693a244af37ba7f73ef37a32e987bd7e41b4156a37590211529e6f319139/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "cf16f0c758f5",
"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 andy;sleep 1; done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200611",
"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": "2bdb190c46b80ff3cc3c40e174d041be8353d8ed154568941ee3fb29f56d6f06",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/2bdb190c46b8",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "56efc1d893d422b7871f078cd9d42e7f9cc526788eb8b039015a8b8becca18a2",
"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": "90b3fc0f677485cd7db29866d27bc2b11bc9e7e0f75cf29678547366fbe39d64",
"EndpointID": "56efc1d893d422b7871f078cd9d42e7f9cc526788eb8b039015a8b8becca18a2",
"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
}
}
}
}
]
5、进入当前正在运行的容器
我们通常容器都是使用后台方式运行的,有时需要进入容器,修改一些配置
命令:
(1) docker exec -it 容器id /bashshell (常用)
(2)docker attach 容器ID
两种命令的区别:
#docker exec #进入容器后打开一个新的终端,可以在里面操作(常用)
#docker attach #进入容器正在执行的终端,不会启动新的进程
6、进入当前正在运行的容器
docker cp 容器id:容器内路径 目的主机路径
(1)主机上新建文件
(2)主机上新建文件
(3)进入容器内新建文件
(4)将容器内文件拷贝至主机上
(5)查看拷贝是否生效