部署项目通常会浪费很多时间,使用docker容器化可以大大压缩这些时间,以下介绍在docker容器内部署项目。
目录结构
> tree .
.
├── Dockerfile
├── html
│ ├── css
│ │ └── index.css
│ ├── Dockerfile
│ ├── images
│ │ ├── background.png
│ │ ├── bottom-left-center.png
│ │ ├── bottom-right-center.png
│ │ ├── \ branch.png
│ │ ├── img_background.png
│ │ ├── normal-hover.png
│ │ ├── normal.png
│ │ └── top-centre.png
│ ├── index.html
│ ├── js
│ │ └── rem_js.js
│ └── README.md
└── nginx.conf
4 directories, 15 files
编写Dockerfile文件
FROM ubuntu:latest # 使用较小的ubuntu作为虚拟环境
RUN apt-get update && apt-get install -y vim && apt-get install -y nginx # 安装nginx
RUN rm -rf /usr/share/nginx/html/* && rm -rf /etc/nginx/nginx.conf # 删除nginx原有文件
COPY nginx.conf /etc/nginx/ # 复制nginx文件配置
COPY html /usr/share/nginx/html/ # 将项目文件写入nginx的目录
EXPOSE 80 # 暴露80端口
CMD ["nginx","-g","daemon off;"] # 启动nginx
配置nginx文件
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server { # nginx默认文件是没有server的
listen 80;
server_name 192.168.0.123;
client_max_body_size 5M;
location / {
root html;
index index.html index.htm;
}
}
}
执行构建命令
docker build -t myproject:1.0.0 .
执行启动容器
docker run -it --name myproject -p 10800:80 -d myproject:1.0.0
验证结果
浏览器输入http://192.168.0.123:10800/显示页面即完成部署。