【Docker学习之三】Docker查找拉取镜像、启动容器、容器使用

时间:2023-06-27 08:22:08

环境
  docker-ce-19.03.1-3.el7.x86_64
  CentOS 7

一、查找、拉取镜像、启动容器
1、查找镜像-docker search
默认查找Docker Hub上的镜像,举例:Docker安装nginx

[root@node105 ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS…
bitnami/nginx Bitnami nginx Docker Image [OK]
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho…
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager [OK]
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… [OK]
crunchgeek/nginx-pagespeed Nginx with PageSpeed + GEO IP + VTS + more_s…
blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. [OK]
centos/nginx--centos7 Platform for running nginx 1.8 or building n…
centos/nginx--centos7 Platform for running nginx 1.12 or building …
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… [OK]
1science/nginx Nginx Docker images that include Consul Temp… [OK]
mailu/nginx Mailu nginx frontend [OK]
pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… [OK]
travix/nginx NGinx reverse proxy [OK]
centos/nginx--centos7 Platform for running nginx 1.10 or building …
wodby/nginx Generic nginx [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX [OK]
[root@node105 ~]#

2、拉取镜像-docker pull

[root@node105 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
1ab2bdfe9778: Pull complete
a17e64cfe253: Pull complete
e1288088c7a8: Pull complete
Digest: sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@node105 ~]#

3、列出本地镜像-docker images
下载完成后,在本地镜像列表里查到 REPOSITORY 为 nginx 的镜像

[root@node105 ~]# docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5a3221f0137b days ago 126MB
[root@node105 ~]#

4、根据镜像启动一个Nginx容器实例-docker run

[root@node105 ~]# docker run --name nginx-test -d -p 8081:80 nginx
db8b3e2f1c647bc2589f04c3984374625455434449ee01e37f0b21163362b052

执行成功返回一行字符串,表示容器ID

--name 为容器设置一个名字

-d 后台运行
-p 端口进行映射,将本地8081端口映射到容器内部的80端口(:左侧是本地端口 右侧是容器内端口)
-P 容器内部端口随机映射到主机的高端口

【Docker学习之三】Docker查找拉取镜像、启动容器、容器使用

另外:docker create创建容器但不启动

eg:

按镜像java创建容器myjava3

$ docker create --name myjava3  -it java /bin/bash

启动myjava3

$ docker start myjava3

以交互方式连接myjava3容器 然后执行/bin/bash

$ docker exec -it 容器ID /bin/bash

5、查看容器运行情况-docker ps

[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db8b3e2f1c64 nginx "nginx -g 'daemon of…" minutes ago Up minutes 0.0.0.0:->/tcp nginx-test
[root@node105 ~]#

6、查看网络端口-docker port

[root@node105 ~]# docker port nginx-test-web
/tcp -> 0.0.0.0:
[root@node105 ~]#

6、查看网络端口-docker port

[root@node105 ~]# docker port nginx-test-web
/tcp -> 0.0.0.0:
[root@node105 ~]#

7、查看应用程序容器的进程-docker top

[root@node105 www]# docker top nginx-test-web
UID PID PPID C STIME TTY TIME CMD
root : ? :: nginx: master process nginx -g daemon off;
: ? :: nginx: worker process

8、查看应用程序日志-docker logs

[root@node105 www]# docker logs -f nginx-test-web

9、检查应用程序-docker inspect 返回一个 JSON 文件记录着 Docker 容器的配置和状态信息

[root@node105 ~]# docker inspect nginx-test-web
[
{
"Id": "398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140",
"Created": "2019-08-25T02:20:55.894180986Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": ,
"ExitCode": ,
"Error": "",
"StartedAt": "2019-08-25T02:20:56.331564845Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5a3221f0137beb960c34b9cf4455424b6210160fd618c5e79401a07d6e5a2ced",
"ResolvConfPath": "/var/lib/docker/containers/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140/hostname",
"HostsPath": "/var/lib/docker/containers/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140/hosts",
"LogPath": "/var/lib/docker/containers/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140-json.log",
"Name": "/nginx-test-web",
"RestartCount": ,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/root/nginx/www:/usr/share/nginx/html",
"/root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf",
"/root/nginx/logs:/var/log/nginx"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": ""
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount":
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": ,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": ,
"Runtime": "runc",
"ConsoleSize": [
, ],
"Isolation": "",
"CpuShares": ,
"Memory": ,
"NanoCpus": ,
"CgroupParent": "",
"BlkioWeight": ,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": ,
"CpuQuota": ,
"CpuRealtimePeriod": ,
"CpuRealtimeRuntime": ,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": ,
"KernelMemoryTCP": ,
"MemoryReservation": ,
"MemorySwap": ,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": ,
"CpuPercent": ,
"IOMaximumIOps": ,
"IOMaximumBandwidth": ,
"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/ec3f5e1f9af11fe6b069a8deaebd664e1ffbea953e44296c99915a9b985cd197-init/diff:/var/lib/docker/overlay2/c4db9bb89c2305db1a0e7cf606393ae448936abe940f6718c49b676a39bfd960/diff:/var/lib/docker/overlay2/379e82acb36eb864e2ec8dc5df0bbce1faaac0d124b4d0f573474465713898d5/diff:/var/lib/docker/overlay2/3689b1b87fe3ce1d82df9b91396f9b6f40db29310589016185d9c1f9b118a082/diff",
"MergedDir": "/var/lib/docker/overlay2/ec3f5e1f9af11fe6b069a8deaebd664e1ffbea953e44296c99915a9b985cd197/merged",
"UpperDir": "/var/lib/docker/overlay2/ec3f5e1f9af11fe6b069a8deaebd664e1ffbea953e44296c99915a9b985cd197/diff",
"WorkDir": "/var/lib/docker/overlay2/ec3f5e1f9af11fe6b069a8deaebd664e1ffbea953e44296c99915a9b985cd197/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/root/nginx/www",
"Destination": "/usr/share/nginx/html",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/root/nginx/conf/nginx.conf",
"Destination": "/etc/nginx/nginx.conf",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/root/nginx/logs",
"Destination": "/var/log/nginx",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "398ec11f39b4",
"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.17.3",
"NJS_VERSION=0.3.5",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "nginx",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "b0edc9c3923fb5aa35c6809212b5d4ac5732344bbf05b94aa45dc9b10312f35d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": ,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": ""
}
]
},
"SandboxKey": "/var/run/docker/netns/b0edc9c3923f",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "dd1bdd3627df73ffbafe6743e59a02a52c9476f4849be086f95933e644df61bb",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": ,
"IPAddress": "172.17.0.3",
"IPPrefixLen": ,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "888aa5a2f0c6e980f85c259b7833c883f358a0a0db907e1d2f4459cffee7d551",
"EndpointID": "dd1bdd3627df73ffbafe6743e59a02a52c9476f4849be086f95933e644df61bb",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": ,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": ,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]

10、关闭容器-docker stop

[root@node105 ~]# docker stop nginx-test
nginx-test
[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@node105 ~]#

11、启动容器-docker start

[root@node105 ~]# docker start nginx-test
nginx-test
[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db8b3e2f1c64 nginx "nginx -g 'daemon of…" minutes ago Up seconds 0.0.0.0:->/tcp nginx-test
[root@node105 ~]#

12、重启容器

[root@node105 ~]# docker restart nginx-test
nginx-test
[root@node105 ~]# docker kill -s HUP nginx-test
nginx-test
[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db8b3e2f1c64 nginx "nginx -g 'daemon of…" minutes ago Up seconds 0.0.0.0:->/tcp nginx-test
[root@node105 ~]#

-s HUP 向容器发送HUP信号,表示重新加载

13、移除应用容器,容器必须是关闭的

[root@node105 ~]# docker rm nginx-test-web
nginx-test-web

二、容器使用

举例:nginx部署
1、将容器内文件拷贝出来

[root@node105 nginx]# docker cp db8b3e2f1c64:/etc/nginx/nginx.conf ~/nginx/conf

2、在本机新建部署nginx目录

[root@node105 ~]# mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf
[root@node105 ~]# ll
total
-rw-------. root root Aug : anaconda-ks.cfg
drwxr-xr-x. root root Aug : nginx

www: 目录将映射为nginx容器配置的虚拟目录。

logs: 目录将映射为nginx容器的日志目录。
conf: 目录里的配置文件将映射为nginx容器的配置文件。

3、部署命令

[root@node105 ~]# docker run -d -p 8082:80 --name nginx-test-web -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx
398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140
[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
398ec11f39b4 nginx "nginx -g 'daemon of…" seconds ago Up seconds 0.0.0.0:->/tcp nginx-test-web
[root@node105 ~]#

-v 将冒号左侧本机目录挂载到冒号右侧容器的目录,创建映射。

4、测试
进入 ~/nginx/www 目录,创建 index.html 文件,内容如下:

[root@node105 ~]# cd ./nginx/www
[root@node105 www]# vi index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>docker nginx部署测试</title>
</head>
<body>
<h1>我的第一个标题</h1>
<p>我的第一个段落。</p>
</body>
</html>

【Docker学习之三】Docker查找拉取镜像、启动容器、容器使用

参考:
CentOS Docker 安装
https://www.runoob.com/docker/centos-docker-install.html
Windows Docker 安装
https://www.runoob.com/docker/windows-docker-install.html
Docker 命令大全
https://www.runoob.com/docker/docker-command-manual.html
Docker 资源汇总
https://www.runoob.com/docker/docker-resources.html