我正在尝试在docker中使用nginx容器化下一个js应用程序。我有用于nextjs应用程序的Dockerfile:
FROM node:18-alpine as builder
WORKDIR /my-space
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:18-alpine as runner
WORKDIR /my-space
COPY --from=builder /my-space/package.json .
COPY --from=builder /my-space/package-lock.json .
COPY --from=builder /my-space/next.config.js ./
COPY --from=builder /my-space/public ./public
COPY --from=builder /my-space/.next/standalone ./
COPY --from=builder /my-space/.next/static ./.next/static
EXPOSE 3000
ENTRYPOINT ["npm", "start"]
字符串
我有一个nginx的Dockerfile:
# Base on offical NGINX Alpine image
FROM nginx:stable
# Remove any existing config files
RUN rm /etc/nginx/conf.d/*
# Copy config files
# *.conf files in conf.d/ dir get included in main config
COPY ./default.conf /etc/nginx/conf.d/
# Expose the listening port
EXPOSE 80
# Launch NGINX
CMD [ "nginx", "-g", "daemon off;" ]
型
我有这个nginx default.conf
配置:
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC:10m inactive=7d use_temp_path=off;
upstream nextjs_upstream {
server frontend:3000;
}
server {
listen 80 default_server;
server_name _;
server_tokens off;
gzip on;
gzip_proxied any;
gzip_comp_level 4;
gzip_types text/css application/javascript image/svg+xml;
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;
}
location /_next/static {
proxy_cache STATIC;
proxy_pass http://nextjs_upstream;
}
location /static {
proxy_cache STATIC;
proxy_ignore_headers Cache-Control;
proxy_cache_valid 60m;
proxy_pass http://nextjs_upstream;
}
location /_next {
proxy_pass http://nextjs_upstream;
}
location /_next/public {
proxy_cache STATIC;
proxy_pass http://nextjs_upstream;
}
location /_next/standalone {
proxy_cache STATIC;
proxy_pass http://nextjs_upstream;
}
location /public {
proxy_cache STATIC;
proxy_ignore_headers Cache-Control;
proxy_cache_valid 60m;
proxy_pass http://nextjs_upstream;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location /api/ {
proxy_pass https://mydev.online/api;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
型
我用所需的标签构建了nextjs app和nginx,然后运行docker compose:
version: '3.9'
services:
frontend:
image: myfront
container_name: frontend
ports:
- 3000:3000
nginx:
image: nginx-custom
ports:
- 80:80
型
但问题是它不起作用。在next.config.json
中,我有:
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
outputStandalone: true,
},
型
但我也尝试了没有它的Dockerfile:
# Build Stage
FROM node:16-alpine AS BUILD_IMAGE
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Production Stage
FROM node:16-alpine AS PRODUCTION_STAGE
WORKDIR /app
COPY --from=BUILD_IMAGE /app/package*.json ./
COPY --from=BUILD_IMAGE /app/.next ./.next
COPY --from=BUILD_IMAGE /app/public ./public
COPY --from=BUILD_IMAGE /app/node_modules ./node_modules
ENV NODE_ENV=production
EXPOSE 3000
CMD ["npm", "start"]
型
但是这两个选项都不起作用。我可以在3000端口上打开网站,但不能在80端口上打开nginx,因为当试图访问/_next
上的路径时,它会出现很多错误并返回空白页面,相同的路径似乎在3000端口上工作。这里可能有什么问题?
附注:我尝试了这个dockerfile,它也不工作(以及它的一些变体),因为它在构建图像时会出错,因为一些模块丢失,或者类似这样的东西(有很多错误):
# Base on offical Node.js Alpine image
FROM node:alpine
# 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 ./
# Install dependencies
RUN npm install --production
# Copy all files
COPY ./ ./
# Build app
RUN npm run build
# Expose the listening port
EXPOSE 3000
# Run container as non-root (unprivileged) user
# The node user is provided in the Node.js Alpine base image
USER node
# Run npm start script when container starts
CMD [ "npm", "start" ]
型
UPD:我还尝试将docker compose文件中的network_mode: host
设置为nginx和frontend容器,并将default.conf中的localhost:3000
设置为重定向到,但这也没有帮助。
1条答案
按热度按时间jucafojl1#
我猜容器之间的网络连接不起作用。可能需要在Docker compose中添加链接和主机
字符串