下來是將Nginx安裝到容器來架構(gòu)一個(gè)簡(jiǎn)單的網(wǎng)站開發(fā),網(wǎng)站命名為Sample.
1.samle網(wǎng)站的初試Dockerfile
- 為Nginx Dockerfile創(chuàng)建一個(gè)目錄
$ mkdir sample
$ cd sample
$ touch Dockerfile
- 獲取Nginx配置文件
$ cd sample
$ mkdir nginx && cd nginx
$ wget https://github.com/hiekay/dockerbook-code/blob/master/code/5/sample/nginx/global.conf
$ wget https://github.com/hiekay/dockerbook-code/blob/master/code/5/sample/nginx/nginx.conf
$ cd ..
- 網(wǎng)站測(cè)試的基本Dockerfile
FROM ubuntu:18.04
MAINTAINER hiekay "hikay5230@gmail.com"
ENV REFRESHED_AT 2018-11-2
RUN apt-get update
RUN apt-get -y -q install nginx
RUN mkdir -p /var/www/html
ADD nginx/global.conf /etc/nginx/conf.d/
ADD nginx/nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
- 解析: dockerfile 包括:
安裝Nginx;
在容器中創(chuàng)建一個(gè)目錄/var/www/html;
將來自本地文件的Nginx配置文件添加到鏡像中;
公開鏡像80端口. - global.conf文件
server {
listen 0.0.0.0:80;
server_name _;
root /var/www/html/website;
index index.html index.htm;
access_log /var/log/nginx/default_access.log;
error_log /var/log/nginx/default_error.log;
}
網(wǎng)站根目錄: root /var/www/html/website;
- nginx.conf配置文件
user www-data;
worker_processes 4;
pid /run/nginx.pid;
daemon off;
events { }
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
}
2.構(gòu)建Sample網(wǎng)站和Nginx鏡像
- 構(gòu)建新的Nginx鏡像
$ sudo docker build -t hiekay/nginx .
- 展示Nginx鏡像的構(gòu)建歷史
$ sudo docker history hiekay/nginx
//返回
IMAGE CREATED CREATED BY SIZE COMMENT
c2f93099a450 About a minute ago /bin/sh -c #(nop) EXPOSE 80 0B
79e5c47816c2 About a minute ago /bin/sh -c #(nop) ADD file:ae1b9b338d442b9bc… 415B
4e375527ddd7 About a minute ago /bin/sh -c #(nop) ADD file:e570a7cbab1d98b88… 286B
16ef3bfd3dcd About a minute ago /bin/sh -c mkdir -p /var/www/html 0B
41f4aad7a454 2 minutes ago /bin/sh -c apt-get -y -q install nginx 59.7MB
1a50f2d65b09 2 minutes ago /bin/sh -c apt-get update 23.8MB
d02ebf320b6c 3 minutes ago /bin/sh -c #(nop) ENV REFRESHED_AT=2018-11-2 0B
a1eda1cd7dfe 27 hours ago /bin/sh -c #(nop) MAINTAINER hiekay "hikay5… 0B
ea4c82dcd15a 2 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 2 weeks ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 2 weeks ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0B
<missing> 2 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 745B
<missing> 2 weeks ago /bin/sh -c #(nop) ADD file:bcd068f67af2788db… 85.8MB
3.從Sample網(wǎng)站和Nginx鏡像構(gòu)建容器
- 下載Sample網(wǎng)站
$ cd sample
$ mkdir website && cd website
$ wget https://github.com/hiekay/dockerbook-code/blob/master/code/5/sample/website/index.html
$ cd ..
其中 index.html 內(nèi)容:
<head>
<title>Test website</title>
</head>
<body>
<h1>This is a test website</h1>
</body>
- 構(gòu)建一個(gè)Nginx測(cè)試容器
$ sudo docker run -d -p 80 --name website -v $PWD/website:/var/www/html/website hiekay/nginx nginx
其中 -v 是指定卷的位置
卷的使用場(chǎng)景:
希望同事對(duì)代碼做開發(fā)和測(cè)試;
代碼改動(dòng)很頻繁,不想在開發(fā)過程中重構(gòu)鏡像;
希望在多個(gè)容器間共享代碼.
- 控制卷的寫狀態(tài):
rw : 讀寫
ro : 只讀
$ sudo docker run -d -p 80 --name website -v $PWD/website:/var/www.html/website:ro hiekay/nginx nginx
- 查看Sample網(wǎng)站容器
$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dc1a45694b4b hiekay/nginx "nginx" 4 minutes ago Up 4 minutes 0.0.0.0:32769->80/tcp website
在宿主機(jī)上瀏覽32769端口,看到網(wǎng)站如下:
4.修改網(wǎng)站
- 修改sample網(wǎng)站
$ vi $PWD/website/index.html
將內(nèi)容換成:
This is a test website for docker .
刷新瀏覽器 , 如下 :
可以看到網(wǎng)站已經(jīng)更新了.
總結(jié):可以實(shí)現(xiàn)正在測(cè)試的網(wǎng)站的運(yùn)行環(huán)境,完全是生產(chǎn)環(huán)境里的真實(shí)狀態(tài).
可以給每個(gè)用于生產(chǎn)的網(wǎng)站服務(wù)環(huán)境配置一個(gè)容器,給不同的開發(fā)框架的運(yùn)行環(huán)境配置一個(gè)容器,或者給后端數(shù)據(jù)庫(kù)配置一個(gè)容器.等.注意 可以進(jìn)容器內(nèi)查看
$ sudo docker exec -t -i 容器ID /bin/bash