说明:有了镜像才可以创建容器,linux,下载一个CentOS 镜像来测试学习!
docker pull centos
新建容器并启动
docker run[可选参数] image
#参数说明:
--name=“NAME” 容器名字 1 2 3 ,用来区分
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口, -p 8080:8080
-p主机端口:容器端口
-p 容器端口
-p ip:主机端口:容器端口
-P(大) 随机指定端口
**启动并进入容器**
[root@localhost /]# docker run -it centos /bin/bash
[root@c7fe83c7bbb9 /]#ls #查看容器内的centos,基础版本,很多命令都是不完善的!
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@c7fe83c7bbb9 /]# exit #退出容器,从容器返回主机
列出所有运行的容器
docker ps 命令
#当前运行的容器
-a #列出当前正在运行的容器+历史运行过的容器
-n=? #显示最近创建的容器
-q #只显示容器的编号
[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
c7fe83c7bbb9 centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago vigorous_archimedes
3c62d0f28ac1 bf756fb1ae65 "/hello" 3 days ago Exited (0) 3 days ago nostalgic_ellis
[root@localhost /]# docker ps -aq
c7fe83c7bbb9
3c62d0f28ac1
退出容器
exit #退出容器
Ctrl+P+Q #退出不停止容器
删除容器
docker rm 容器ID #删除指定的容器,不能删除正在运行的容器,强制删除加参数 -f
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -aq |xargs docker rm #删除所有的容器
启动和停止容器的操作
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前运行的容器
docker kill 容器id #强制停止运行的容器
常用其他命令
后台启动容器
[root@localhost /]# docker run -d centos
8607da2063d8cdec6998cefad2d59b719b253acdf62c3cb9432276f61f1f1416
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d168e00c2b26 centos "/bin/bash" 13 seconds ago Exited (0) 12 seconds ago strange_lichterman
#docker ps ,发现centos 停止了!
#常见的坑: docker容器使用后台运行,就必须要有一个前台进程!docker发现没有应用,就会自动停止!
#nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了。
查看日志命令:
docker logs -tf --tail 10 容器ID #没有程序运行,docker logs -tf 是没有日志的。
#自己编写一段shell脚本
"while true;do echo Ayu;sleep 1;done"
#启动centos 循环写入Ayu!
[root@localhost /]# docker run -d centos /bin/bash -c "while true;do echo Ayu;sleep 1;done"
6dcb86cc371ec74cb9d14b7fcade041bbd5dfa806b91fdf0abc2059450c00d2e
#显示日志
-tf #显示日志
--tail number #显示指定条数
[root@localhost /]# docker logs -tf --tail 10 6dcb86cc371e
2020-11-30T02:53:54.455239938Z Ayu
2020-11-30T02:53:55.456983221Z Ayu
2020-11-30T02:53:56.471617331Z Ayu
2020-11-30T02:53:57.480654766Z Ayu
2020-11-30T02:53:58.488584593Z Ayu
2020-11-30T02:53:59.492477296Z Ayu
2020-11-30T02:54:00.496047521Z Ayu
2020-11-30T02:54:01.500665488Z Ayu
2020-11-30T02:54:02.505357362Z Ayu
2020-11-30T02:54:03.508614927Z Ayu
...............
以上日志因为是写了while 循环, 不停输出, 所以不停回显日志
查看容器内部的进程信息
#docker top 容器ID
[root@localhost /]# docker top 6dcb86cc371e
UID PID PPID C STIME TTY TIME CMD
root 28524 28507 0 10:53 ? 00:00:00 /bin/bash -c while true;do echo Ayu;sleep 1;done
root 29089 28524 0 11:01 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看镜像的源数据
#命令 docker inspect 容器ID
#测试:
[root@localhost /]# docker inspect 6dcb86cc371e
[
{
"Id": "6dcb86cc371ec74cb9d14b7fcade041bbd5dfa806b91fdf0abc2059450c00d2e",
"Created": "2020-11-30T02:53:27.654422923Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo Ayu;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 28524,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-11-30T02:53:28.334330512Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
"ResolvConfPath": "/var/lib/docker/containers/6dcb86cc371ec74cb9d14b7fcade041bbd5dfa806b91fdf0abc2059450c00d2e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/6dcb86cc371ec74cb9d14b7fcade041bbd5dfa806b91fdf0abc2059450c00d2e/hostname",
"HostsPath": "/var/lib/docker/containers/6dcb86cc371ec74cb9d14b7fcade041bbd5dfa806b91fdf0abc2059450c00d2e/hosts",
"LogPath": "/var/lib/docker/containers/6dcb86cc371ec74cb9d14b7fcade041bbd5dfa806b91fdf0abc2059450c00d2e/6dcb86cc371ec74cb9d14b7fcade041bbd5dfa806b91fdf0abc2059450c00d2e-json.log",
"Name": "/zealous_poitras",
"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/dc26b34d9eda67e4994e5891feadd9c22ed777e504b5a86605e2a7aff6536552-init/diff:/var/lib/docker/overlay2/7effd8fb5f54d123f31617bdf5c168d46f2bc312f7b8382ab2ddd912eb17e7ed/diff",
"MergedDir": "/var/lib/docker/overlay2/dc26b34d9eda67e4994e5891feadd9c22ed777e504b5a86605e2a7aff6536552/merged",
"UpperDir": "/var/lib/docker/overlay2/dc26b34d9eda67e4994e5891feadd9c22ed777e504b5a86605e2a7aff6536552/diff",
"WorkDir": "/var/lib/docker/overlay2/dc26b34d9eda67e4994e5891feadd9c22ed777e504b5a86605e2a7aff6536552/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "6dcb86cc371e",
"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 Ayu;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": "ad8530b74b9453893fcfb26ca75a1cc95e54613d38321a25999fe03842427d6b",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/ad8530b74b94",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "dbd4c1e8527b2b0b411dc566deabd007338ef6704ce2c8b94096f7591f0ae4f3",
"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": "67b3cfe3804ef518bd68178b866f51aec0914e203e54c3d7002b89186b610bfd",
"EndpointID": "dbd4c1e8527b2b0b411dc566deabd007338ef6704ce2c8b94096f7591f0ae4f3",
"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
}
}
}
}
]
进入当前正在运行的容器
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6dcb86cc371e centos "/bin/bash -c 'while…" 3 hours ago Up 3 hours zealous_poitras
#命令 docker exec -it 容器ID
#测试:
[root@localhost /]# docker exec -it 6dcb86cc371e /bin/bash
#命令:docker attach 容器ID
#测试:
[root@localhost ~]# docker attach 50e58773c6ca
#命令:docker cp 容器ID #将容器中文件cp到主机上
#测试:
[root@localhost home]# docker cp 50e58773c6ca:/Ayu.java /home/
[root@localhost home]# ls
Ayu.java docker
#cp是一个手动过程,我们使用 -v卷的技术,可以实现!
整理于
https://www.bilibili.com/video/BV1og4y1q7M4?p=12
欢迎补充指正!