Docker的常用命令
然后我们来学我们最重要的镜像命令:
docker images 查看所有本地的主机上的镜像
docker search搜索镜像:
docker pull下载镜像:
docker pull 镜像名[:tag]
指定版本下载:
docker rmi删除镜像:
批量删除:
以上命令是针对于镜像
容器命令:
说明:我们有了镜像才可以创建容器,Linux,下载一个centos镜像来测试学习
docker pull centos
新建容器并启动:
测试
启动并进入容器:
这样我们就在docker中去创建了一个linux虚拟机容器。
很多命令是不完善的。
从容器中退回主机:
列出所有运行中的容器:
我们还可以查看我们曾经使用过的容器:
docker ps:列出正在运行的容器
-a:列出正在运行的容器,带出历史运行过的容器
-n=? 显示最近创建的容器
-q:只显示容器的编号
退出容器
exit 容器停止并退出
Ctrl +P+Q 容器不停止退出
删除容器
不加-f不能删除正在运行的容器。
docker ps -a -q|xargs docker rm 删除所有的容器
启动和停止容器的操作
docker start 容器 id 启动容器
docker restart 容器 id 重启容器
docker stop 容器 id 停止当前正在运行的容器
docker kill 容器 id 强制停止当前容器
常用的其它命令
我们现在写完脚本,也能去查到我们的容器,现在我们就可以去查看我们的日志了。
显示日志:
查看全部日志:
查看容器中的进程信息 ps:
top命令
docker top 容器 id
查看镜像的元数据
命令:docker inspect d0ce277cd508
[
{
"Id": "d0ce277cd5086e1a36e41f5ea4bcce7ffaa668df715b58fe74976ad2b72daefb",
"Created": "2022-11-26T12:07:37.841424805Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo vision;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 7002,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-11-26T12:07:45.232335398Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/d0ce277cd5086e1a36e41f5ea4bcce7ffaa668df715b58fe74976ad2b72daefb/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/d0ce277cd5086e1a36e41f5ea4bcce7ffaa668df715b58fe74976ad2b72daefb/hostname",
"HostsPath": "/var/lib/docker/containers/d0ce277cd5086e1a36e41f5ea4bcce7ffaa668df715b58fe74976ad2b72daefb/hosts",
"LogPath": "/var/lib/docker/containers/d0ce277cd5086e1a36e41f5ea4bcce7ffaa668df715b58fe74976ad2b72daefb/d0ce277cd5086e1a36e41f5ea4bcce7ffaa668df715b58fe74976ad2b72daefb-json.log",
"Name": "/xenodochial_bassi",
"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/0176f1e7e69ddf21b7812deb61cf6eda53829f648456312d719705774f1bd200-init/diff:/var/lib/docker/overlay2/04a97b4de60b9be39ca13e668502267f55f9bd68773dd8b1035ac0aea95453dc/diff",
"MergedDir": "/var/lib/docker/overlay2/0176f1e7e69ddf21b7812deb61cf6eda53829f648456312d719705774f1bd200/merged",
"UpperDir": "/var/lib/docker/overlay2/0176f1e7e69ddf21b7812deb61cf6eda53829f648456312d719705774f1bd200/diff",
"WorkDir": "/var/lib/docker/overlay2/0176f1e7e69ddf21b7812deb61cf6eda53829f648456312d719705774f1bd200/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "d0ce277cd508",
"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 vision;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": "52cc407062562e0c03e36bccce93d20262d649ddc8ec8b357b13b66adec28800",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/52cc40706256",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "5e1be14513360d9de30404e19ba2b50d2ad05a48981e641dde3562022f09cce2",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "c23d42eac542ab88617c6d0cb754adcd61255d31eee49674d341b07ac3e8f08a",
"EndpointID": "5e1be14513360d9de30404e19ba2b50d2ad05a48981e641dde3562022f09cce2",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
测试:
方式二
docker attach 容器 id
从容器内拷贝文件到主机上:
docker cp 容器id:容器内路径 目的地主机路径
小结:
接下来就是我们的一些练习,使用docker安装nginx:
我们可以现在我们的docker中去搜索我们的nginx:
docker search nginx
我们可以搜到很多歌版本的nginx。
然后我们去下载nginx镜像:
docker pull nginx
然后我们去看看我们有没有下载好我们的镜像:
docker images:
然后我们去启动我们的nginx:
docker run -d --name nginx01 -p 3344:80 nginx
启动好了之后,我们去查看一下我们的容器:
然后我们来进行一下本机测试
curl localhost:3344
端口暴露的概念
我们现在去进入到nginx里面:
docker exec -it nginx01 /bin/bash
我们现在就进入到容器里面:
退出nginx:
思考问题:我们每次改动nginx配置文件,都需要进入容器内部,十分的麻烦,我要是可以在容器外部提供一个映射路径,达到在容器修改文件名,容器就可以自动修改。-v 数据卷。
然后我们再来安装一下tomcat:
这个用完即删还是不是太好,所以我们还是推荐用正常的方法去做:
先下载:
然后我们就去启动我们的tomcat:
然后我们就去测试我们的Tomcat:
发现问题,linux命令少了,没有webapps,阿里云的原因,默认是最小的镜像,剔除掉不必要的,保证最小可运行环境。
我们把webapps.dist的内容复制到webapps中。
思考问题:我们以后要部署项目,如果每次都要进入容器是不是十分麻烦?我要是可以在容器外部提供一个映射在webapps,我们就可以在外包放置项目,就自动同步到内部就好了!