Clu*_*der 7 nginx docker next.js
我有一个 nextjs 项目,希望使用 Docker 和 nginx 运行。
我希望使用在后台连接到 nextjs 的 nginx(只有 nginx 可以与 nextjs 通信,用户需要与 nginx 通信才能与 nextjs 通信)。
假设它是标准的nextjs项目结构和dockerfile内容(下面提供),有没有办法在docker中与nextjs一起使用nginx?
我知道我可以使用 Docker-compose。但我想将其保留在一个 docker 映像下。因为我打算将图像推送到 heroku 虚拟主机。
注意:我正在使用服务器端渲染
docker文件
# Base on offical Node.js Alpine image
FROM node:latest as builder
# Set working directory
WORKDIR /usr/app
# install node-prune (https://github.com/tj/node-prune)
RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin
# Copy package.json and package-lock.json before other files
# Utilise Docker cache to save re-installing dependencies if unchanged
COPY package.json ./
COPY yarn.lock ./
# Install dependencies
RUN yarn install --frozen-lockfile
# Copy all files
COPY ./ ./
# Build app
RUN yarn build
# remove development dependencies
RUN yarn install --production
# run node prune. Reduce node_modules size
RUN /usr/local/bin/node-prune
#######################################################
FROM node:alpine
WORKDIR /usr/app
# COPY package.json next.config.js .env* ./
# COPY --from=builder /usr/app/public ./public
COPY --from=builder /usr/app/.next ./.next
COPY --from=builder /usr/app/node_modules ./node_modules
EXPOSE 3000
CMD ["node_modules/.bin/next", "start"]
Run Code Online (Sandbox Code Playgroud)
dockerfile 的灵感来自https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile.multistage
编辑:nginx default.conf
upstream nextjs_upstream {
server nextjs:3000;
# We could add additional servers here for load-balancing
}
server {
listen 80 default_server;
server_name _;
server_tokens off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
location / {
proxy_pass http://nextjs_upstream;
}
}
Run Code Online (Sandbox Code Playgroud)
为了能够在单个 Docker 容器中同时使用 Nginx 和 NextJS而不使用 Docker-Compose,您需要使用Supervisord
Supervisor 是一个客户端/服务器系统,允许用户控制类 UNIX 操作系统上的多个进程。
问题不在于 nginx 配置或 dockerfile。启动容器时它同时运行 nginx 和 nextjs。由于我找不到同时运行两者的方法,因此使用supervisord是我需要的工具。
其工作需要以下内容
Dockerfile
# Base on offical Node.js Alpine image
FROM node:latest as builder
# Set working directory
WORKDIR /usr/app
# Copy package.json and package-lock.json before other files
# Utilise Docker cache to save re-installing dependencies if unchanged
COPY package.json ./
COPY yarn.lock ./
# Install dependencies
RUN yarn install --frozen-lockfile
# Copy all files
COPY ./ ./
# Build app
RUN yarn build
# remove development dependencies
RUN yarn install --production
#######################################################
FROM nginx:alpine
WORKDIR /usr/app
RUN apk add nodejs-current npm supervisor
RUN mkdir mkdir -p /var/log/supervisor && mkdir -p /etc/supervisor/conf.d
# Remove any existing config files
RUN rm /etc/nginx/conf.d/*
# Copy nginx config files
# *.conf files in conf.d/ dir get included in main config
COPY ./.nginx/default.conf /etc/nginx/conf.d/
# COPY package.json next.config.js .env* ./
# COPY --from=builder /usr/app/public ./public
COPY --from=builder /usr/app/.next ./.next
COPY --from=builder /usr/app/node_modules ./node_modules
# supervisor base configuration
ADD supervisor.conf /etc/supervisor.conf
# replace $PORT in nginx config (provided by executior) and start supervisord (run nextjs and nginx)
CMD sed -i -e 's/$PORT/'"$PORT"'/g' /etc/nginx/conf.d/default.conf && \
supervisord -c /etc/supervisor.conf
Run Code Online (Sandbox Code Playgroud)
主管.conf
[supervisord]
nodaemon=true
[program:nextjs]
directory=/usr/app
command=node_modules/.bin/next start
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
autorestart=true
[program:nginx]
command=nginx -g 'daemon off;'
killasgroup=true
stopasgroup=true
redirect_stderr=true
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
autorestart=true
Run Code Online (Sandbox Code Playgroud)
nginx 配置(default.conf)
upstream nextjs_upstream {
server localhost:3000;
# We could add additional servers here for load-balancing
}
server {
listen $PORT default_server;
server_name _;
server_tokens off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
location / {
proxy_pass http://nextjs_upstream;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:使用 nginx 作为反向代理。NextJS 将在端口 3000 上运行。用户将无法直接访问它。必须经过nginx。
构建docker镜像
docker build -t nextjs-img -f ./dockerfile .
Run Code Online (Sandbox Code Playgroud)
运行docker容器
docker run --rm -e 'PORT=80' -p 8080:80 -d --name nextjs-img nextjs-img:latest
Run Code Online (Sandbox Code Playgroud)
去localhost:8080
| 归档时间: |
|
| 查看次数: |
5524 次 |
| 最近记录: |