在容器中部署静态网站

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在容器中部署静态网站相关的知识,希望对你有一定的参考价值。

设置容器的端口映射

通过run命令的两个选项来实现这个功能

run-P】【-p

-P(大写的P将为容器暴露的所有端口进行映射)(随机映射)

-P--publish-all=true|false默认为false

       dockerrun-P -i-tubuntu/bin/bash

-p(小写的p能够指定映射容器哪些端口)

-p--publish=[]

指定端口有四种情况

containerPort

      docker run -p 80 -I -tubuntu /bin/bash

 

hostPort:containerPort

      docker run -p8080:80 -I -t ubuntu /bin/bash

ip::containerPort

       docker run -p0.0.0.0:80 -I -t ubuntu /bin/bash

ip:hostPort:containerPort

       docker run -p0.0.0.0:8080:80 -I -t ubuntu /bin/bash

 

nginx部署流程:

创建映射80端口的交互式容器

安装Nginx

安装文本编辑器vim

创建静态页面

修改Nginx配置文件

运行Nginx

验证网站访问

 

[email protected]:~$docker run -p 80 --name web -i -t ubuntu /bin/bash

[email protected]:/# apt-get intsall -y nginx#安装nginx

[email protected]:/var/www/html#apt-get install -y vim#安装vim

[email protected]:/#mkdir -p /var/www/html

[email protected]:/# cd/var/www/html/

[email protected]:/var/www/html#

[email protected]:/var/www/html#cat index.html #编辑一个静态页面

<html>

<head>

       <title>Nginx inDocker</title>

</head>

<body>

      <h1>Hello,I‘m website in Docker!</h1>

</body>

</html>

 

查看下nginx安装在哪里

[email protected]:/var/www/html#whereis nginx

nginx: /usr/sbin/nginx/etc/nginx /usr/share/nginx /usr/share/man/man1/nginx.1.gz

 vim /etc/nginx/sites-enabled/default#打开default文件

将root的值改为我们刚刚建立的静态网站的位置

[email protected]:/var/www/html#cat  /etc/nginx/sites-enabled/default|grep root

        root /var/www/html;

        #      root /usr/share/nginx/html;

        # deny access to .htaccess files, ifApache‘s document root

#       root html;

#       root html;

[email protected]:/var/www/html#cd /

[email protected]:/#

[email protected]:~#ps -ef|grep nginx#查看进程

root     27163 13952  0 Apr22 ?        00:00:00 nginx: master process/usr/sbin/nginx

www-data 2716427163  0 Apr22 ?        00:00:05 nginx: worker process

www-data 2716527163  0 Apr22 ?        00:00:00 nginx: worker process

www-data 2716627163  0 Apr22 ?        00:00:05 nginx: worker process

www-data 2716727163  0 Apr22 ?        00:00:04 nginx: worker process

root     31545 31522  0 04:44 pts/0    00:00:00 grep --color=auto nginx

[email protected]:~#

可以用psport查看端口映射的情况

[email protected]:~$docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES

0cc8c10d217a        ubuntu:latest       "/bin/bash"         2 days ago          Up 2 days           0.0.0.0:32769->80/tcp   web                

[email protected]:~$docker port web

80/tcp ->0.0.0.0:32769

[email protected]:~$ docker top web#可以查看容器中进程运行的情况

UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD

root                13952               7138                0                   Apr22               pts/1               00:00:00            /bin/bash

[email protected]:~$ curl http://127.0.0.1:32769#访问

<!DOCTYPE html>

<html>

<head>

<title>Welcome tonginx!</title>

<style>

    body {

        width: 35em;

        margin: 0 auto;

        font-family: Tahoma, Verdana, Arial,sans-serif;

    }

</style>

</head>

<body>

<h1>Welcome tonginx!</h1>

<p>If you seethis page, the nginx web server is successfully installed and

working. Furtherconfiguration is required.</p>

 

<p>For onlinedocumentation and support please refer to

<ahref="http://nginx.org/">nginx.org</a>.<br/>

Commercial support isavailable at

<ahref="http://nginx.com/">nginx.com</a>.</p>

 

<p><em>Thankyou for using nginx.</em></p>

</body>

</html>

也可以用容器的ip地址来访问

[email protected]:~$docker inspect web|grep IPAddress

        "IPAddress":"172.17.0.7",

[email protected]:~$

[email protected]:~$curl http://172.17.0.7

<!DOCTYPE html>

<html>

<head>

<title>Welcome tonginx!</title>

<style>

    body {

        width: 35em;

        margin: 0 auto;

        font-family: Tahoma, Verdana, Arial,sans-serif;

    }

</style>

</head>

<body>

<h1>Welcome tonginx!</h1>

<p>If you seethis page, the nginx web server is successfully installed and

working. Furtherconfiguration is required.</p>

 

<p>For onlinedocumentation and support please refer to

<ahref="http://nginx.org/">nginx.org</a>.<br/>

Commercial support isavailable at

<ahref="http://nginx.com/">nginx.com</a>.</p>

 

<p><em>Thankyou for using nginx.</em></p>

</body>

</html>

 

[email protected]:~$ docker stop web#停掉容器

 

web

[email protected]:~$

[email protected]:~$ docker start -i web#启动容器

[email protected]:/# ps -ef#重新启动后并没有开启nginx

UID        PID PPID  C STIME TTY          TIME CMD

root         1    0 16 17:17 ?        00:00:02/bin/bash

root        11    1 26 17:17 ?        00:00:00 ps-ef

ctrl+pctrl+q推出,这个时候就可以用exec命令来启动nginx

[email protected]:~$ docker exec web nginx#再次启动nginx

[email protected]:~$docker top web

UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD

root                32349               7138                1                   01:17               pts/0               00:00:02            /bin/bash

root                32396               32349               0                   01:19               ?                   00:00:00            nginx: master process nginx

www-data            32397               32396               0                   01:19               ?                   00:00:00            nginx: worker process

这时候再用原来的IP地址来访问,就访问失败了

 

[email protected]:~$curl http://172.17.0.7

curl: (7) Failed toconnect to 172.17.0.7 port 80: No route to host

[email protected]:~$docker inspect web|grep -E "IPAddress|HostPort"

                    "HostPort":""

        "IPAddress":"172.17.0.8",

                    "HostPort":"32770"

#发现容器的IP地址和端口已经改变了(当我们重新开启时会发生改变)


本文出自 “11364450” 博客,请务必保留此出处http://11374450.blog.51cto.com/11364450/1918703

以上是关于在容器中部署静态网站的主要内容,如果未能解决你的问题,请参考以下文章

在容器中部署静态网站

Docker 容器部署Apache静态网站

Docker学习笔记(4-3)Docker容器内部署静态网站

如何在Github上免费部署静态网站

1分钟部署一个属于自己的网站,借助云开发静态网站部署属于自己的网站,部署vue静态网站

使用Apache服务部署静态网站