有了镜像才能创建容器,下载一个镜像
[root@ecs-279667 ~]# docker pull centos
#新建容器
docker run [可选参数] image
–name “name” 容器名称
-d 后台方式运行
-it 使用交互方式运行,进入容器查看命令
-p 指定容器的端口 -p 8080:8080
ip:主机端口:容器端口
主机端口:容器端口
容器端口
-P (大P)随机指定端口
#测试,启动并进入容器
[root@ecs-279667 ~]# docker run -it centos /bin/bash
[root@5ef275b2f176 /]# ls #查看容器内的centos
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@5ef275b2f176 /]#
#从容器中退出到主机
[root@5ef275b2f176 /]# exit;
列出所有运行的容器
docker ps #列出当前正在运行的容器
-a #列出当前正在运行的容器,包括历史运行的容器
-a -n #列出最近运行的容器
#示例:
[root@ecs-279667 /]# docker ps -a -n=1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ef275b2f176 centos “/bin/bash” 8 minutes ago Exited (0) 6 minutes ago charming_dhawan
-q #只显示容器的编号
[root@ecs-279667 /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ef275b2f176 centos “/bin/bash” 3 minutes ago Exited (0) 2 minutes ago charming_dhawan
cb3a408e1fcc feb5d9fea6a5 “/hello” 2 weeks ago Exited (0) 2 weeks ago interesting_shaw
退出容器
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 logs -f -t --tail
查看镜像的元数据
docker inspect 容器id
[root@ecs-279667 ~]# docker inspect 0fd8280d3048
[
{
“Id”: “0fd8280d304875265b7c447b0be16952f6e9304a7f22bcbbecee86deb5ea6fd5”,
“Created”: “2021-12-26T11:52:31.91931487Z”,
“Path”: “/bin/bash”,
“Args”: [],
“State”: {
“Status”: “running”,
“Running”: true,
“Paused”: false,
“Restarting”: false,
“OOMKilled”: false,
“Dead”: false,
“Pid”: 106192,
“ExitCode”: 0,
“Error”: “”,
“StartedAt”: “2021-12-26T11:52:32.381424509Z”,
“FinishedAt”: “0001-01-01T00:00:00Z”
},
“Image”: “sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6”,
“ResolvConfPath”: “/var/lib/docker/containers/0fd8280d304875265b7c447b0be16952f6e9304a7f22bcbbecee86deb5ea6fd5/resolv.conf”,
“HostnamePath”: “/var/lib/docker/containers/0fd8280d304875265b7c447b0be16952f6e9304a7f22bcbbecee86deb5ea6fd5/hostname”,
“HostsPath”: “/var/lib/docker/containers/0fd8280d304875265b7c447b0be16952f6e9304a7f22bcbbecee86deb5ea6fd5/hosts”,
“LogPath”: “/var/lib/docker/containers/0fd8280d304875265b7c447b0be16952f6e9304a7f22bcbbecee86deb5ea6fd5/0fd8280d304875265b7c447b0be16952f6e9304a7f22bcbbecee86deb5ea6fd5-json.log”,
“Name”: “/vigilant_nobel”,
“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/4853f7682b488378299a9b6c27ffedb72d1f7c31e580cf62deb651fb351d4280-init/diff:/var/lib/docker/overlay2/764962f3dbb4c25dedc16cca503337ad899adb3e8e76c8c7e239d5e960678f2c/diff”,
“MergedDir”: “/var/lib/docker/overlay2/4853f7682b488378299a9b6c27ffedb72d1f7c31e580cf62deb651fb351d4280/merged”,
“UpperDir”: “/var/lib/docker/overlay2/4853f7682b488378299a9b6c27ffedb72d1f7c31e580cf62deb651fb351d4280/diff”,
“WorkDir”: “/var/lib/docker/overlay2/4853f7682b488378299a9b6c27ffedb72d1f7c31e580cf62deb651fb351d4280/work”
},
“Name”: “overlay2”
},
“Mounts”: [],
“Config”: {
“Hostname”: “0fd8280d3048”,
“Domainname”: “”,
“User”: “”,
“AttachStdin”: true,
“AttachStdout”: true,
“AttachStderr”: true,
“Tty”: true,
“OpenStdin”: true,
“StdinOnce”: true,
“Env”: [
“PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”
],
“Cmd”: [
“/bin/bash”
],
“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”: “15b7a16f68a27e2703ca9f2a917d5131d94f9abc6d2ce67338917377c1737b6c”,
“HairpinMode”: false,
“LinkLocalIPv6Address”: “”,
“LinkLocalIPv6PrefixLen”: 0,
“Ports”: {},
“SandboxKey”: “/var/run/docker/netns/15b7a16f68a2”,
“SecondaryIPAddresses”: null,
“SecondaryIPv6Addresses”: null,
“EndpointID”: “e5903ee0fb1412c4fc7a120aea50804b22e0922b6de50ed1aa57a961b0ab0d1d”,
“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”: “c6e17621d6b9f4488d222da8f7c6a4600af795a90b4df71a1538f61bdec58eee”,
“EndpointID”: “e5903ee0fb1412c4fc7a120aea50804b22e0922b6de50ed1aa57a961b0ab0d1d”,
“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@ecs-279667 ~]#
进入当前正在运行的容器
#容器通常都是后台运行,如何进入容器呢
docker exec -it 容器id bashshell
[root@ecs-279667 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0fd8280d3048 centos “/bin/bash” 30 minutes ago Up 30 minutes vigilant_nobel
[root@ecs-279667 ~]# docker exec -it 0fd8280d3048 /bin/bash
[root@0fd8280d3048 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@0fd8280d3048 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 11:52 pts/0 00:00:00 /bin/bash
root 17 0 0 12:25 pts/1 00:00:00 /bin/bash
root 32 17 0 12:26 pts/1 00:00:00 ps -ef
[root@0fd8280d3048 /]#
#第二种方式进去容器
docker attach 容器id
[root@ecs-279667 ~]# docker attach 0fd8280d3048
[root@0fd8280d3048 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#区别
docker exec #进入容器后开启一个新的终端,可以在里面操作
docker attach #进入容器正在执行的终端,不会开启新的进程