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

2020-06-17 07:06:41易采站长站整理

前言

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

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


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: