我正在尝试将运行Next.js和NGINX的Docker容器部署到Google Compute Engine。当使用docker-compose up
部署在本地时,它可以完美工作,但是部署到CE时,我会收到错误
[emerg] 1#1: host not found in upstream "nextjs:3000" in /etc/nginx/conf.d/default.conf:4
本地日志是
Attaching to nextjs, profiles-fe_nginx_1
nextjs | 2019-05-02T03:51:26: PM2 log: Launching in no daemon mode
nextjs | 2019-05-02T03:51:26: PM2 log: App [npm:0] starting in -fork mode-
nextjs | 2019-05-02T03:51:26: PM2 log: App [npm:0] online
nextjs | > create-next-example-app@ start /usr/app
nextjs | > NODE_ENV=production node server.js
nextjs | > Ready on http://localhost:3000
部署到CE命令
docker-compose build
docker tag <local_container> <remote_container>
docker push <remove_container
gcloud beta compute --project=<project_name> instances create-with-container <instance_name> --zone=europe-west2-c --machine-type=g1-small --container-image=<remove_container> --tags http-server,https-server
default.conf
upstream app {
server nextjs:3000;
}
server {
listen 80;
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;
# BUILT ASSETS (E.G. JS BUNDLES)
# Browser cache - max cache headers from Next.js as build id in url
# Server cache - valid forever (cleared after cache "inactive" period)
location /_next/static {
# proxy_cache STATIC;
proxy_pass http://app;
}
# STATIC ASSETS (E.G. IMAGES)
# Browser cache - "no-cache" headers from Next.js as no build id in url
# Server cache - refresh regularly in case of changes
location /static {
# proxy_cache STATIC;
proxy_ignore_headers Cache-Control;
proxy_cache_valid 60m;
proxy_pass http://app;
}
# DYNAMIC ASSETS - NO CACHE
location / {
proxy_pass http://app;
}
}
docker-compose
文件
version: '3'
services:
nextjs:
build: ./
ports:
- "3000:3000"
networks:
- app-network
container_name: nextjs
restart: unless-stopped
nginx:
build: ./nginx
depends_on:
- nextjs
ports:
- "80:80"
links:
- nextjs:nextjs
networks:
- app-network
networks:
app-network:
driver: bridge
./nginx/Dockerfile
FROM nginx
# 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 80
./Dockerfile
FROM node:11.14.0-alpine
# Set working directory
WORKDIR /usr/app
# Install PM2 globally
RUN npm install --global pm2
# 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 --save
# Copy all files
COPY ./ ./
# Build app
RUN npm run build
EXPOSE 3000
# Launch app with PM2
CMD [ "pm2-runtime", "start", "npm", "--", "start" ]