前言

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

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

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

设置的方式有以下几种:

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

[root@localhost ~]# docker run -p 80 --name web_test -i -t 80864d42dd23  hub.c.163.com/library/ubuntu /bin/bash

hostPort:containerPort映射主机端口和容器端口

[root@localhost ~]# docker run -p 8080:80 --name web_test -i -t 80864d42dd23  hub.c.163.com/library/ubuntu /bin/bash

ip::containerPort设置主机的随机端口到容器端口

[root@localhost ~]# 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映射指定地址的指定端口到容器的指定端口

[root@localhost ~]# 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):

[root@localhost ~]# docker run -p 80 --name static_test -i -t hub.c.163.com/library/ubuntu /bin/bash
root@25fcbf6e953d:/# apt-get install -y nginx
Reading package lists... Done
Building dependency tree  
Reading state information... Done
E: Unable to locate package nginx
root@25fcbf6e953d:/# 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
root@25fcbf6e953d:/# apt-get install -y nginx
Reading package lists... Done
Building dependency tree  
Reading state information... Done
**安装日志省略。。。。。。。。。。**

然后安装vim:

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

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

root@25fcbf6e953d:/# mkdir -p /var/www/html
root@25fcbf6e953d:/# 
root@25fcbf6e953d:/# cd /var/www/html/
root@25fcbf6e953d:/var/www/html# vim index.html
root@25fcbf6e953d:/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
root@25fcbf6e953d:/var/www/html# cat index.html 
<html>
<body>
<h1>
  this is the first docker static file
</h1>
</body>
</html>
root@25fcbf6e953d:/var/www/html# 

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

root@25fcbf6e953d:/var/www/html# whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx
root@25fcbf6e953d:/var/www/html# cd /etc/nginx/sites-enabled/
root@25fcbf6e953d:/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
root@25fcbf6e953d:/etc/nginx/sites-enabled# vim default 

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

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

root@25fcbf6e953d:/etc/nginx/sites-enabled# nginx
root@25fcbf6e953d:/etc/nginx/sites-enabled# ps -ef 
UID  PID PPID C STIME TTY   TIME CMD
root   1  0 0 04:48 "htmlcode">
root@localhost ~]# 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
[root@localhost ~]# docker port static_test
80/tcp -> 0.0.0.0:32772
[root@localhost ~]# 

可以看到容器的80端口映射到注解的32772端口。

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

[root@localhost ~]# curl http://127.0.0.1:32772/index.html
<html>
<body>
<h1>
  this is the first docker static file
</h1>
</body>
</html>
[root@localhost ~]#

也可以通过docker inspect 容器名来查看容器的详细信息和ip

通过容器ip进行访问:

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

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

[root@localhost ~]# docker stop static_test
static_test
[root@localhost ~]# 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
[root@localhost ~]# docker start static_test
static_test
[root@localhost ~]# 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
[root@localhost ~]# 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@localhost ~]# docker exec static_test nginx
[root@localhost ~]# 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    "color: #ff0000">总结

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

标签:
dockerfile,静态网页,docker容器部署,docker容器

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

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

暂无“在Docker容器中部署静态网页的方法教程”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。