mac docker打包nginx web应用
本文使用docker打包nginx应用并运行;
安装docker
[Docker官网]](https://www.docker.com/)
1.编写DockerFile文件,在web项目中
#Nginx Dockerfile
FROM centos
# 下载阿里云的源替换官方的源
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
RUN ls /etc/yum.repos.d/
# 删除自带的源,避免去下国外的源
RUN rm -rf /etc/yum.repos.d/CentOS-Linux-AppStream.repo
# 删除自带的源,避免去下国外的源
RUN rm -rf /etc/yum.repos.d/CentOS-Linux-BaseOS.repo
RUN yum install -y nginx
RUN rm -rf /usr/share/nginx/html/*
# 复制nginx.conf
COPY nginx.conf /etc/nginx/nginx.conf
# 复制web应用,默认是 /web
COPY dist /usr/share/nginx/html/web
# 暴露端口
EXPOSE 7000 443
CMD ["nginx","-g","daemon off;"]
需要将打包输出路径设置为 /web;例:http://127.0.0.1/web/
2. 创建nginx.conf文件
# user nginx;
worker_processes 1;
# pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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"';
error_log /var/log/nginx/server_error.log;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 7000;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /web {
try_files $uri $uri/ /web/index.html?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location ~ /\.(?!well-known).* {
deny all;
}
}
include servers/*;
}
3. 打包docker
# 打包镜像
docker build -t eladminv1.0 .
# 运行docker 镜像
docker run -d -p 127.0.0.1:7000:7000 eladminv1.0