Docker-容器操作练习之---端口映射

今天做一个有趣的实验基于端口转发:
实验内容:通过宿主机IP地址访问容器上的网站。
实验原理:将容器上的端口映射到宿主机上。
实验步骤如下:

首先创建一个容器查看ID.
[root@localhost ~]# docker run -it nginx /bin/bash
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
33564738cf50        nginx               "/bin/bash"         3 minutes ago       Up 3 minutes        80/tcp              jolly_northcutt
[root@localhost ~]# docker inspect 3356 |grep -i ipaddr    #不区分大小写查找出ip
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",
[root@localhost ~]# curl 172.17.0.3                 #尝试连接,很显然连接不上,进入容器查看服务状态(怀疑服务没有启动)
curl: (7) Failed connect to 172.17.0.3:80; Connection refused

[root@localhost ~]# docker exec -it 3356 /bin/bash   
root@33564738cf50:/# lsof                           #进入容器发现没有lsof命令,好简单怎办办??怎么查看nginx状态???
bash: lsof: command not found
root@33564738cf50:/# ss
bash: ss: command not found
root@33564738cf50:/# netstat
bash: netstat: command not found
root@33564738cf50:/# 

root@33564738cf50:/# /etc/init.d/nginx status    #使用最原始的方法,这里有各种服务的启动脚本
[FAIL] nginx is not running ... failed!          #发现状态时fail
root@33564738cf50:/# /etc/init.d/nginx start     #ok,现在把nginx服务启动起来,并退出容器ctrl+p+q

[root@localhost ~]# curl 172.17.0.3              #可以看见访问成功了
<!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>
[root@localhost ~]# 
[root@localhost ~]# ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.391 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.046 ms

#现在出现了一个问题,这个容器用宿主机能ping通,但是别人ping不通,那别人怎么访问呢?
#这时就要使用到端口映射了,你只需要加一个选项,而且你不用自己设置防火墙规则,docker引擎会自己进行防火墙配置(是不是很方便!!!)如下:

[root@localhost ~]# docker run -d -p 83:80 --name "web01" -it nginx /bin/bash   #当访问宿主机的81端口时转发到容器的80端口
2a03d7039f6643ead073cd6a063972115f1f757e54a78749ccb629da044547a9

[root@localhost ~]# docker ps |grep 2a03                #运行起来了
2a03d7039f66        nginx               "/bin/bash"         17 seconds ago      Up 16 seconds       0.0.0.0:81->80/tcp   web01

[root@localhost ~]# ip a                        #查看本机IP
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:61:17:0f brd ff:ff:ff:ff:ff:ff
    inet 192.168.26.100/24 brd 192.168.26.255 scope global ens33
    
[root@localhost ~]# curl 192.168.26.100:83        #通过83端口成功访问
<!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>
[root@localhost ~]# 

****************************************************
此处接下一篇
****************************************************



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值