從零開始學習docker(四)host

需求

    創建一個網站,但願這個網站能被別人訪問到。若是咱們在本地建立一個Nginx服務器。html

iie4bu@hostdocker:~$ docker run --name web -d nginx
d6edf032ce6ee4126124b89d30c6d11793a120865a004abb64fe20fad2e3a43b
iie4bu@hostdocker:~$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
d6edf032ce6e        nginx               "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes        80/tcp              web

可是咱們的Nginx的服務目前是訪問不了的。只能經過docker exec -it web /bin/bash來訪問。nginx

所以須要把Nginx服務暴露到外面。web

咱們知道這個Nginx容器是一個獨立的network namespace,有ip地址,先查看他的網絡狀況docker

iie4bu@hostdocker:~$ docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "b7c11f829aacbfe6578b556865d2bbd6d2276442cb58099ae2edb7167b85b365",
        "Created": "2019-06-26T09:52:55.529849773+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "d6edf032ce6ee4126124b89d30c6d11793a120865a004abb64fe20fad2e3a43b": {
                "Name": "web",
                "EndpointID": "d03b95f55185e75908cf04fd3075a5cc4d91fa1df214643b2013d7964f19fb0c",
                "MacAddress": "02:42:ac:11:00:04",
                "IPv4Address": "172.17.0.4/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

Nginx默認是連到bridge上面的。能夠看到ip是172.17.0.4bash

在外面是能夠ping到這個ip的:服務器

iie4bu@hostdocker:~$ ping 172.17.0.4
PING 172.17.0.4 (172.17.0.4) 56(84) bytes of data.
64 bytes from 172.17.0.4: icmp_seq=1 ttl=64 time=0.624 ms
64 bytes from 172.17.0.4: icmp_seq=2 ttl=64 time=0.052 ms
64 bytes from 172.17.0.4: icmp_seq=3 ttl=64 time=0.048 ms
^C
--- 172.17.0.4 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.048/0.241/0.624/0.270 ms
iie4bu@hostdocker:~$

由於他是連到咱們的docker0上面的。網絡

iie4bu@hostdocker:~$ telnet 172.17.0.4 80
Trying 172.17.0.4...
Connected to 172.17.0.4.
Escape character is '^]'.

說明是能訪問的。curl

使用curl訪問:tcp

iie4bu@hostdocker:~$ curl http://172.17.0.4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 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>

curl成功訪問。網站

咱們但願外面也能夠訪問。

目前Nginx端口只綁定到了172.17.0.4的網絡空間。若是把端口映射到本地的服務器上80端口,是否是就能夠了。

如何把Nginx的80端口映射到本地?

先將Nginx中止:docker container stop web而且刪除docker container rm web

重新添加一個Nginx container,並添加參數-p,將容器裏面的80端口,映射到本地的80端口

iie4bu@hostdocker:~$ docker run --name web -d -p 80:80 nginx
94f9176f55a6ea9180d571bbb2f13319f9b5024bc4e4aae8ff28d0a92394cc79
iie4bu@hostdocker:~$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
94f9176f55a6        nginx               "nginx -g 'daemon of…"   18 minutes ago      Up 18 minutes       0.0.0.0:80->80/tcp   web

這樣能夠經過curl 127.0.0.1訪問Nginx了。

iie4bu@hostdocker:~$ curl http://127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 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>

經過本地的80端口就能夠訪問容器中的80端口的服務了。

相關文章
相關標籤/搜索