Skip to content

Commit

Permalink
133 add docker support (#134)
Browse files Browse the repository at this point in the history
* feat(docker): add dockerFile

* feat(docker): fix it

* fix(docker): fix it

* fix(docker): fix it

* fix(docker): fix it

* fix(docker): use standalone mode

* feat(docker): use github action build docker image

* fix(docker): fix docker build env

* feat(docker): temp use develop env

* feat(docker): copy env to build docker image stage

* feat(docker): use aliyun ACR

* feat(docker): push multi tag for docker

* feat(docker): update docker-compose

* fix(docker): fix build error

* fix(docker): fix push multi tag

* fix(docker): fix push multi tag

* fix(docker): fix push multi tag

* fix(docker): fix docker-compose

* fix(docker): fix docker-compose.yml

* fix(docker): fix docker-compose.yml

* fix(docker): fix docker-compose.yml

* feat(docker): add stack env to docker compose

* feat(debug): add debug code

* feat(docker): add cn docker

* fix(docker): fix startup failed

* feat(docker): not expose port
  • Loading branch information
PaiJi authored Jan 21, 2024
1 parent fe78aa1 commit eb1f0c0
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.next
13 changes: 13 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# API key used by the CLI and the SDK
# Make sure your framework/tooling loads this file on startup to have it available for the SDK
XATA_API_KEY=
# Xata branch that will be used if there's not a xata branch with the same name as your git branch
XATA_BRANCH=main
XATA_FALLBACK_BRANCH=main
ENABLE_TRACK=false
NEXT_PUBLIC_ENABLE_CN_DOMAIN=false
NEXT_PUBLIC_REGION=GLOBAL
CN_STATIC_URL=www.furrycons.cn
SENTRY_DSN=
SENTRY_AUTH_TOKEN=
NEXT_PUBLIC_SENTRY_DSN=
86 changes: 86 additions & 0 deletions .github/workflows/deploy-self-host.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#
name: Deploy To Selfhost Machine

# Configures this workflow to run every time a change is pushed to the branch called `release`.
on:
push:
branches: main

jobs:
global-image-release:
runs-on: ubuntu-latest
environment: Prod
env:
XATA_API_KEY: ${{ secrets.XATA_API_KEY }}
XATA_FALLBACK_BRANCH: ${{ vars.XATA_FALLBACK_BRANCH }}
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
ENABLE_TRACK: ${{ vars.ENABLE_TRACK }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Create Env
run: |
touch .env
echo NEXT_PUBLIC_REGION="GLOBAL" >>.env
echo XATA_API_KEY="$XATA_API_KEY" >> .env
echo XATA_FALLBACK_BRANCH="$XATA_FALLBACK_BRANCH" >> .env
echo SENTRY_AUTH_TOKEN="$SENTRY_AUTH_TOKEN" >> .env
echo ENABLE_TRACK="$ENABLE_TRACK" >> .env
- name: Login to ACR
uses: aliyun/acr-login@v1
with:
login-server: https://registry.cn-hongkong.aliyuncs.com
username: "${{ secrets.REGISTRY_USERNAME }}"
password: "${{ secrets.REGISTRY_PASSWORD }}"

- name: Build and push image
env:
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t registry.cn-hongkong.aliyuncs.com/fec/web:$IMAGE_TAG -t registry.cn-hongkong.aliyuncs.com/fec/web:latest .
docker push registry.cn-hongkong.aliyuncs.com/fec/web:$IMAGE_TAG
docker push registry.cn-hongkong.aliyuncs.com/fec/web:latest
cn-image-release:
runs-on: ubuntu-latest
environment: Prod-CN
env:
XATA_API_KEY: ${{ secrets.XATA_API_KEY }}
XATA_FALLBACK_BRANCH: ${{ vars.XATA_FALLBACK_BRANCH }}
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
ENABLE_TRACK: ${{ vars.ENABLE_TRACK }}
NEXT_PUBLIC_ENABLE_CN_DOMAIN: true
CN_STATIC_URL: ${{vars.CN_STATIC_URL}}
SENTRY_DSN: ${{vars.SENTRY_DSN}}
NEXT_PUBLIC_SENTRY_DSN: ${{vars.SENTRY_DSN}}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Create Env
run: |
touch .env
echo NEXT_PUBLIC_REGION="CN" >>.env
echo XATA_API_KEY="$XATA_API_KEY" >> .env
echo XATA_FALLBACK_BRANCH="$XATA_FALLBACK_BRANCH" >> .env
echo SENTRY_AUTH_TOKEN="$SENTRY_AUTH_TOKEN" >> .env
echo ENABLE_TRACK="$ENABLE_TRACK" >> .env
echo NEXT_PUBLIC_ENABLE_CN_DOMAIN="true" >>.env
echo CN_STATIC_URL="$CN_STATIC_URL" >>.env
echo SENTRY_DSN="$SENTRY_DSN" >>.env
echo NEXT_PUBLIC_SENTRY_DSN="$NEXT_PUBLIC_SENTRY_DSN" >>.env
- name: Login to ACR
uses: aliyun/acr-login@v1
with:
login-server: https://registry.cn-hongkong.aliyuncs.com
username: "${{ secrets.REGISTRY_USERNAME }}"
password: "${{ secrets.REGISTRY_PASSWORD }}"

- name: Build and push image
env:
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t registry.cn-hongkong.aliyuncs.com/fec/cn-web:$IMAGE_TAG -t registry.cn-hongkong.aliyuncs.com/fec/cn-web:latest .
docker push registry.cn-hongkong.aliyuncs.com/fec/cn-web:$IMAGE_TAG
docker push registry.cn-hongkong.aliyuncs.com/fec/cn-web:latest
68 changes: 68 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
FROM node:18-alpine AS base

# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* .yarnrc.yml package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then corepack enable&&yarn --immutable; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi


# Rebuild the source code only when needed
FROM base AS builder
RUN apk add --no-cache git
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN corepack enable&&yarn build

# If using npm comment out above and use below instead
# RUN npm run build

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000
# set hostname to localhost
ENV HOSTNAME "0.0.0.0"

# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD ["node", "server.js"]
25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3"
services:
global-web:
image: registry-vpc.cn-hongkong.aliyuncs.com/fec/web:latest
restart: always
environment:
- NODE_ENV=production
env_file:
- stack.env
networks:
static-network:
ipv4_address: 10.0.0.9
cn-web:
image: registry-vpc.cn-hongkong.aliyuncs.com/fec/cn-web:latest
restart: always
environment:
- NODE_ENV=production
env_file:
- stack.env
networks:
static-network:
ipv4_address: 10.0.0.10
networks:
static-network:
external: true
14 changes: 1 addition & 13 deletions next-sitemap.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
/** @type {import('next-sitemap').IConfig} */
module.exports = {
siteUrl: "https://www.furryeventchina.com",
generateRobotsTxt: true, // (optional)
outDir: "./out",
alternateRefs: [
{
href: "https://www.furrycons.cn",
hreflang: "zh-cn",
},
{
href: "https://cn.furryeventchina.com",
hreflang: "zh",
},
],
// ...other options
generateRobotsTxt: true,
};
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const gitRevisionPlugin = new GitRevisionPlugin();

/** @type {import('next').NextConfig} */
const nextConfig = {
output: "export",
output: "standalone",
reactStrictMode: true,
images: {
unoptimized: false,
Expand Down
5 changes: 5 additions & 0 deletions src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { Html, Head, Main, NextScript } from "next/document";
import Script from "next/script";

const isEnableTrack = process.env.ENABLE_TRACK === "true";
console.log(
"ENABLE_TRACK",
process.env.ENABLE_TRACK,
typeof process.env.ENABLE_TRACK
);

export default function Document() {
return (
Expand Down

0 comments on commit eb1f0c0

Please sign in to comment.