背景
公司項目部署方式為較為傳統的發布方式,通過jenkins 發布到服務器,然后通過 lsyncd進行文件同步到負載均衡的發布環境中。最近為了更好的編排服務器,計划所有項目進行容器化調整。
解決思路及實施過程
1)服務器安裝docker(該過程網上均能學習,這里就不詳細描述了)
2)創建測試文件index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>測試頁面</title> </head> <body> 前端靜態頁面構建docker鏡像過程測試頁面。標注下公司,杭州瑞懿科技有限公司 2018-10-27 </body> </html>
3)創建nginx.conf 替換原鏡像中的配置文件,並將對應的端口設置到9099
# fangt add for web server
# 2018-10-27
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile on;
tcp_nodelay on;
keepalive_timeout 30;
server {
listen 9099;
server_name localhost;
root /usr/share/nginx/dist;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
location / {
try_files $uri $uri/ =404;
index index.html index.htm;
}
}
}
補充一點,我這邊是刪除了原先的nginx.conf的配置,如果你的項目使用了字體就會無法加載樣式 mime的問題
# fangt add for web server
# 2018-10-27
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile on;
tcp_nodelay on;
keepalive_timeout 30;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/dist;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
location / {
try_files $uri $uri/ =404;
index index.html index.htm;
gzip_static on;
expires max;
add_header Cache-Control public;
if ($request_filename ~* ^.*?\.(eot)|(ttf)|(woff)|(svg)|(otf)$) {
add_header Access-Control-Allow-Origin *;
}
}
}
}
4)創建構建鏡像的文件,這里采用Dockerfile
FROM nginx
RUN mkdir /usr/share/nginx/dist
RUN rm -rf /etc/nginx/nginx.conf
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./ /usr/share/nginx/dist
EXPOSE 9099
5)通過Dockerfile構建鏡像
運行終端,切換到文件路徑,運行命令 docker image build -t mkweb/testsite .
E:\003_pWork\99_Test\testSite>docker image build -t mkweb/testsite .
Sending build context to Docker daemon 4.608kB
Step 1/6 : FROM nginx
---> dbfc48660aeb
Step 2/6 : RUN mkdir /usr/share/nginx/dist
---> Running in 2cf0166722bd
---> 12198d4d5874
Removing intermediate container 2cf0166722bd
Step 3/6 : RUN rm -rf /etc/nginx/nginx.conf
---> Running in 9872d0cca830
---> c8a902ab6df4
Removing intermediate container 9872d0cca830
Step 4/6 : COPY ./nginx.conf /etc/nginx/nginx.conf
---> 672ba1dd4562
Removing intermediate container 89237707122a
Step 5/6 : COPY ./ /usr/share/nginx/dist
---> d12c1ffdea45
Removing intermediate container 8ebda800c5a1
Step 6/6 : EXPOSE 9099
---> Running in c258bd6153d6
---> a21f5d3b4b4f
Removing intermediate container c258bd6153d6
Successfully built a21f5d3b4b4f
SECURITY WARNING: You are building a Docker image from Windows against a non-Win
dows Docker host. All files and directories added to build context will have '-r
wxr-xr-x' permissions. It is recommended to double check and reset permissions f
or sensitive files and directories.
6)運行鏡像
docker run -p 9099:9099 mkweb/testsite
7)測試頁面,顯示正常

8)過程完結
