[13th][Day18] try nginx

使用 docker build 并测试一个 web server

使用 docker 测试静态网站
建立一个资料夹
https://ithelp.ithome.com.tw/upload/images/20210926/20119546yqD7bEyT2v.png

下载 global.conf nginx.conf 或者可以自己写一份

wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/global.conf
wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/nginx.conf

准备 Dockerfile

FROM ubuntu:20.04
MAINTAINER eric "[email protected]"
ENV REFRESHED_AT 2021-09-26
RUN apt-get update
RUN apt-get -y 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

about dockerfile
安装 nginx
建一个资料夹 /var/www/html/website
将刚刚下载的 global.conf nginx.conf 夹到 image 中
expose 80 port

global.conf
https://ithelp.ithome.com.tw/upload/images/20210926/201195469nlWRrBoIS.png

将 nginx 设定为监听 80 port
设定 nginx 导页的 root 路径为 /var/www/html/website 就是刚刚 dockerfile 中 mkdir 的部分
需要把 nginx 设定为 daemon off mode,让 nginx 可以在 container work

nginx/nginx.conf
https://ithelp.ithome.com.tw/upload/images/20210926/201195468NQTgn2TNf.png

daemon off;

nginx 默认情况下会用 daemon 的方式启动,会导致 container 只有短暂运作就结束,daemon off 能让 container 维持 active 的模式

准备完成
https://ithelp.ithome.com.tw/upload/images/20210926/20119546S0czQUPgOT.png

build image

docker build -t trytry/nginx .

https://ithelp.ithome.com.tw/upload/images/20210926/201195463kdverrSvV.png

准备 index.html
https://ithelp.ithome.com.tw/upload/images/20210926/201195462Rxtk5nXPb.png

run 一个 nginx testing container

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

-v VOLUME 挂上本机的资料夹作为 VOLUME mount 到 run 起来的 container 中

注意路径位置 $PWD 会抓取在 terminal 下指令时的目录
https://ithelp.ithome.com.tw/upload/images/20210926/20119546Zx515aIRRE.png

我们有指定要 expose 的 port 但没有指定本机对 container 的 port

docker ps -l

https://ithelp.ithome.com.tw/upload/images/20210926/201195469pD0Q6A1Qn.png

得到这次 docker 帮我配的 port 是 0.0.0.0:60087->80/tcp

直接在本机打网址就能从本机 -> nginx container -> mount 进去的 VOLUME 中的 index.html
https://ithelp.ithome.com.tw/upload/images/20210926/20119546RX8HqCt9wk.png

范例来源:第一本Docker书/James Turnbull


<<:  [Day13] 使用OpenCV & Dlib作人脸侦测需要知道的一些事

>>:  Day12 hover应用(一)

Day 27. 过滤器 - Filter

今天是三天连假的第一天,实体上课真的太累了,一不小心睡到下午诶嘿(๑´ڡ`๑)连假过完铁人也差不多完...

D6 第三周 (回忆篇)

这礼拜进度比较快写到 week4 的作业。看了一下 fetch API 的东西,$.ajax(), ...

centos7没有安装ifconfig命令的解决方法

ifconfig命令是设置或显示网络接口的程序,可以显示出我们机器的网卡信息, 可是有些时候最小化安...

RDS汇入汇出

资料库的另一个重点项目是资料的传递. 这次实作跨资料库的传递. 使用新增Linked Servers...