说明:有了镜像可以创建容器,来个linux学习下,下载各centos镜像测试学习
docker pull centos
新建容器并启动
通过命令
docker run [可选参数] image --help
#参数说明
--name=“Name” 容器名字
-d 后台方式运行
-i/t 使用交互方式运行,进入容器查看内容
-p 指定容器端口
-p 随机指定容器端口
##启动并进入容器(主机名是镜像名)
docker run -it centos /bin/bash
##退出
exit
发现run命令后面跟的参数很多
列出所有运行的容器
[root@localhost ~]# docker ps 命令
空 列出正在运行的容器
-a 列出正在运行的容器,顺并带出历史运行的容器
-n=? 显示最近创建的容器
-q 只显示ID
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3f0cf61bc93 centos "/bin/bash" 3 minutes ago Exited (0) 10 seconds ago kind_robinson
18d3a02eb7e0 hello-world "/hello" 22 minutes ago Exited (0) 22 minutes ago kind_heisenberg
113def5429e1 ubuntu:15.10 "/bin/sh -c 'while t…" 8 days ago Exited (137) 8 days ago sharp_perlman
2991750e007e ubuntu:15.10 "/bin/bash" 8 days ago Exited (0) 8 days ago beautiful_noether
5b28bbef5bb4 ubuntu:15.10 "/bin/echo 'Hello wo…" 8 days ago Exited (0) 8 days ago exciting_neumann
e5a275ae91bf ubuntu:15.10 "/bin/echo 'Hello wo…" 8 days ago Exited (0) 8 days ago bold_hopper
16b2f38e0044 hello-world "/hello" 8 days ago Exited (0) 8 days ago serene_burnell
##分析结果
退出容器
exit 直接容器停止退出
Ctrl+P+Q 退出后容器不停止
删除容器
docker rm 容器id ##删除指定的容器不能删除正在运行的容器
docker rm -f $(docker ps -aq)##删除是所有的容器,强制删除
docker ps -a -q|xargs docker rm ##删除是所有的容器
启动和停止容器的操作
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止运行的容器
##命令docker run d 镜像名
##如下:运行过发现centos停止了
##坑!!docker后台要是运行的话,必须要有一个前台进程
##容器发现没有应用自动停止
[root@localhost ~]# docker run -d centos
d6c3edf840454c287aacc9e8a1831088569cff59ad46a898da8836a484bf5123
You have new mail in /var/spool/mail/root
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d6c3edf84045 centos "/bin/bash" 11 seconds ago Exited (0) 10 seconds ago reverent_sanderson
e077af4dfff3 centos "/bin/bash" 6 minutes ago Exited (0) 3 minutes ago hopeful_raman
docker logs -f -t --tail 如果没有运行过,容器没有日志
##骚操作 通过执行shell语句增加日志量(代码有个bug,以attach进入会卡循环)
docker run -d centos /bin/bash -c 'while true;do echo helloword;slepp 1;done'
docker logs -f -t --tail 10 容器id
docker logs -f -t 容器id
-f -t==-tf
###查看容器中的进程信息
##top命令
[root@localhost ~]# docker top 4222dc0984ab
UID PID PPID C STIME TTY
root 34115 34096 33 23:37 ?
[root@localhost ~]# docker inspect 4222dc0984ab
[
{
"Id": "4222dc0984ab2a7a244f0d7a94e8892c8f8fcced0bd50e1ae440f0d7ae6b80e8",
"Created": "2021-04-24T15:26:21.077803499Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo helloword;slepp 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 34115,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-04-24T15:37:08.873092118Z",
"FinishedAt": "2021-04-24T15:31:40.862122967Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/4222dc0984ab2a7a244f0d7a94e8892c8f8fcced0bd50e1ae440f0d7ae6b80e8/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/4222dc0984ab2a7a244f0d7a94e8892c8f8fcced0bd50e1ae440f0d7ae6b80e8/hostname",
"HostsPath": "/var/lib/docker/containers/4222dc0984ab2a7a244f0d7a94e8892c8f8fcced0bd50e1ae440f0d7ae6b80e8/hosts",
"LogPath": "/var/lib/docker/containers/4222dc0984ab2a7a244f0d7a94e8892c8f8fcced0bd50e1ae440f0d7ae6b80e8/4222dc0984ab2a7a244f0d7a94e8892c8f8fcced0bd50e1ae440f0d7ae6b80e8-json.log",
"Name": "/condescending_williams",
"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/81a4f9d640b2763740c0b740a2da4c6648e2e9384563cac7a59086da65d8cfd0-init/diff:/var/lib/docker/overlay2/9ee0c87c4e2bbbeabfd993ef997837c468f1f3ec91ea901f04e673ff6e56b9ec/diff",
"MergedDir": "/var/lib/docker/overlay2/81a4f9d640b2763740c0b740a2da4c6648e2e9384563cac7a59086da65d8cfd0/merged",
"UpperDir": "/var/lib/docker/overlay2/81a4f9d640b2763740c0b740a2da4c6648e2e9384563cac7a59086da65d8cfd0/diff",
"WorkDir": "/var/lib/docker/overlay2/81a4f9d640b2763740c0b740a2da4c6648e2e9384563cac7a59086da65d8cfd0/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "4222dc0984ab",
"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 helloword;slepp 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"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": "f4e66f8a4ae67e522c5ab9dbf46145dd88490d8012be22eb41ef2082355bd9bf",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/f4e66f8a4ae6",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "5109799a04169189b9286b4ea9243adb53736052f5d756603c2161db8d74ce90",
"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": "f8d1527155be283f023f3ac2b96606b0e093d3bf79889a440aa5f640a604ed7e",
"EndpointID": "5109799a04169189b9286b4ea9243adb53736052f5d756603c2161db8d74ce90",
"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
}
}
}
}
]
一个非常重要的命令
docker里面非常详细的信息
进入当前运行的容器
docker exec -it 容器id /bin/bash
##测试
[root@localhost ~]# docker exec -it 4222dc0984ab /bin/bash
[root@4222dc0984ab /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 34 15:37 ? 00:05:50 /bin/bash -c while true;do echo helloword;slepp 1;done
root 62046 0 0 15:53 pts/0 00:00:00 /bin/bash
root 92636 62046 0 15:54 pts/0 00:00:00 ps -ef
##方式二
docker attach 容器id
docker exec ##进入容器会打开新的终端
docker attach ##进入程序正在执行的终端,不会启用新的进程
docker cp 容器id:路径 宿主机路径
##用例:
[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost home]# docker run -it centos /bin/bash
[root@c897ccc566cf /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@c897ccc566cf /]# cd home/
[root@c897ccc566cf home]# ls
##容器内创建一个文件
[root@c897ccc566cf home]# touch long.md
[root@c897ccc566cf home]# ls
long.md
[root@c897ccc566cf home]# exit
exit
##执行复制拷贝命令
[root@localhost home]# docker cp 6eb641aa12d7:/home/long.md /home
Error: No such container:path: 6eb641aa12d7:/home/long.md
[root@localhost home]# docker cp c897ccc566cf:/home/long.md /home
[root@localhost home]# ls
hello.md helloword.md long.md
##拷贝是一个手动的过程,以后用—v 数据卷的技术实现自动同步
docker stats 查看主机实时的内存消耗