无法访问docker容器架设的服务器,需要怎样调试。

1、
首先建立基础的nginx服务器。

docker run -d -p 1234:80 nginx:latest 
docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6afe2e74773a nginx:latest "nginx -g 'daemon ..." 6 seconds ago Up 5 seconds 0.0.0.0:1234->80/tcp goofy_goldstine


其中1234就是绑定的外部的端口号。

接着,执行

curl 127.0.0.1:1234

得到结果:

<!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>

则说明nginx是正常的。

2、接着执行挂载卷的命令

sudo docker run -d -p 80 --name website11 -v $PWD/website:/var/www/html/website jamtur01/nginx nginx

sudo docker ps后可以看到

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46c490d5f8d4 jamtur01/nginx "nginx" 12 seconds ago Up 11 seconds 0.0.0.0:32773->80/tcp website11
于是执行

curl 127.0.0.1:32773

返回:

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>

这说明错了。错在哪里呢?

3、

执行

docker inspect --format='{{.Mounts}}' website11 

查看绑定的信息

[{bind /home/user/sample/website/website /var/www/html/website true }]

这时候,发现本地目录/home/user/sample/website/website不正确。
于是切换到正确的位置,重新执行docker run 的名称。

再次执行curl,成功。









posted @ 2017-10-27 16:26  北里闻箫  阅读(323)  评论(0编辑  收藏  举报