Docker部署Nginx应用(2)

Docker部署Nginx应用(2)

1、拉取Nginx镜像

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
f7e2b70d04ae: Pull complete 
08dd01e3f3ac: Pull complete 
d9ef3a1eb792: Pull complete 
Digest: sha256:98efe605f61725fd817ea69521b0eeb32bef007af0e3d0aeb6258c6e6fe7fc1a
Status: Downloaded newer image for nginx:latest

2、查看拉取到的镜像

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              881bd08c0b08        7 days ago          109MB

3、在Docker中启用Nginx

[root@localhost ~]# docker run -p 80:80 --name myNginx -d nginx
ad7cc45c81ffadbd44254c1c043e048c677a4ebcd4ca618efcd098729cec2fa5

4、查看启动的nginx进程

[root@localhost ~]# ps -ef|grep nginx
root      13223  13208  0 05:50 ?        00:00:00 nginx: master process nginx -g daemon off;
101       13253  13223  0 05:50 ?        00:00:00 nginx: worker process
root      13262  12350  0 05:50 pts/1    00:00:00 grep --color=auto nginx

5、访问nginx

[root@localhost ~]# curl http://localhost:80/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
         35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

6、监测Nginx引用状态

[root@localhost ~]# docker inspect ad7cc45c81ffadbd44254c1c043e048c677a4ebcd4ca618efcd098729cec2fa5
[
    {
        "Id": "ad7cc45c81ffadbd44254c1c043e048c677a4ebcd4ca618efcd098729cec2fa5",
        "Created": "2019-03-12T09:58:06.595522099Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 13411,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2019-03-12T09:58:07.12571162Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:881bd08c0b08234bd19136957f15e4301097f4646c1e700f7fea26e41fc40069",
        "ResolvConfPath": "/var/lib/docker/containers/ad7cc45c81ffadbd44254c1c043e048c677a4ebcd4ca618efcd098729cec2fa5/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/ad7cc45c81ffadbd44254c1c043e048c677a4ebcd4ca618efcd098729cec2fa5/hostname",
        "HostsPath": "/var/lib/docker/containers/ad7cc45c81ffadbd44254c1c043e048c677a4ebcd4ca618efcd098729cec2fa5/hosts",
        "LogPath": "/var/lib/docker/containers/ad7cc45c81ffadbd44254c1c043e048c677a4ebcd4ca618efcd098729cec2fa5/ad7cc45c81ffadbd44254c1c043e048c677a4ebcd4ca618efcd098729cec2fa5-json.log",
        "Name": "/myNginx",
        "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": "80"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "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,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "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/bc9ef8c119903b79ec581081d835c447ce00b72eb70b07b45d14b3dde7a37c51-init/diff:/var/lib/docker/overlay2/2f3362aabe1d926214bed5c53a425fde596d23861cc6a59168c594974623876a/diff:/var/lib/docker/overlay2/56da5031c7fb61e617765323e953cd71e07679f202f56df818342500f409b50b/diff:/var/lib/docker/overlay2/1321013bc28d4931f52ab6cc47de24b57c6221686aa25c1acea18c7f87131070/diff",
                "MergedDir": "/var/lib/docker/overlay2/bc9ef8c119903b79ec581081d835c447ce00b72eb70b07b45d14b3dde7a37c51/merged",
                "UpperDir": "/var/lib/docker/overlay2/bc9ef8c119903b79ec581081d835c447ce00b72eb70b07b45d14b3dde7a37c51/diff",
                "WorkDir": "/var/lib/docker/overlay2/bc9ef8c119903b79ec581081d835c447ce00b72eb70b07b45d14b3dde7a37c51/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "ad7cc45c81ff",
            "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.15.9-1~stretch",
                "NJS_VERSION=1.15.9.0.2.8-1~stretch"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "nginx",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "c107fe699256ce2babfe253697e8920386d06d86dae4d91be13c0b6cca00aa08",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "80"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/c107fe699256",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "88fa96a128cc924a9117cae0f720609ed70cad716892da3baf598e7f58a03ecc",
            "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": "43fd51e168684e9ecd3684cf50beff2305983c747083d35e2b8a04d3470d775b",
                    "EndpointID": "88fa96a128cc924a9117cae0f720609ed70cad716892da3baf598e7f58a03ecc",
                    "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
                }
            }
        }
    }
]

7、停止Nginx应用

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
ad7cc45c81ff        nginx               "nginx -g 'daemon of…"   23 seconds ago      Up 23 seconds       0.0.0.0:80->80/tcp   myNginx
[root@localhost ~]# docker stop ad7cc45c81ff
ad7cc45c81ff

8、查看Nginx进程,访问80端口

[root@localhost ~]# ps -ef|grep nginx
root      13319  12350  0 05:50 pts/1    00:00:00 grep --color=auto nginx
[root@localhost ~]# curl http://localhost:80/
curl: (7) Failed connect to localhost:80; Connection refused

9、删除Nginx应用Container(一般需要先停止后才能删除,强制删除使用 docker rm -f xxx)

[root@localhost ~]# docker stop ad7cc45c81ff
ad7cc45c81ff
[root@localhost ~]# docker rm ad7cc45c81ff
ad7cc45c81ff

 10、Docker的Nginx应用映射到主机8899端口

[root@localhost ~]# curl http://localhost:8899/
curl: (7) Failed connect to localhost:8899; Connection refused
[root@localhost ~]# docker run -p 8899:80 --name myNginx -d nginx
598c5f3d52e0511480eebb031c2cdee05b155054edd014ca22ca1bef49d62867
[root@localhost ~]# curl http://localhost:8899/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
         35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
原文地址:https://www.cnblogs.com/wrcold520/p/10518464.html