Docker06-静态网站实战

本文档详细介绍了如何使用Dockerfile从ubuntu:14.04基础镜像构建Nginx镜像,并部署静态网站。首先,准备上下文目录和Nginx配置,接着编写Dockerfile,安装Nginx并设置静态页面路径。然后,构建镜像,启动容器,并将宿主机目录挂载为卷以实现页面内容的更新。通过访问宿主机的特定端口,可以查看和验证静态网站及其内容的变化。
摘要由CSDN通过智能技术生成

Docker06-静态网站实战

使用nginx搭建一个静态网站simple。

上下文准备

准备构建上下文目录,准备资源(ng配置)。

mkdir sample && cd sample
mkdir nginx && cd nginx
wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/global.conf
wget http://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/nginx.conf
cd ..
touch Dockerfile

目录结构如下:

➜  sample tree
.
|____Dockerfile
|____nginx
| |____global.conf
| |____nginx.conf

编写Dockerfile文件:

➜  sample more Dockerfile 
FROM ubuntu:14.04
MAINTAINER laozhou laozhou@example.com
ENV REFRESHED_AT 2019-10-20
RUN apt-get -yqq update && apt-get -yqq install nginx
RUN mkdir -p /var/www/html/website
ADD nginx/global.conf /etc/nginx/conf.d/
ADD nginx/nginx.conf /etc/nginx/nginx.conf
EXPOSE 80

从基础镜像ubuntu:14.04开始构建,安装nginx,创建/var/www/html/website目录,将构建上下文中的ng配置加入到容器中对应的地方,最后暴露容器的80端口。

ng的配置很简单,可以到github上自行查看。

构建镜像

接下来就可以构建我们自己的nginx镜像了:

docker build -t zhouyinyan/nginx .

输出(安装ng的日志忽略):

Sending build context to Docker daemon  4.608kB
Step 1/8 : FROM ubuntu:14.04
 ---> 2c5e00d77a67
Step 2/8 : MAINTAINER laozhou laozhou@example.com
 ---> Running in 662cd05a2fe7
 ---> c5bb61b8a0c9
Removing intermediate container 662cd05a2fe7
Step 3/8 : ENV REFRESHED_AT 2019-10-20
 ---> Running in 716358b71f7e
 ---> 6bea342012c0
Removing intermediate container 716358b71f7e
Step 4/8 : RUN apt-get -yqq update && apt-get -yqq install nginx
 ---> Running in c104edff89cb
debconf: unable to initialize frontend: Dialog
......
Processing triggers for libc-bin (2.19-0ubuntu6.15) ...
Processing triggers for sgml-base (1.26+nmu4ubuntu1) ...
 ---> 68ea870a53f7
Removing intermediate container c104edff89cb
Step 5/8 : RUN mkdir -p /var/www/html/website
 ---> Running in e9daacce272b
 ---> 8db00e21a444
Removing intermediate container e9daacce272b
Step 6/8 : ADD nginx/global.conf /etc/nginx/conf.d/
 ---> 88ca203634da
Step 7/8 : ADD nginx/nginx.conf /etc/nginx/nginx.conf
 ---> 74715c06de40
Step 8/8 : EXPOSE 80
 ---> Running in 82363a1be193
 ---> a9d62e9f8538
Removing intermediate container 82363a1be193
Successfully built a9d62e9f8538
Successfully tagged zhouyinyan/nginx:latest

可以看到构建镜像的每一层的过程。

我们有可以使用docker history命令来查看每一层运行的命令:

docker history zhouyinyan/nginx

准备静态页面

准备下静态网站的页面(一个index.html页面):

mkdir website && cd website
wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/website/index.html

启动静态网站

接下来,我们来用刚构建的ng镜像来启动一个nginx容器:

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

-t: 将宿主机的目录作为卷,挂载到容器中,卷在docker中非常重要,它为docker提供持久化数据或者共享数据的能力。

通过docker ps查看容器的80端口映射的宿主机的哪个端口?

➜  sample docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES
fe823e3e8312        zhouyinyan/nginx    "nginx"             4 seconds ago       Up 4 seconds        0.0.0.0:32768->80/tcp   website

可访问宿主机的32768端口,来获取index.html页面

➜  sample curl http://localhost:32768/index.html
<head>

<title>Test website</title>

</head>

<body>

<h1>This is a test website</h1>

</body>

修改页面

现在,我们来修改下页面的内容:

vim $PWD/website/index.html 

将第9行的<h1>This is a test website</h1>修改为<h1>This is a test website for docker</h1>,再次访问,页面内容发生变更了。

➜  sample curl http://localhost:32768/index.html
<head>

<title>Test website</title>

</head>

<body>

<h1>This is a test website for docker</h1>

</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值