1.9 Docker 网络管理-外部访问容器

2018-05-21

1.9 Docker 网络管理-外部访问容器

  首先使用 Ubuntu 镜像新建一个容器,然后在该容器中安装 httpd 服务,并启动

root@ubuntu:~# docker run -itd 45d bash

fb08605195ef3d8d8ebd909ca0ddf3bc390ee87528d5c9c5d35c8f4abf5262d8

root@ubuntu:~# docker exec -it fb08 bash

root@fb08605195ef:/# apt-get install apache2

root@fb08605195ef:/# ps aux|grep apa

root       822  0.0  0.2  71560  2624 ?        Ss   06:01   0:00 /usr/sbin/apache2 -k start

www-data   825  0.0  0.4 360716  4248 ?        Sl   06:01   0:00 /usr/sbin/apache2 -k start

www-data   826  0.0  0.4 360716  4248 ?        Sl   06:01   0:00 /usr/sbin/apache2 -k start

root       896  0.0  0.0  11276   728 ?        S+   07:20   0:00 grep --color=auto apa

root@fb08605195ef:/# netstat -lnp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name

tcp6       0      0 :::80                   :::*                    LISTEN      822/apache2

Active UNIX domain sockets (only servers)

Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path

  已经监听 80 端口,但外部访问不了

  基于当前的容器创建镜像

root@ubuntu:~# docker commit -m "add_apache2" -a "zhanjie.jin" fb0 ubuntu:apache2

eb11cb23405be922e785eee96a26e0e5801fd5dce7a8e1f4f28cb89be3994c05

root@ubuntu:~# docker images

REPOSITORY                  TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

ubuntu                      apache2             eb11cb23405b        8 seconds ago       266.8 MB

ubuntu                      v2                  7ccdacdace59        29 hours ago        110.3 MB

ubuntu_v2                   latest              7ccdacdace59        29 hours ago        110.3 MB

  使用该镜像创建容器,同时指定端口映射

root@ubuntu:~# docker run -itd -p 5123:80 ubuntu:apache2 bash

56d517148dcf6b91a0d36a2e75bf04b0dd912e4b4e3d865416fa31dbf5289383

  进入容器,查看一下

root@ubuntu:~# docker exec -it 56d bash

root@56d517148dcf:/# ps aux|grep apa

root        23  0.0  0.0  11276   728 ?        S+   07:36   0:00 grep --color=auto apa

  并没有 apache2 进程,启动它

root@56d517148dcf:/# /etc/init.d/apache2 start

 * Starting Apache httpd web server apache2                                                                                                   AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 192.168.42.9. Set the 'ServerName' directive globally to suppress this message

 *

root@56d517148dcf:/# ps aux|grep apa

root        46  0.0  0.2  71560  2696 ?        Ss   07:37   0:00 /usr/sbin/apache2 -k start

www-data    49  0.0  0.4 360716  4236 ?        Sl   07:37   0:00 /usr/sbin/apache2 -k start

www-data    50  0.0  0.4 360716  4236 ?        Sl   07:37   0:00 /usr/sbin/apache2 -k start

root       110  0.0  0.0  11276   724 ?        S+   07:38   0:00 grep --color=auto apa

  编辑 1.html 文件,随便写点东西

root@56d517148dcf:/# vim /var/www/html/1.html

test_output

  测试

root@ubuntu:~# curl 127.0.0.1:5123/1.html

test_output

  在指定端口映射时,-p 后面也支持 IP:port:ip:port 的格式,比如:-p 127.0.0.1:8080:80,也可以不写本地的端口,只写 ip,这样会随意分配一个端口,-p 127.0.0.1::80


标题:1.9 Docker 网络管理-外部访问容器
作者:散宜生
地址:https://17kblog.com/articles/2018/05/21/1526876633832.html