docker通过nginx部署公司官网
一、准备静态页面
这里不做过多解释,公司官网本次演示采用静态html模版。
二、上传网站到服务器
这里采用scp命令上传
scp -P 6022 -r home root@121.5.223.16:/root/706
文件上传后,我们在服务器中创建Dockerfile文件。
这里我们配置的nginx成ssl,支持https协议。
FROM nginx
MAINTAINER wanghongjie
COPY home/ /usr/share/nginx/html/
COPY nginx.conf /etc/nginx/nginx.conf
COPY cert/ /etc/nginx/conf.d/
RUN echo 'echo init ok!!'
说明:
FROM nginx 获取nginx镜像
MAINTAINER wanghongjie 制作镜像作者
COPY home/ /usr/share/nginx/html/ 复制home文件夹下文件到nginx/html 目录下
COPY cert/ /etc/nginx/conf.d/ 复制 https 证书到nginx/conf.d/ 目录下。记录该目录地址。
RUN echo 'echo init ok!!'输出镜像制作完成。
在Dockerfile同级目录下,新建 nginx.conf 文件
文件内容如下:
worker_processes auto;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
client_max_body_size 20m;
server {
listen 443 ssl;
server_name www.706xinli.com 706xinli.com; # 自己的域名
# 注意文件位置,是从/etc/nginx/下开始算起的
ssl_certificate conf.d/1_706xinli.com_bundle.crt;
ssl_certificate_key conf.d/2_706xinli.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
client_max_body_size 1024m;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /about {
root /usr/share/nginx/html;
index about.html index.htm;
try_files $uri $uri/ /about.html;
}
}
server {
listen 80; # 监听80端口
server_name www.706xinli.com 706xinli.com; # 自己的域名
#把http的域名请求转成https
return 301 https://$host$request_uri;
}
}
具体配置内容暂时不做过多解释,需要注意下user root;
这里用户配置的是root用户。另外该配置采用强制https协议,如访问80端口,则强制转发到443端口。
三、docker镜像制作
执行以下命令:
docker build -t 706_home:V1.0.3 .
其中 706_home:V1.0.3分别为镜像名称和版本号。
Sending build context to Docker daemon 6.913MB
Step 1/6 : FROM nginx
---> d1a364dc548d
Step 2/6 : MAINTAINER wanghongjie
---> Using cache
---> a62c4a7861b1
Step 3/6 : COPY home/ /usr/share/nginx/html/
---> Using cache
---> 91663b8fc82f
Step 4/6 : COPY nginx.conf /etc/nginx/nginx.conf
---> 25abcc31cf3a
Step 5/6 : COPY cert/ /etc/nginx/conf.d/
---> c5c5fde4492a
Step 6/6 : RUN echo 'echo init ok!!'
---> Running in cc234ad04b1a
echo init ok!!
Removing intermediate container cc234ad04b1a
---> c7d44a14f287
Successfully built c7d44a14f287
Successfully tagged 706_home:V1.0.3
输出以上信息则镜像打包完成。
四、运行
docker run -p 443:443 -d --name 706home c7d44a14f287
这里的c7d44a14f287
为打包镜像时产生的镜像ID.
更多内容请查看 Julywhj的博客期待您的光顾。