Dokcer 部署 Nginx 应用示例合集

170 阅读3分钟

Dokcer 部署 Nginx 应用示例合集

Nginx 在 Docker 上简单应用

# 查询Docker Hub 上的 nginx 镜像
docker search nginx

# 拉取官方的Nginx镜像
docker pull nginx

# 使用 Nginx 默认的配置来启动一个 Nginx 容器实例
docker run --rm --name nginx-01 -p 8801:80 -d nginx

# --rm:容器终止运行后,自动删除容器文件。
# --name nginx-01:容器的名字叫做nginx-01
# -p:端口进行映射,将本地 8801 端口映射到容器内部的 80 端口
# -d:容器启动后,在后台运行

成功, http://81.71.98.176:8801/

映射本地目录到 nginx 容器

# 1、创建本地(挂载)目录,用于存放Nginx的相关文件信息
# 根目录 /root/nginx/demo-02
mkdir -p /root/nginx/demo-02/www /root/nginx/demo-02/logs /root/nginx/demo-02/conf

# www: 目录将映射为 nginx 容器配置的虚拟目录。
# logs: 目录将映射为 nginx 容器的日志目录。
# conf: 目录里的配置文件将映射为 nginx 容器的配置文件。

# 2、拷贝容器内 Nginx 默认配置文件到本地当前目录下的 conf 目录
docker cp 2116cfbd95e1:/etc/nginx/nginx.conf /root/nginx/demo-02/conf/

# 3、部署
docker run -d -p 8802:80 --name nginx-demo-02 \
  -v /root/nginx/demo-02/www:/usr/share/nginx/html \
  -v /root/nginx/demo-02/conf/nginx.conf:/etc/nginx/nginx.conf \
  -v /root/nginx/demo-02/logs:/var/log/nginx \
  nginx
  
# --rm:       容器终止运行后,自动删除容器文件。
# -p 8802:80: 将容器的 80 端口映射到主机的 8802 端口.
# --name nginx-demo-02:将容器命名为 nginx-demo-02 
# -v /root/nginx/demo-02/www:/usr/share/nginx/html:将我们自己创建的 www 目录挂载到容器的 /usr/share/nginx/html。
# -v /root/nginx/demo-02/conf/nginx.conf:/etc/nginx/nginx.conf:将我们自 己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。
# -v /root/nginx/demo-02/logs:/var/log/nginx:将我们自己创建的 logs 挂载到容器的 /var/log/nginx
  • 启动以上命令后进入 /root/nginx/demo-02/www 目录
cd /root/nginx/demo-02/www

# 创建 index.html 文件
touch index.html

# index.html 内容
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Nginx-02</title>
</head>
<body>
    <h1>hello nginx-02</h1>
</body>
</html>

成功,http://81.71.98.176:8802/

vite-antd-pc 项目

  • 启动一个nginx 项目
# 根目录 /root/vite-antd-pc
mkdir -p /root/vite-antd-pc/www /root/vite-antd-pc/logs /root/vite-antd-pc/conf

mkdir -p /root/vite-antd-pc/source

# 2、拷贝容器内 Nginx 默认配置文件到本地当前目录下的 conf 目录
docker cp 2116cfbd95e1:/etc/nginx/nginx.conf /root/vite-antd-pc/conf/

# 3、部署
docker run -d -p 8803:80 --name vite-antd-pc \
  -v /root/vite-antd-pc/www:/usr/share/nginx/html \
  -v /root/vite-antd-pc/conf/nginx.conf:/etc/nginx/nginx.conf \
  -v /root/vite-antd-pc/logs:/var/log/nginx \
  nginx
  • 前端文件部署, 将该项目的打包后的代码上传到 /root/vite-antd-pc/www
# 拉取代码
cd source/
git clone https://github.com/luozyiii/vite-antd-pc.git

# 安装依赖
npm install
# 构建
npm run build

# copy 构建产物 到 www
cp -r /root/vite-antd-pc/source/vite-antd-pc/dist/* /root/vite-antd-pc/www/
  • history路由,需增加配置
# conf/nginx.conf
http {
    server {
        # 监听端口
        listen       80;
        # 监听地址
        server_name  localhost;
        # 静态资源
        location / {
            # 根目录
            root   /usr/share/nginx/html;
            # 设置默认页
            index  index.html index.htm;
            # url 切换时始终返回index.html history 路由
            try_files $uri /index.html;
        }
        # 接口转发
        location ~ /api/ {
            proxy_pass http://112.74.201.142:7001;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

    include /etc/nginx/conf.d/*.conf;
    
}

# 增加配置后,重启应用 docker restart xxx

成功,http://81.71.98.176:8803/

Docker 部署 NestJS 应用

juejin.cn/post/725751…

成功,http://81.71.98.176:3000/