一、使用Docker容器运行Nginx
1.1 使用docker run命令运行Nginx应用
1.1.1 观察下载容器镜像过程
查找本地容器镜像文件;
执行命令过程一:下载容器镜像
# docker run -d nginx:latest
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a2abf6c4d29d: Downloading 1.966MB/31.36MB 下载中
a9edb18cadd1: Downloading 1.572MB/25.35MB
589b7251471a: Download complete 下载完成
186b1aaa4aa6: Download complete
b4df32aa5a72: Waiting 等待下载
a0bcbecc962e: Waiting
1.1.2 观察容器运行情况
[root@hcss-ecs-c9aa ~]# docker run -d nginx:latest
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
e1caac4eb9d2: Pull complete
88f6f236f401: Pull complete
c3ea3344e711: Pull complete
cc1bb4345a3a: Pull complete
da8fa4352481: Pull complete
c7f80e9cdab2: Pull complete
18a869624cb6: Pull complete
Digest: sha256:c26ae7472d624ba1fafd296e73cecc4f93f853088e6a9c13c0d52f6ca5865107
Status: Downloaded newer image for nginx:latest
7703bdc8addcd43ed2c4c688b6421294763ba21272d2b373047de70d7aa8e700
命令解释
docker run 启动一个容器
-d 把容器镜像中需要执行的命令以daemon(守护进程)的方式运行
nginx 应用容器镜像的名称,通常表示该镜像为某一个软件
latest 表示上述容器镜像的版本,表示最新版本,用户可自定义其标识,例如v1或v2等
[root@hcss-ecs-c9aa ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7703bdc8addc nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp eloquent_brahmagupta
7af006b0d7e7 apache/skywalking-oap-server "bash docker-entrypo…" 7 days ago Restarting (1) 57 seconds ago skywalking
2a4db0a36483 elastichq/elasticsearch-hq "supervisord -c /etc…" 8 days ago Up 8 days 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp elastic-hq
83d78562ddfd elasticsearch:7.6.2 "/usr/local/bin/dock…" 8 days ago Up 8 days 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp elasticsearch
3d787242ced8 tomcat "catalina.sh run" 6 weeks ago Up 6 weeks 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp tomcat
db31bdd0c8d3 tomcat "catalina.sh run" 6 weeks ago Up 6 weeks 0.0.0.0:8001->8080/tcp, :::8001->8080/tcp eloquent_austin
[root@hcss-ecs-c9aa ~]#
命令解释
docker ps 类似于Linux系统的ps命令,查看正在运行的容器,如果想查看没有运行的容器,需要在此命令后使用--all
输出内容解释 :
CONTAINERID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES |
---|---|---|---|---|---|---|
9834c8c18a7c | nginx:latest | "/docker-entrypoint.…" | 24 seconds ago | Up 23 seconds | 80/tcp | condescending_pare |
1.2 访问容器中运行的Nginx服务
实际工作中不需要此步操作。
[root@hcss-ecs-c9aa ~]# docker inspect 7703bdc8addc
[
{
"Id": "7703bdc8addcd43ed2c4c688b6421294763ba21272d2b373047de70d7aa8e700",
"Created": "2024-03-12T10:28:34.215898424Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 21181,
"ExitCode": 0,
"Error": "",
"StartedAt": "2024-03-12T10:28:34.768741925Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:e4720093a3c1381245b53a5a51b417963b3c4472d3f47fc301930a4f3b17666a",
"ResolvConfPath": "/var/lib/docker/containers/7703bdc8addcd43ed2c4c688b6421294763ba21272d2b373047de70d7aa8e700/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/7703bdc8addcd43ed2c4c688b6421294763ba21272d2b373047de70d7aa8e700/hostname",
"HostsPath": "/var/lib/docker/containers/7703bdc8addcd43ed2c4c688b6421294763ba21272d2b373047de70d7aa8e700/hosts",
"LogPath": "/var/lib/docker/containers/7703bdc8addcd43ed2c4c688b6421294763ba21272d2b373047de70d7aa8e700/7703bdc8addcd43ed2c4c688b6421294763ba21272d2b373047de70d7aa8e700-json.log",
"Name": "/eloquent_brahmagupta",
"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,
"ConsoleSize": [
42,
238
],
"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": [],
"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",
"/sys/devices/virtual/powercap"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/728f5f7435004ca65c01fb2ef45a4a06416e3078eccab7dd8d0ddc0f17bf6668-init/diff:/var/lib/docker/overlay2/8731b50728d254033b8e6e329f56c023d1be8e3b920f3a532eb53ab2d28a7f46/diff:/var/lib/docker/overlay2/a2fff9de39747996cdb6eb6a88c6ec993dfff3cf7c03db94977a205828c24457/diff:/var/lib/docker/overlay2/19d2261dea489425c90ae08992e07db2f21eca84fd3dfde8a5534ef8365480c1/diff:/var/lib/docker/overlay2/a27b37066449a1c67fa2f77c95857ee00b172cb8b695f9dd9b17d72b6895d933/diff:/var/lib/docker/overlay2/f4b3817f9144577cfa15e45e4a33196deaf23882e3189597b21fbb1ff974a553/diff:/var/lib/docker/overlay2/2368a9382ffd0bff3b2c1c69b8cb5e9b60d182a1c21e52d720f7392d53addbd0/diff:/var/lib/docker/overlay2/3e1ff134e001317963d328a54e60c1d43f66ad340f95daf4299eb12399f310dc/diff",
"MergedDir": "/var/lib/docker/overlay2/728f5f7435004ca65c01fb2ef45a4a06416e3078eccab7dd8d0ddc0f17bf6668/merged",
"UpperDir": "/var/lib/docker/overlay2/728f5f7435004ca65c01fb2ef45a4a06416e3078eccab7dd8d0ddc0f17bf6668/diff",
"WorkDir": "/var/lib/docker/overlay2/728f5f7435004ca65c01fb2ef45a4a06416e3078eccab7dd8d0ddc0f17bf6668/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "7703bdc8addc",
"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.25.4",
"NJS_VERSION=0.8.3",
"PKG_RELEASE=1~bookworm"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "nginx:latest",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "fe05844e7bc3d2263b3364cb72398d10ba59cd90afb77cc634843d5874b7111e",
"SandboxKey": "/var/run/docker/netns/fe05844e7bc3",
"Ports": {
"80/tcp": null
},
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "0d03bd5103585c52e4d3e85923ff75588467a6c7689bf024a419362db2f3cde8",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.6",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:06",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"7703bdc8addc"
],
"MacAddress": "02:42:ac:11:00:06",
"NetworkID": "1c76b6480d2d3a873f6c7450fc22aa10635522d330b52d34378a82a925458ac6",
"EndpointID": "0d03bd5103585c52e4d3e85923ff75588467a6c7689bf024a419362db2f3cde8",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.6",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"DriverOpts": null,
"DNSNames": null
}
}
}
}
]
命令解释
docker inspect 为查看容器结构信息命令
9834 为前面生成的容器ID号前4位,使用这个ID号时,由于其较长,使用时能最短识别即可。
二、Docker命令
2.1 Docker命令获取方法
# docker -h
Flag shorthand -h has been deprecated, please use --helpUsage: docker [OPTIONS] COMMAND 用法
A self-sufficient runtime for containers 功能介绍
Options: 选项
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides
DOCKER_HOST env var and default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")
(default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quitManagement Commands: 管理类命令
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Docker Buildx (Docker Inc., v0.7.1-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc., v0.12.0)
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumesCommands: 未分组命令
attach Attach local standard input, output, and error streams to a running container
build Build an image from a 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
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a 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
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
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
wait Block until one or more containers stop, then print their exit codes
2.2 Docker官网提供的命令帮助
网址链接:Reference documentation | Docker Docs
2.3 Docker命令使用
2.3.1 docker run
# docker run -i -t --name c1 centos:latest bash
[root@948f234e22a1 /]#
命令解释
docker run 运行一个命令在容器中,命令是主体,没有命令容器就会消亡
-i 交互式
-t 提供终端
--name c1 把将运行的容器命名为c1
centos:latest 使用centos最新版本容器镜像
bash 在容器中执行的命令
2.3.2 docker ps
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
命令解释
docker ps 查看正在运行的容器,本案例由于没有命令在容器中运行,因此容器被停止了,所以本次查看没有结果。
[root@hcss-ecs-c9aa ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7703bdc8addc nginx:latest "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 80/tcp eloquent_brahmagupta
7af006b0d7e7 apache/skywalking-oap-server "bash docker-entrypo…" 8 days ago Restarting (1) 27 seconds ago skywalking
2a4db0a36483 elastichq/elasticsearch-hq "supervisord -c /etc…" 8 days ago Up 8 days 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp elastic-hq
83d78562ddfd elasticsearch:7.6.2 "/usr/local/bin/dock…" 8 days ago Up 8 days 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp elasticsearch
e1e710fb0c0f tomcat "catalina.sh run" 6 weeks ago Created tomcat2
3d787242ced8 tomcat "catalina.sh run" 6 weeks ago Up 6 weeks 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp tomcat
0723ca722285 tomcat "/bin/true" 6 weeks ago Created webpage
810577a1d4e7 maoqichuan/docker_run "/bin/bash" 6 weeks ago Exited (0) 6 weeks ago compassionate_bhabha
e321669bab07 maoqichuan/docker_run "/bin/bash" 6 weeks ago Exited (0) 6 weeks ago mystifying_lumiere
db31bdd0c8d3 tomcat "catalina.sh run" 6 weeks ago Up 6 weeks 0.0.0.0:8001->8080/tcp, :::8001->8080/tcp eloquent_austin
84db7796fff5 tomcat "catalina.sh run" 6 weeks ago Created kind_turing
e7984dbc34e5 tomcat "catalina.sh run" 6 weeks ago Exited (130) 6 weeks ago distracted_khorana
f3afcb7e51bb d2c94e258dcb "/hello" 6 weeks ago Exited (0) 6 weeks ago nervous_cartwright
4371779b27f1 d2c94e258dcb "/hello" 6 weeks ago Exited (0) 6 weeks ago
命令解释
docker ps --all 可以查看正在运行的和停止运行的容器
2.3.3 docker inspect
# docker inspect c2
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "d3de2fdbc30ee36a55c1431ef3ae4578392e552009f00b2019b4720735fe5a60",
"EndpointID": "d1a2b7609f2f73a6cac67229a4395eef293f695c0ac4fd6c9c9e6913c9c85c1c",
"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 inpect 查看容器详细信息
2.3.4 docker exec
# docker exec -it c2 ls /root
anaconda-ks.cfg anaconda-post.log original-ks.cfg
命令解释
docker exec 在容器外实现与容器交互执行某命令
-it 交互式
c2 正在运行的容器名称
ls /root 在正在运行的容器中运行相关的命令
2.3.5 docker attach
查看正在运行的容器
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f2eea16da4c centos:latest "bash" 13 minutes ago Up 13 minutes c2
命令解释
docker attach 类似于ssh命令,可以进入到容器中
c2 正在运行的容器名称
2.3.6 docker stop
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f2eea16da4c centos:latest "bash" 22 minutes ago Up 22 minutes c2
# docker stop 9f2eea
9f2eea
2.3.7 docker start
# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f2eea16da4c centos:latest "bash" 22 minutes ago Exited (137) 4 seconds ago c2
# docker start 9f2eea
9f2eea
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f2eea16da4c centos:latest "bash" 24 minutes ago Up 16 seconds c2
2.3.8 docker top
在Docker Host查看容器中运行的进程信息。
# docker top c2
UID PID PPID C STIME TTY TIME CMD
root 69040 69020 0 18:37 pts/0 00:00:00 bash
UID | PID | PPID | C | STIME | TTY | TIME | CMD |
---|---|---|---|---|---|---|---|
root | 69040 | 69020 | 0 | 18:37 | pts/0 | 00:00:00 | bash |
命令解释
docker top 查看container内进程信息,指在docker host上查看,与docker exec -it c2 ps -ef不同。
输出说明
UID 容器中运行的命令用户ID
PID 容器中运行的命令PID
PPID 容器中运行的命令父PID,由于PPID是一个容器,此可指为容器在Docker Host中进程ID
C 占用CPU百分比
STIME 启动时间
TTY 运行所在的终端
TIME 运行时间
CMD 执行的命令
2.3.9 docker rm
如果容器已停止,使用此命令可以直接删除;如果容器处于运行状态,则需要提前关闭容器后,再删除容器。下面演示容器正在运行关闭后删除的方法。
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f2eea16da4c centos:latest "bash" 2 days ago Up 3 seconds c2
# docker stop c2
或
# docker stop 9f2eea16da4c
# docker rm c2
或
# docker rm 9f2eea16da4c