未加星标

在Docker容器中部署静态网页的方法教程

字体大小 | |
[运维安全 所属分类 运维安全 | 发布者 店小二05 | 时间 | 作者 红领巾 ] 0人收藏点击收藏

前言

一般我们在访问容器时需要通过容器的端口来访问,那如何设置容器的端口映射呢?

我们通过以下命令来设置:

docker run -p ip:hostPort:containerPort [--name] [-i] [-t] 镜像名 [COMMAND][ARG...]
ip:表示宿主机ip
hostPort:宿主机端口号
containerPort:容器端口号

设置的方式有以下几种:

containerPort,指定容器端口号,宿主机端口随机生成

[[email protected] ~]# docker run -p 80 --name web_test -i -t 80864d42dd23 hub.c.163.com/library/ubuntu /bin/bash
hostPort:containerPort映射主机端口和容器端口

[[email protected] ~]# docker run -p 8080:80 --name web_test -i -t 80864d42dd23 hub.c.163.com/library/ubuntu /bin/bash
ip::containerPort设置主机的随机端口到容器端口

[[email protected] ~]# docker run -p 0.0.0.0::80 --name web_test -i -t 80864d42dd23 hub.c.163.com/library/ubuntu /bin/bash
ip:hostPort:containerPort映射指定地址的指定端口到容器的指定端口

[[email protected] ~]# docker run -p 0.0.0.0:8080:80 --name web_test -i -t 80864d42dd23 hub.c.163.com/library/ubuntu /bin/bash
下面通过nginx在容器部署静态网页,通过以下步骤
      - 创建80映射端口的交互式容器
      - 安装nginx
      - 安装文本编辑器vim
      - 创建静态网页
      - 运行nginx
      - 验证网页

示例如下(如果安装完ubuntu后不能安装nginx进行apt-get update):

[[email protected] ~]# docker run -p 80 --name static_test -i -t hub.c.163.com/library/ubuntu /bin/bash
[email protected]:/# apt-get install -y nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package nginx
[email protected]:/# apt-get update
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
**中间日志省略。。。。。。。。。。**
Fetched 24.9 MB in 9s (2717 kB/s)
Reading package lists... Done
[email protected]:/# apt-get install -y nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
**安装日志省略。。。。。。。。。。**

然后安装vim:

[email protected]:/# apt-get install -y vim
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
**安装日志省略。。。。。。。。。。**

然后建立存放静态文件的目录,并创建一个简单的html文件:

[email protected]:/# mkdir -p /var/www/html
[email protected]:/#
[email protected]:/# cd /var/www/html/
[email protected]:/var/www/html# vim index.html
[email protected]:/var/www/html# ll
total 8
drwxr-xr-x. 2 root root 53 Mar 13 05:02 ./
drwxr-xr-x. 3 root root 17 Mar 13 04:50 ../
-rw-r--r--. 1 root root 79 Mar 13 05:02 index.html
-rw-r--r--. 1 root root 612 Mar 13 04:51 index.nginx-debian.html
[email protected]:/var/www/html# cat index.html
<html>
<body>
<h1>
this is the first docker static file
</h1>
</body>
</html>
[email protected]:/var/www/html#

找到nginx的安装目录,更改default的root值为刚才创建的目录:

[email protected]:/var/www/html# whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx
[email protected]:/var/www/html# cd /etc/nginx/sites-enabled/
[email protected]:/etc/nginx/sites-enabled# ll
total 4
drwxr-xr-x. 2 root root 20 Mar 13 04:51 ./
drwxr-xr-x. 6 root root 4096 Mar 13 04:51 ../
lrwxrwxrwx. 1 root root 34 Mar 13 04:51 default -> /etc/nginx/sites-available/default
[email protected]:/etc/nginx/sites-enabled# vim default
在Docker容器中部署静态网页的方法教程

然后启动nginx,查看容器进程:

[email protected]:/etc/nginx/sites-enabled# nginx
[email protected]:/etc/nginx/sites-enabled# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 04:48 ? 00:00:00 /bin/bash
root 827 1 0 05:06 ? 00:00:00 nginx: master process nginx
www-data 828 827 0 05:06 ? 00:00:00 nginx: worker process
www-data 829 827 0 05:06 ? 00:00:00 nginx: worker process
www-data 830 827 0 05:06 ? 00:00:00 nginx: worker process
www-data 831 827 0 05:06 ? 00:00:00 nginx: worker process
www-data 832 827 0 05:06 ? 00:00:00 nginx: worker process
www-data 833 827 0 05:06 ? 00:00:00 nginx: worker process
www-data 834 827 0 05:06 ? 00:00:00 nginx: worker process
www-data 835 827 0 05:06 ? 00:00:00 nginx: worker process
root 836 1 0 05:07 ? 00:00:00 ps -ef
[email protected]:/etc/nginx/sites-enabled#
然后按ctrl+p,ctrl+q退出交互式容器。

使用Docker ps查看容器的端口和运行情况,也可以通过dcoker port 镜像名查看:

[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25fcbf6e953d hub.c.163.com/library/ubuntu "/bin/bash" 18 minutes ago Up 18 minutes 0.0.0.0:32772->80/tcp static_test
[[email protected] ~]# docker port static_test
80/tcp -> 0.0.0.0:32772
[[email protected] ~]#
可以看到容器的80端口映射到注解的32772端口。

通过curl访问创建的index.html文件:

[[email protected] ~]# curl http://127.0.0.1:32772/index.html
<html>
<body>
<h1>
this is the first docker static file
</h1>
</body>
</html>
[[email protected] ~]#
也可以通过docker inspect 容器名来查看容器的详细信息和ip

通过容器ip进行访问:

[[email protected] ~]# docker inspect static_test
其他信息省略
"IPAddress": "172.17.0.5",
[[email protected] ~]# curl http://172.17.0.5/index.html
<html>
<body>
<h1>
this is the first docker static file
</h1>
</body>
</html>
[[email protected] ~]#

当我们使用docker stop 容器名停止这个容器后,再使用docker start 容器名启动容器后,里面的nginx并没有启动。那么我们使用docker exec 容器名 nginx来启动:

[[email protected] ~]# docker stop static_test
static_test
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4f32bbe4a34 hub.c.163.com/library/ubuntu "/bin/bash" 41 hours ago Up 41 hours loving_brattain
d75a2d8c7822 xingguo/df_test1 "nginx -g 'daemon off" 2 days ago Up 2 days 0.0.0.0:32770->80/tcp df_nginx_web
959c0fc5d903 xingguo/commit_test1 "nginx -g 'daemon off" 2 days ago Up 2 days 0.0.0.0:32769->80/tcp nginx_test
[[email protected] ~]# docker start static_test
static_test
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25fcbf6e953d hub.c.163.com/library/ubuntu "/bin/bash" 29 minutes ago Up 2 seconds 0.0.0.0:32773->80/tcp static_test
b4f32bbe4a34 hub.c.163.com/library/ubuntu "/bin/bash" 41 hours ago Up 41 hours loving_brattain
d75a2d8c7822 xingguo/df_test1 "nginx -g 'daemon off" 2 days ago Up 2 days 0.0.0.0:32770->80/tcp df_nginx_web
959c0fc5d903 xingguo/commit_test1 "nginx -g 'daemon off" 2 days ago Up 2 days 0.0.0.0:32769->80/tcp nginx_test
[[email protected] ~]# docker top static_test
UID PID PPID C STIME TTY TIME CMD
root 4719 4702 0 13:17 pts/2 00:00:00 /bin/bash
[[email protected] ~]# docker exec static_test nginx
[[email protected] ~]# docker top static_test
UID PID PPID C STIME TTY TIME CMD
root 4719 4702 0 13:17 pts/2 00:00:00 /bin/bash
root 4800 1 0 13:18 ? 00:00:00 nginx: master process nginx
amandab+ 4801 4800 0 13:18 ? 00:00:00 nginx: worker process
amandab+ 4802 4800 0 13:18 ? 00:00:00 nginx: worker process
amandab+ 4803 4800 0 13:18 ? 00:00:00 nginx: worker process
amandab+ 4804 4800 0 13:18 ? 00:00:00 nginx: worker process
amandab+ 4805 4800 0 13:18 ? 00:00:00 nginx: worker process
amandab+ 4806 4800 0 13:18 ? 00:00:00 nginx: worker process
amandab+ 4807 4800 0 13:18 ? 00:00:00 nginx: worker process
amandab+ 4808 4800 0 13:18 ? 00:00:00 nginx: worker process
[[email protected] ~]#

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

本文运维安全相关术语:linux服务器代维 linux服务器搭建 运维管理 运维工程师 企业安全文章 企业安全管理 cf安全系统检测到游戏数据异常

主题: DockerTI谢大
分页:12
转载请注明
本文标题:在Docker容器中部署静态网页的方法教程
本站链接:http://www.codesec.net/view/557031.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 运维安全 | 评论(0) | 阅读(30)