docker run:表示创建和启动:
docker run+镜像名。若镜像不存在本地,就会在线去下载该镜像。
注意:
1、容器内的进程必须处于前台运行状态,否则容器就会直接退出。自己部署一个容器运行,命令不得后台运行,前台运行即可。
2、如果容器内,什么事也没做,容器也会挂掉。容器内,必须有一个进程在前台运行。
#运行容器的玩法
#1.运行一个挂掉的容器,(从错误示范来学docker容器)
[root@Hadoop2 ~]# docker run 5d0da3dc9764
[root@Hadoop2 ~]# docker run 5d0da3dc9764
^[[A[root@Hadoop2 ~]# docker run 5d0da3dc9764
[root@Hadoop2 ~]# docker run 5d0da3dc9764
[root@Hadoop2 ~]# docker run 5d0da3dc9764
[root@Hadoop2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
这个写法,会产生多个独立的容器记录,且容器内没有程序在运行,因此挂掉。
[root@Hadoop2 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a5b4fbb2725 5d0da3dc9764 "/bin/bash" 24 seconds ago Exited (0) 23 seconds ago vigorous_swirles
36364da5f661 5d0da3dc9764 "/bin/bash" 25 seconds ago Exited (0) 24 seconds ago nifty_banach
d596bf2ef05e 5d0da3dc9764 "/bin/bash" 28 seconds ago Exited (0) 27 seconds ago dreamy_kowalevski
5339d8bfe246 5d0da3dc9764 "/bin/bash" 29 seconds ago Exited (0) 28 seconds ago laughing_chaum
c6a69c5a7be3 5d0da3dc9764 "/bin/bash" 31 seconds ago Exited (0) 30 seconds ago hopeful_joliot
4a77939ee186 nginx "/docker-entrypoint.…" 7 minutes ago Exited (0) 6 minutes ago affectionate_chatelet
[root@Hadoop2 ~]#
进入容器内,且在容器内执行某个命令
[root@Hadoop2 ~]# docker run -it centos sh
sh-4.4# cat /etc/redhat-release
CentOS Linux release 8.4.2105
开启某个容器,让它帮你执行某个命令,属于前台运行会卡住一个终端。
[root@Hadoop2 ~]# docker run centos ping www.baidu.com
PING www.a.shifen.com (14.119.104.189) 56(84) bytes of data.
64 bytes from 14.119.104.189 (14.119.104.189): icmp_seq=1 ttl=127 time=37.7 ms
-d参数,让容器在后台跑着(针对宿主机而言)
返回容器ID
[root@Hadoop2 ~]# docker run -d centos ping baidu.com
23c59f80b4e441ef935bfdba926c66f07df955d25ad4397ecdc74315441f7aa2
[root@Hadoop2 ~]#
运行一个活着的容器,Docker ps可以看到的容器
[root@Hadoop2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23c59f80b4e4 centos "ping baidu.com" 49 seconds ago Up 48 seconds fervent_perlman
[root@Hadoop2 ~]#
3、丰富docker运行的参数。-d:后台运行,--rm容器挂掉后自动删除,--name给容器起名
启动一个容器
[root@Hadoop2 ~]# docker run -d --rm --name JinCentos centos ping baidu.com
b9b28ae6274191a22722adf1c72d62e3a9ce9b49394bd0d36ded0f6bc65e4cbc
[root@Hadoop2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b9b28ae62741 centos "ping baidu.com" 4 seconds ago Up 3 seconds JinCentos
23c59f80b4e4 centos "ping baidu.com" 11 minutes ago Up 11 minutes fervent_perlman
停止该容器
[root@Hadoop2 ~]# docker stop b9b28ae62741
b9b28ae62741
[root@Hadoop2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23c59f80b4e4 centos "ping baidu.com" 12 minutes ago Up 12 minutes fervent_perlman
容器被删除,即容器历史被删除。
[root@Hadoop2 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23c59f80b4e4 centos "ping baidu.com" 12 minutes ago Up 12 minutes fervent_perlman
2c62952389ed centos "ping www.baidu.com" 21 minutes ago Exited (0) 21 minutes ago admiring_yalow
2545a30fdfae centos "sh" 28 minutes ago Exited (127) 22 minutes ago trusting_burnell
0a5b4fbb2725 5d0da3dc9764 "/bin/bash" 57 minutes ago Exited (0) 57 minutes ago vigorous_swirles
36364da5f661 5d0da3dc9764 "/bin/bash" 57 minutes ago Exited (0) 57 minutes ago nifty_banach
d596bf2ef05e 5d0da3dc9764 "/bin/bash" 57 minutes ago Exited (0) 57 minutes ago dreamy_kowalevski
5339d8bfe246 5d0da3dc9764 "/bin/bash" 57 minutes ago Exited (0) 57 minutes ago laughing_chaum
c6a69c5a7be3 5d0da3dc9764 "/bin/bash" 57 minutes ago Exited (0) 57 minutes ago hopeful_joliot
4a77939ee186 nginx "/docker-entrypoint.…" About an hour ago Exited (0) About an hour ago affectionate_chatelet
[root@Hadoop2 ~]#
4、查看容器日志
#-f每秒刷新他
[root@Hadoop2 ~]# docker logs -f 23c59f80b4e4
[root@Hadoop2 ~]# docker logs 23c59f80b4e4 | tail -5
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1320 ttl=127 time=44.7 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1321 ttl=127 time=44.9 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1322 ttl=127 time=44.7 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1323 ttl=127 time=46.3 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1324 ttl=127 time=45.7 ms
[root@Hadoop2 ~]#
5、进入正在运行的容器空间内,挂了的容器进不去哈。
exec:进入容器内、-it:交付式操作(i表示可以输入命令,t表示打开终端,合起来就是在Xshell中输入命令)
[root@Hadoop2 ~]# docker exec -it 2009e3dc9226 bash
容器中跑起来的进程如下:
ps:查看服务器的进程信息;
-e:等价于 ‘-A’ ,表示列出全部的进程;
-f:显示全部的列(显示全字段)
[root@2009e3dc9226 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 02:49 ? 00:00:00 ping www.baidu.com
root 7 0 0 02:51 pts/0 00:00:00 bash
root 21 7 0 02:57 pts/0 00:00:00 ps -ef
exec进入容器空间内,不能进入镜像内
[root@Hadoop2 ~]# docker exec -it nginx sh
Error response from daemon: No such container: nginx
进入容器内,但是这种方式好像不好用,tab补全功能都无法实现
[root@Hadoop2 ~]# docker run -it nginx sh
# ls
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
# ca ^H^H^H^H^[[B
sh: 2: ca: not found
# cat /et ^H^H^H
cat: /et: No such file or directory
cat: ''$'\b\b\b': No such file or directory
# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
# pwd
/
#
6、查看容器的详细信息,用于高级的调试:
[root@Hadoop2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2009e3dc9226 centos "ping www.baidu.com" 17 minutes ago Up 17 minutes wonderful_benz
[root@Hadoop2 ~]# docker container inspect 2009e3dc9226
下面是容器信息:
[
{
"Id": "2009e3dc92266f542b3183202797fd872a669bf372f9707b20d72abbe4db8a9b",
"Created": "2023-09-25T02:49:29.374550017Z",
"Path": "ping",
"Args": [
"www.baidu.com"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2699,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-09-25T02:49:29.698895563Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/2009e3dc92266f542b3183202797fd872a669bf372f9707b20d72abbe4db8a9b/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/2009e3dc92266f542b3183202797fd872a669bf372f9707b20d72abbe4db8a9b/hostname",
"HostsPath": "/var/lib/docker/containers/2009e3dc92266f542b3183202797fd872a669bf372f9707b20d72abbe4db8a9b/hosts",
"LogPath": "/var/lib/docker/containers/2009e3dc92266f542b3183202797fd872a669bf372f9707b20d72abbe4db8a9b/2009e3dc92266f542b3183202797fd872a669bf372f9707b20d72abbe4db8a9b-json.log",
"Name": "/wonderful_benz",
"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": [
42,
163
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"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/bfeb129864811361b2418120a417ad83ce67380729b0f8b831bd013a12d47b8b-init/diff:/var/lib/docker/overlay2/6dddfccfb15dace43a872d1e3ff8826ddbf6e85a21069370284530793d41e55d/diff",
"MergedDir": "/var/lib/docker/overlay2/bfeb129864811361b2418120a417ad83ce67380729b0f8b831bd013a12d47b8b/merged",
"UpperDir": "/var/lib/docker/overlay2/bfeb129864811361b2418120a417ad83ce67380729b0f8b831bd013a12d47b8b/diff",
"WorkDir": "/var/lib/docker/overlay2/bfeb129864811361b2418120a417ad83ce67380729b0f8b831bd013a12d47b8b/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "2009e3dc9226",
"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": [
"ping",
"www.baidu.com"
],
"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": "9109beb39f3fe2709208bff3768eb5a033660a4cf175bea0fa93a312d9cb05a0",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/9109beb39f3f",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "15e3dda79356d6cc24fc38d49b62ab95af71b52f3c093c78eb3cd1979811ef0f",
"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": "82f7a83705441cfaab812eda444fb7161c2397c3f02cd1a2dd630a7754b19197",
"EndpointID": "15e3dda79356d6cc24fc38d49b62ab95af71b52f3c093c78eb3cd1979811ef0f",
"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
}
}
}
}
]
7、容器的端口映射
[root@Hadoop2 ~]# netstat -tunlp | grep 80
#-d后台运行容器,且起个名字,且端口映射宿主机的85端口,访问到容器内的80端口
[root@Hadoop2 ~]# docker run -d --name ShuJiNginx -p 85:80 nginx
0d2a29a139fa2328c0f39bc3894fb8668fdf276c169f5ce228d49bdd06fd54e8
查看运行的容器
[root@Hadoop2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0d2a29a139fa nginx "/docker-entrypoint.…" 20 seconds ago Up 19 seconds 0.0.0.0:85->80/tcp, :::85->80/tcp ShuJiNginx
ad8a2f80b607 nginx "/docker-entrypoint.…" 6 hours ago Up 6 hours 80/tcp busy_darwin
2009e3dc9226 centos "ping www.baidu.com" 7 hours ago Up 7 hours wonderful_benz
查看容器的端口转发情况
docker port 容器ID
[root@Hadoop2 ~]# docker port 0d2a29a139fa
80/tcp -> 0.0.0.0:85
80/tcp -> [::]:85
随机端口映射,-P随机访问一个宿主机的空闲端口,映射到容器内打开的端口。
[root@Hadoop2 ~]# docker run -d --name shushu -P nginx
8fb7285f1191ca5c82e4d9f035c2fee0da1e00778a7342c2a83cf3d1cd7a74de
[root@Hadoop2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8fb7285f1191 nginx "/docker-entrypoint.…" 13 seconds ago Up 11 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp shushu
0d2a29a139fa nginx "/docker-entrypoint.…" 19 minutes ago Up 19 minutes 0.0.0.0:85->80/tcp, :::85->80/tcp ShuJiNginx
ad8a2f80b607 nginx "/docker-entrypoint.…" 6 hours ago Up 6 hours 80/tcp busy_darwin
2009e3dc9226 centos "ping www.baidu.com" 7 hours ago Up 7 hours wonderful_benz
8、容器的提交:
#运行基础的centos,在容器内安装vim,然后提交新的镜像;
#新的镜像,再运行处的容器,默认就携带vim;
[root@Hadoop2 ~]# docker run -it centos bash
安装vim
[root@0343a89f71fc yum.repos.d]# yum install vim -y
提交镜像
[root@Hadoop2 ~]# docker commit 0343a89f71fc shujidockerhuzhangao/jingxiangming
sha256:e2ce129f8ae8a5bebb3ebe36bc489f418ae3f6d75aeae8bc075f4f865fc1ab85
[root@Hadoop2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
shujidockerhuzhangao/jingxiangming latest e2ce129f8ae8 10 seconds ago 559MB
b57d813f9e5d 2 days ago 559MB
nginx latest f5a6b296b8a2 2 weeks ago 187MB
centos latest 5d0da3dc9764 2 years ago 231MB
#使用这个镜像
[root@Hadoop2 ~]# docker run -it shujidockerhuzhangao/jingxiangming bash
[root@bb3717503096 /]# vim shuji.txt
[root@bb3717503096 /]# cat shuji.txt
shu ji yao jia you wa !!!!!
[root@bb3717503096 /]#