【Docker学习之三】Docker查找拉取镜像、启动容器、容器使用
环境
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. 11866 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1641 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 736 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS… 73 bitnami/nginx Bitnami nginx Docker Image 70 [OK] tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 51 [OK] nginxdemos/hello NGINX webserver that serves a simple page co… 24 [OK] jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 23 nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 20 jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 20 [OK] schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 17 [OK] crunchgeek/nginx-pagespeed Nginx with PageSpeed + GEO IP + VTS + more_s… 13 blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 12 [OK] centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 11 centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 10 nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 9 nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 5 sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 5 [OK] 1science/nginx Nginx Docker images that include Consul Temp… 5 [OK] mailu/nginx Mailu nginx frontend 3 [OK] pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… 2 [OK] travix/nginx NGinx reverse proxy 2 [OK] centos/nginx-110-centos7 Platform for running nginx 1.10 or building … 0 wodby/nginx Generic nginx 0 [OK] ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 0 [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 9 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 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…" 2 minutes ago Up 2 minutes 0.0.0.0:8081->80/tcp nginx-test [root@node105 ~]#
6、查看网络端口-docker port
[root@node105 ~]# docker port nginx-test-web 80/tcp -> 0.0.0.0:8082 [root@node105 ~]#
6、查看网络端口-docker port
[root@node105 ~]# docker port nginx-test-web 80/tcp -> 0.0.0.0:8082 [root@node105 ~]#
7、查看应用程序容器的进程-docker top
[root@node105 www]# docker top nginx-test-web UID PID PPID C STIME TTY TIME CMD root 16451 16435 0 10:20 ? 00:00:00 nginx: master process nginx -g daemon off; 101 16482 16451 0 10:20 ? 00:00:00 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": 16451, "ExitCode": 0, "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": 0, "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": "8082" } ] }, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "Capabilities": null, "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/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": 0, "Ports": { "80/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "8082" } ] }, "SandboxKey": "/var/run/docker/netns/b0edc9c3923f", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "dd1bdd3627df73ffbafe6743e59a02a52c9476f4849be086f95933e644df61bb", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.3", "IPPrefixLen": 16, "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": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "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…" 16 minutes ago Up 4 seconds 0.0.0.0:8081->80/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…" 18 minutes ago Up 25 seconds 0.0.0.0:8081->80/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 4 -rw-------. 1 root root 1779 Aug 15 14:39 anaconda-ks.cfg drwxr-xr-x. 5 root root 41 Aug 25 10:16 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…" 22 seconds ago Up 20 seconds 0.0.0.0:8082->80/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>
参考:
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