后台启动容器:
docker run -d 容器ID
后台启动容器命令:
docker run -d 容器;ID
[root@localhost ~]# docker run -d centos
a785e61eac2cd9adc784e018ca7985652af8e7f3e0069e17ac6512c682c857d1
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
问题:虽然后台启动了容器,可是docker ps 并未发现启动的容器。
解释:
常见的坑,docker 容器使用后台运行,就必须要有一个前台进程,docker没有发现应用,就会自动停止
nginx,容器启动后后,发现自己没有提供服务,就会立刻停止,就是没有程序。
查看日志
docker logs 容器 #建议加上参数,使查看的日志更清晰明了
查看日志命令:
docker logs -tf --tail 容器
可接参数:
-t 显示时间
-f 显示全部日志
tail number 后面可以接想看的日志条数,比如想看10条日志 --tail 10
后台运行容器,让该容器每隔一秒运行一次脚本,就不会停止容器了
[root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo huage;sleep 1;done"
2d51be42dba9f460db52548106741cc13ecd8afeb30829215c8b3fbec278235b
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2d51be42dba9 centos "/bin/sh -c 'while t…" 7 seconds ago Up 6 seconds practical_lamarr
启动容器后,容器不断执行脚本,我们可以用查看日志命令,查看日志:
[root@localhost ~]# docker logs -tf --tail 8 2d51be42dba9 #查看最近8条日志,带日志时间。
2022-04-16T03:04:45.112916086Z huage
2022-04-16T03:04:46.115966803Z huage
2022-04-16T03:04:47.118753294Z huage
2022-04-16T03:04:48.121216822Z huage
2022-04-16T03:04:49.122943682Z huage
2022-04-16T03:04:50.125310862Z huage
2022-04-16T03:04:51.127814908Z huage
2022-04-16T03:04:52.129570962Z huage
不带参数执行命令:
[root@localhost ~]# docker logs 2d51be42dba9
huage
huage
huage
huage
huage
huage
这里没有截取全部日志,因为不带参数会把启动容器后所有日志都显示出来,很长,且没有时间,不方便查阅
查看容器中进程信息
docker top 容器ID
[root@localhost ~]# docker top 2d51be42dba9
UID PID PPID C STIME TTY
TIME CMD
root 13161 13141 1 11:23 ?
00:00:00 /bin/sh -c while true;do echo huage;sleep 1;done
root 13191 13161 0 11:23 ?
00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看容器或镜像的元数据
docker inspect 镜像ID或容器ID
容器或镜像的各种信息都可以通过这个命令查看
[root@localhost ~]# docker inspect 2d51be42dba9
[
{ #我们在这可以发现,外面显示的容器ID就是这整个ID的前几位。
"Id": "2d51be42dba9f460db52548106741cc13ecd8afeb30829215c8b3fbec278235b",
"Created": "2022-04-16T03:01:43.360845441Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo huage;sleep 1;done" #这里显示出了我们之前编写的shell脚本
],
"State": {
"Status": "running", #正在运行状态
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 13161,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-04-16T03:23:18.689919295Z",
"FinishedAt": "2022-04-16T03:22:57.476629666Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/2d51be42dba9f460db52548106741cc13ecd8afeb30829215c8b3fbec278235b/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/2d51be42dba9f460db52548106741cc13ecd8afeb30829215c8b3fbec278235b/hostname",
"HostsPath": "/var/lib/docker/containers/2d51be42dba9f460db52548106741cc13ecd8afeb30829215c8b3fbec278235b/hosts",
"LogPath": "/var/lib/docker/containers/2d51be42dba9f460db52548106741cc13ecd8afeb30829215c8b3fbec278235b/2d51be42dba9f460db52548106741cc13ecd8afeb30829215c8b3fbec278235b-json.log",
"Name": "/practical_lamarr",
"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/86c16bdf9bea13c6ff37a58a482155df5522073064ea006ac827620e7b7d1e05-init/diff:/var/lib/docker/overlay2/dfe67e3d0e2c320b961f27663d07cfd21ab51f699d623e1a72de1c3d2bdffd42/diff",
"MergedDir": "/var/lib/docker/overlay2/86c16bdf9bea13c6ff37a58a482155df5522073064ea006ac827620e7b7d1e05/merged",
"UpperDir": "/var/lib/docker/overlay2/86c16bdf9bea13c6ff37a58a482155df5522073064ea006ac827620e7b7d1e05/diff",
"WorkDir": "/var/lib/docker/overlay2/86c16bdf9bea13c6ff37a58a482155df5522073064ea006ac827620e7b7d1e05/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "2d51be42dba9", #容器名字
"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 huage;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": { #下面就是一些docker网络的一些参数
"Bridge": "",
"SandboxID": "76423c2069c16775b9a55969218939044773ee3be61ad89d25f26577a3e4e377",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/76423c2069c1",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "63ebd4c3e9d14e8e9d9648a238f3f1d5a5e3b32faec654150fe4eb029ca74be6",
"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": "c33e155773ca2923ab5104f32955b39bfa9e93b96c3ee5bdce282a773b84120c",
"EndpointID": "63ebd4c3e9d14e8e9d9648a238f3f1d5a5e3b32faec654150fe4eb029ca74be6",
"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 exec -it 容器ID /bin/bash #常用
“docker exec”至少需要2个参数。
这个命令进入容器后会打开一个新的终端,更方便操作。
我们通常容器都是使用后台运行的方式,有时候需要修改一些配置,就需要进入容器。
命令:
docker exec-it 容器ID bashShell
测试
以交互模式进入容器,然后运行bash
[root@7f497e338757 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 09:09 pts/0 00:00:00 /bin/bash
root 50 0 0 09:23 pts/1 00:00:00 /bin/bash
root 65 50 0 09:23 pts/1 00:00:00 ps -ef
docker attach 容器ID
docker attach 只需要一个参数
这个命令进入容器不会打开新的终端,会进入之前执行着命令的终端,不方便。
命令
docker attach 容器ID
测试
[root@localhost ~]# docker attach 7f497e338757
[root@7f497e338757 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 09:09 pts/0 00:00:00 /bin/bash
root 50 0 0 09:23 pts/1 00:00:00 /bin/bash
root 66 1 0 09:27 pts/0 00:00:00 ps -ef
docker exec #进入容器后开启一个新的终端,可以在里面操作。(常用)
docker attach #进入容器正在执行的终端,不会启动新的进程。
从容器内拷贝文件到主机上
docker cp 容器ID:容器内地文件路径 存放的主机路径
docker cp 3e954c247eed:/home/hua.txt /home
测试:
在容器内创建一个文件
[root@localhost ~]# docker attach 3e954c247eed
[root@3e954c247eed /]# cd /home
[root@3e954c247eed home]# touch hua.txt
[root@3e954c247eed home]# ls
hua.txt
退出容器,将容器内的文件拷贝到主机上
[root@3e954c247eed home]# exit
将容器home目录的hua.txt文件拷贝到主机的home里
[root@localhost ~]# docker cp 3e954c247eed:/home/hua.txt /home
[root@localhost ~]# cd /home
[root@localhost home]# ls
hua hua.txt
注意:拷贝时要退出容器再使用拷贝命令。容器内使用docker命令会报错。
commit镜像 容器转为镜像
docker commit 提交容器成为一个新的副本
使用方法
docker commit -m="提交的描述信息" -a="作者" 依赖的容器id 取个生成的镜像名:[TAG标签]
参数:
-m 该镜像的描述,描述镜像的特点和功能等
-a 可以添加镜像的作者
使用这些参数要加 = 号
commit就是将镜像层加容器层打包合成一整个镜像,将当前容器的状态打包成一个镜像。
如果想要保存当前容器的状态,就可以通过commit来提交,或得一个镜像。就好比虚拟机拍摄的快照,若想回到之前容器的状态,就可以直接docker run 将这个镜像创建为容器。
常用的命令思维导图:
docker 容器的创建、改名、启动、重启、停止、删除、停止全部容器、删除全部容器等命令思维导图