目录
常用命令
后台启动命令
# 命令 docker run -d 镜像名字
[root@wulijingjing ~]# docker run -d centos
3b4d5e7afa97663280291ab46d4f92ec785fb793155e5a289177f04385e6b589
[root@wulijingjing ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 问题: docker ps 发现 centos 停止了
# 常见的坑:docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动以后,发现自己没有提供服务,就会立刻停止,就没有程序了
查看日志
docker logs -f -t --tail number 容器ID
# 自己编写一段shell脚本
[root@wulijingjing ~]# docker run -d centos /bin/sh -c "while true; do echo hello-world; sleep 1; done"
5b440b8e235abae0995f10ca77a150d363f1608d3e513db47f191227822b5040
# 查看镜像
[root@wulijingjing ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b440b8e235a centos "/bin/sh -c 'while t…" 8 seconds ago Up 7 seconds adoring_haibt
# 查看日志
[root@wulijingjing ~]# docker logs -tf --tail 10 5b440b8e235a
2021-11-12T09:02:36.041245834Z hello-world
2021-11-12T09:02:37.055173734Z hello-world
2021-11-12T09:02:38.062059619Z hello-world
2021-11-12T09:02:39.067590354Z hello-world
2021-11-12T09:02:40.076586160Z hello-world
2021-11-12T09:02:41.085295315Z hello-world
2021-11-12T09:02:42.091247996Z hello-world
2021-11-12T09:02:43.095308249Z hello-world
查看容器中进程信息
[root@wulijingjing ~]# docker run -it centos
[root@e7edbce67299 /]# [root@wulijingjing ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7edbce67299 centos "/bin/bash" 12 seconds ago Up 11 seconds stoic_tu
[root@wulijingjing ~]# docker top e7edbce67299
UID PID PPID C STIME TTY TIME CMD
root 28205 28185 0 17:15 pts/0 00:00:00 /bin/bash
查看容器的元数据
# docker inspect 容器ID
[root@wulijingjing ~]# docker inspect e7edbce67299
[
{
"Id": "e7edbce67299518d3778022fdd9fdf4e2c6caf3b89119dd0d6d6036ce2326a3f",
"Created": "2021-11-12T09:15:43.769487765Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 28205,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-11-12T09:15:44.292038386Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/e7edbce67299518d3778022fdd9fdf4e2c6caf3b89119dd0d6d6036ce2326a3f/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/e7edbce67299518d3778022fdd9fdf4e2c6caf3b89119dd0d6d6036ce2326a3f/hostname",
"HostsPath": "/var/lib/docker/containers/e7edbce67299518d3778022fdd9fdf4e2c6caf3b89119dd0d6d6036ce2326a3f/hosts",
"LogPath": "/var/lib/docker/containers/e7edbce67299518d3778022fdd9fdf4e2c6caf3b89119dd0d6d6036ce2326a3f/e7edbce67299518d3778022fdd9fdf4e2c6caf3b89119dd0d6d6036ce2326a3f-json.log",
"Name": "/stoic_tu",
"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/0bb98bbf1308ea2e3ea529525d9e9681f1e22cf22be3a20758e2a925de95c1d4-init/diff:/var/lib/docker/overlay2/b30364675a808814427004623edc5492db3ae26ba624b85157440b7fcdac458f/diff",
"MergedDir": "/var/lib/docker/overlay2/0bb98bbf1308ea2e3ea529525d9e9681f1e22cf22be3a20758e2a925de95c1d4/merged",
"UpperDir": "/var/lib/docker/overlay2/0bb98bbf1308ea2e3ea529525d9e9681f1e22cf22be3a20758e2a925de95c1d4/diff",
"WorkDir": "/var/lib/docker/overlay2/0bb98bbf1308ea2e3ea529525d9e9681f1e22cf22be3a20758e2a925de95c1d4/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "e7edbce67299",
"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": "c0cdaf33c64b811628ff76107565c665a950389680022277c9f2dec8791ed85a",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/c0cdaf33c64b",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "3b9acce3e87c0289eac23d56de46e9f1bea569b86b8bdcf7f856305c71108802",
"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": "a57b0c04a428019a14c56159a58728814d62664fa1903dc26c4219df69f16fe6",
"EndpointID": "3b9acce3e87c0289eac23d56de46e9f1bea569b86b8bdcf7f856305c71108802",
"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 bashShell;
[root@wulijingjing ~]# docker exec -it e7edbce67299 /bin/bash
[root@e7edbce67299 /]# ll
bash: ll: command not found
[root@e7edbce67299 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 方法二 命令 docker attach 容器ID
[root@wulijingjing ~]# docker attach e7edbce67299
[root@e7edbce67299 /]#
# docker exec 进入容器以后开启一个新的终端,可以在里面操作(常用)
# docker attach 进入容器正在执行的终端,不会启动新的进程!
# 进入容器
[root@wulijingjing home]# docker attach e7edbce67299
[root@e7edbce67299 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@e7edbce67299 /]# cd home
# 在容器中创建文件
[root@e7edbce67299 home]# touch test.java
[root@e7edbce67299 home]# ls
test.java
[root@e7edbce67299 home]# exit
exit
# 没有正在运行的容器,也可以复制文件,只要容器没有被删除就行。
[root@wulijingjing home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@wulijingjing home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7edbce67299 centos "/bin/bash" About an hour ago Exited (0) 12 seconds ago stoic_tu
5b440b8e235a centos "/bin/sh -c 'while t…" About an hour ago Exited (137) About an hour ago adoring_haibt
3b4d5e7afa97 centos "/bin/bash" 2 hours ago Exited (0) 2 hours ago youthful_noyce
8c73f13caa80 centos "/bin/bash" 2 hours ago Exited (0) 2 hours ago intelligent_agnesi
# 将这些文件拷贝出来到主机上
[root@wulijingjing home]# docker cp e7edbce67299:/home/test.java /home
[root@wulijingjing home]# ls
docker skylandMysql test.java vehicle wujingjing
# 拷贝是个手动的过程,未来使用-v卷的技术,可以实现,自动同步。
总结
attach Attach local standard input, output, and error streams to a running container # 当前shell下attach连接指定运行镜像,进入原来的命令行
build Build an image from a Dockerfile # 通过Dockerfile定制镜像
commit Create a new image from a container's changes # 提交当前容器为新的镜像
cp Copy files/folders between a container and the local filesystem # 从容器中拷贝指定文件或者目录到宿主机中
create Create a new container # 创建一个新的容器,但不启动
diff Inspect changes to files or directories on a container's filesystem # 查看docker容器变化
events Get real time events from the server # 从docker服务获取容器实时事件
exec Run a command in a running container # 在已经存在的容器上运行命令
export Export a container's filesystem as a tar archive # 导出容器的内容流作为一个tar归档文件【对应import】
history Show the history of an image # 展示一个镜像形成历史
images List images # 列出系统的所有镜像
import Import the contents from a tarball to create a filesystem image # 从tar包中的内容创建一个新的文件系统映像【对应export】
info Display system-wide information # 显示系统相关的信息
inspect Return low-level information on Docker objects # 查看容器详细信息
kill Kill one or more running containers # kill 指定的 docker 容器
load Load an image from a tar archive or STDIN # 从一个tar包中加载一个镜像【对应save】
login Log in to a Docker registry # 注册或者登录一个docker源服务器
logout Log out from a Docker registry # 从当前的docker registry 退出
logs Fetch the logs of a container # 输出当前容器日志信息
pause Pause all processes within one or more containers # 暂停容器
port List port mappings or a specific mapping for the container # 查看映射端口对应的容器内部源端口
ps List containers # 列出容器列表
pull Pull an image or a repository from a registry # 从docker镜像源服务器拉取指定镜像或者镜像库
push Push an image or a repository to a registry # 推送指定的镜像或者镜像库到docker源服务器
rename Rename a container # 重命名容器
restart Restart one or more containers # 重启运行的容器
rm Remove one or more containers # 移除一个或者多个容器
rmi Remove one or more images # 移除一个或者多个镜像【镜像必须没有容器使用才可删除,或者直接-f强制删除】
run Run a command in a new container # 创建一个新的容器并且运行
save Save one or more images to a tar archive (streamed to STDOUT by default) # 保存一个镜像为一个tar包【对应load】
search Search the Docker Hub for images # 在docker hub中搜索镜像
start Start one or more stopped containers # 启动容器
stats Display a live stream of container(s) resource usage statistics #
stop Stop one or more running containers # 停止镜像
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE # 给源中镜像打标签
top Display the running processes of a container # 查看容器中运行的进程信息
unpause Unpause all processes within one or more containers # 取消暂停容器
update Update configuration of one or more containers
version Show the Docker version information # 查看docker版本号
wait Block until one or more containers stop, then print their exit codes # 截取容器停止时的退出状态