构建 Gatsby 的 Docker 镜像
创建 nginx.conf 文件
server {
listen 80;
server_name _;
absolute_redirect off;
root /var/www/;
index index.html;
error_page 404 /404/index.html;
# Force all paths to load either itself (js files) or go through index.html.
location / {
try_files $uri $uri/ =404;
}
}
创建 Dockerfile
FROM node:latest as builder
WORKDIR /app
COPY package*.json ./
COPY yarn.lock ./
# 如果实在国内构建,设置淘宝仓库代理加快构建速度
RUN yarn config set registry https://registry.npm.taobao.org
RUN yarn
COPY . .
RUN yarn build
# 用 nginx 作为网页服务器
FROM nginx:stable
EXPOSE 80
# 从 builder 中复制静态文件到 nginx 静态文件目录
COPY /app/public /var/www
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
# 让 nginx 以前台进程运行
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
CMD ["nginx"]
构建镜像
docker build -t <tag> .