Preface
So the blogger here has some columns full of useful information!
The first is a summary of the blogger’s high-quality blogs. The blogs in this column are all written by the blogger with the most care. They are full of useful information. I hope it will be helpful to everyone.
Then there is the column that the blogger spends the most time on recently, "Docker from Realization to Practice to Underlying Principles". I hope everyone will pay more attention to it!
Container operation cases
Basic operations of containers
Start a container while pulling.
Check the container's information to see if it is running normally.
docker container inspect mynginx001 > tmp.json # 查看信息,重定向到一个json文件中
[
{
"Id": "6a1a254beda42a72127867e68474c30673829bbdcdaf5f6c4a3ac05aab5355e0",
"Created": "2023-09-15T08:44:54.599522581Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 532,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-09-15T08:44:55.79180895Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:08a1cbf9c69edd2ab8e5250ae97703f60b9393fc5a4827cedda4b7387a5cfc6a",
"ResolvConfPath": "/data/var/lib/docker/containers/6a1a254beda42a72127867e68474c30673829bbdcdaf5f6c4a3ac05aab5355e0/resolv.conf",
"HostnamePath": "/data/var/lib/docker/containers/6a1a254beda42a72127867e68474c30673829bbdcdaf5f6c4a3ac05aab5355e0/hostname",
"HostsPath": "/data/var/lib/docker/containers/6a1a254beda42a72127867e68474c30673829bbdcdaf5f6c4a3ac05aab5355e0/hosts",
"LogPath": "/data/var/lib/docker/containers/6a1a254beda42a72127867e68474c30673829bbdcdaf5f6c4a3ac05aab5355e0/6a1a254beda42a72127867e68474c30673829bbdcdaf5f6c4a3ac05aab5355e0-json.log",
"Name": "/mynginx001",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {
}
},
"NetworkMode": "default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "8080"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
31,
108
],
"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",
"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,
"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": "/data/var/lib/docker/overlay2/23661dd6ebb5c432ce3f1448ac08d038e59ad101b29efe29addefd165b4104a0-init/diff:/data/var/lib/docker/overlay2/e5d076862da7aee474d0550d60c5851dc793757e7ed03fa5fb817c2fa5f1a408/diff:/data/var/lib/docker/overlay2/0351e0d22c137be4b1410c4ba4266d7f7933196345b27d5d9bc22045dfee3d75/diff:/data/var/lib/docker/overlay2/72ccc6676d75d479366ce8182c4ce7be156e8b6e3543ce2e6732b6274a79e4d7/diff:/data/var/lib/docker/overlay2/67bfca339fd7a87963e91b7eb415c9599dbcc18ce95cdbf8c9ef2335e38e5d1a/diff:/data/var/lib/docker/overlay2/9a7b148946cfeb69cb1037446f607a2da6a59c0f1bda46e7d68ed378a8f760e9/diff:/data/var/lib/docker/overlay2/23faacc4402854b8f869f2398bd4efcf323359b9e04830f7c177936c216a6de4/diff",
"MergedDir": "/data/var/lib/docker/overlay2/23661dd6ebb5c432ce3f1448ac08d038e59ad101b29efe29addefd165b4104a0/merged",
"UpperDir": "/data/var/lib/docker/overlay2/23661dd6ebb5c432ce3f1448ac08d038e59ad101b29efe29addefd165b4104a0/diff",
"WorkDir": "/data/var/lib/docker/overlay2/23661dd6ebb5c432ce3f1448ac08d038e59ad101b29efe29addefd165b4104a0/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "6a1a254beda4",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {
}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.22.0",
"NJS_VERSION=0.7.6",
"PKG_RELEASE=1~bullseye"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "nginx:1.22.0",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <[email protected]>"
},
"StopSignal": "SIGQUIT"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "555fb0c11fc23cf0aa2b6b3a21399152208941da805c2428a1a2c844aa3871ec",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
},
{
"HostIp": "::",
"HostPort": "8080"
}
]
},
"SandboxKey": "/var/run/docker/netns/555fb0c11fc2",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "9b30e85ff2551851435264a5ef8517887c4a5f38603cba90fe9dd1a4ca1bbdde",
"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": "c628e2b0d777ee8674106b75d24bc0ba856047abced6d2ad84e101611461092a",
"EndpointID": "9b30e85ff2551851435264a5ef8517887c4a5f38603cba90fe9dd1a4ca1bbdde",
"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
}
}
}
}
]
Clean a container.
Container state migration
Create a container.
The status at this time is Created
.
It is now running.
Of course, we can also perform various operations to pause, start, etc.
Container batch processing tips
Batch search for containers
Order | explain |
---|---|
docker container ls -qf name=xxx |
Filter by name to get container number |
docker container ls --filter status=running |
Filter container information based on status |
docker container ls -aq |
Silently obtain all container IDs |
docker container ls --filter ancestor=xxx |
Filter container information with image name xxx |
-a :表示打印所有的容器信息, 包括正在运行和已经退出的
-q : 表示只返回容器 ID
-f : 表示基于给的条件过滤 等价于 --filter 选项
First start some containers and make some preparations.
Stop one.
Start filtering.
Filter mynginx003
containers named.
[root@ALiCentos7:~]$ docker ps -f name=mynginx003
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4f00f4a7544 nginx:1.21.4 "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp mynginx003
[root@ALiCentos7:~]$
Filter running containers.
[root@ALiCentos7:~]$ docker ps -f status=running
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64f920d38e0f nginx:1.21.4 "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp mynginx004
d4f00f4a7544 nginx:1.21.4 "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp mynginx003
447d44e1d5b3 nginx:1.22.0 "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp mynginx002
38b14c5cd3d3 nginx:1.21.4 "/docker-entrypoint.…" 17 minutes ago Up 16 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx001
[root@ALiCentos7:~]$
The filter image is nginx:1.21.4
a container.
[root@ALiCentos7:~]$ docker ps -f ancestor=nginx:1.21.4
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64f920d38e0f nginx:1.21.4 "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp mynginx004
d4f00f4a7544 nginx:1.21.4 "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp mynginx003
38b14c5cd3d3 nginx:1.21.4 "/docker-entrypoint.…" 19 minutes ago Up 18 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx001
[root@ALiCentos7:~]$
-q
Just returnid
[root@ALiCentos7:~]$ docker ps -f ancestor=nginx:1.21.4 -q
64f920d38e0f
d4f00f4a7544
38b14c5cd3d3
[root@ALiCentos7:~]$
Container batch operations
After we get this id
, we can perform batch operations on the containers.
docker stop `docker ps -q` # 把docker ps -q的搜索结果送给docker stop
Container interaction mode
attached mode
All logs bash
are printed to the server, and ^C
the container will exit if the network is disconnected at this time.
The attach mode generally cannot be used in production environments and is generally used in production environments.
detached mode
It is not affected by bash, runs in the background, and is a daemon process.
So how to view the logs of containers running in the background?
Attach to a detached running container
docker attach mynginx001
At this point, the container becomes running in the foreground.
interactive mode
interactive mode.
But this nginx
is not started at this time, we need to bash
start it manually inside the container.
Of course, you can also use docker exec
commands to interact with a container running in the background.
Container and host content copy
We first index.html
copy the contents inside the container to the host, and then after making certain modifications on the host, copy them back to the container.
Start a container first.
Find this inside the containerindex.html
Copy things locally.
Make a change to the title.
Copy it back.
Visit again now and have a look.
Modifications have been completed.
Note: Copying files between containers is not supported.
Container automatically deleted
Bring --rm
options
- Create a foreground run into the container, exit and see what happens
- Create a background run into the container
docker stop
and see what happens
At this point we discovered that it had been automatically deleted.
--rm
Options must be used with caution when working, in case the container stops, the items will be gone.
Container automatically restarts
Container restart options are as follows:
docker run --restart=no [容器名] :默认值不自动重启
docker run --restart=on-failure:3 [容器名] : on-failure # 若容器的退出状态非 0,则docker 自动重启容器,还可以指定重启次数,若超过指定次数未能启动容器则放弃。
docker run --restart=always [容器名] :always # 容器退出时总是重启
docker run --restart=unless-stopped [容器名] unless-stopped # 容器退出时总是重启,但不考虑 Docker 守护进程启动时就已经停止的容器。
If the –restart parameter is not set when the container is started, update it with the following command:
docker update --restart=always [容器名]
Container environment variable settings
As mentioned before when learning commands, -e
options can set environment variables.
If you want to set up multiple ones, there are two ways.
- Set with multiple
-e
options - Use a configuration file
Set up the configuration file.
View container details
docker container inspect 容器
Container executes single line command
We can directly use the docker container environment to execute some commands. For example, when there is a certain command in the container but not on the host, we can use the container to complete certain tasks.
Container image import and export
docker export
anddocker import
docker save
anddocker load
View container logs
See docker logs
the detailed explanation of the command.
Container resource view
You can see detailed explanations of docker top
Hedu docker stats
commands.