Add docker

This commit is contained in:
Kasper Juul Hermansen 2022-10-12 22:31:15 +02:00
parent c449ec250a
commit 276343378d
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
13 changed files with 90 additions and 9 deletions

5
.dockerignore Normal file
View File

@ -0,0 +1,5 @@
.next/
.git/
node_modules/
README.md

3
next.config.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = {
output: "standalone",
};

10
prod.docker-compose.yml Normal file
View File

@ -0,0 +1,10 @@
version: "3"
services:
rawpotion-man:
build:
context: .
dockerfile: scripts/docker/prod.Dockerfile
restart: unless-stopped
ports:
- 3000:3000

0
public/.gitkeep Normal file
View File

View File

@ -0,0 +1,63 @@
# Step 1. Rebuild the source code only when needed
FROM node:18-alpine AS builder
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
# Omit --production flag for TypeScript devDependencies
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i; \
else echo "Lockfile not found." && exit 1; \
fi
COPY src ./src
COPY public ./public
COPY posts ./posts
COPY next.config.js .
COPY tsconfig.json .
# Environment variables must be present at build time
# https://github.com/vercel/next.js/discussions/14030
ARG ENV_VARIABLE
ENV ENV_VARIABLE=${ENV_VARIABLE}
ARG NEXT_PUBLIC_ENV_VARIABLE
ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
# Uncomment the following line to disable telemetry at build time
# ENV NEXT_TELEMETRY_DISABLED 1
RUN yarn build
# Step 2. Production image, copy all the files and run next
FROM node:18-alpine AS runner
WORKDIR /app
# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs
COPY --from=builder /app/public ./public
# 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
COPY --from=builder --chown=nextjs:nodejs /app/posts ./posts
# Environment variables must be redefined at run time
ARG ENV_VARIABLE
ENV ENV_VARIABLE=${ENV_VARIABLE}
ARG NEXT_PUBLIC_ENV_VARIABLE
ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
# Uncomment the following line to disable telemetry at run time
# ENV NEXT_TELEMETRY_DISABLED 1
CMD node server.js

View File

@ -8,7 +8,7 @@ import Link from "next/link";
import path from "path"; import path from "path";
import CustomLink from "../../../components/CustomLink"; import CustomLink from "../../../components/CustomLink";
import Layout from "../../../components/Layout"; import Layout from "../../../components/Layout";
import { postFilePaths, POSTS_PATH } from "../../../utils/mdxUtils"; import { golangPostFilePaths, GOLANG_POSTS_PATH } from "../../../utils/mdxUtils";
// Custom components/renderers to pass to MDX. // Custom components/renderers to pass to MDX.
// Since the MDX files aren't loaded by webpack, they have no knowledge of how // Since the MDX files aren't loaded by webpack, they have no knowledge of how
@ -60,7 +60,7 @@ export default function PostPage({ source, frontMatter }) {
} }
export const getStaticProps = async ({ params }) => { export const getStaticProps = async ({ params }) => {
const postFilePath = path.join(POSTS_PATH, `${params.slug}.mdx`); const postFilePath = path.join(GOLANG_POSTS_PATH, `${params.slug}.mdx`);
const source = fs.readFileSync(postFilePath); const source = fs.readFileSync(postFilePath);
const { content, data } = matter(source); const { content, data } = matter(source);
@ -83,7 +83,7 @@ export const getStaticProps = async ({ params }) => {
}; };
export const getStaticPaths = async () => { export const getStaticPaths = async () => {
const paths = postFilePaths const paths = golangPostFilePaths
// Remove file extensions for page paths // Remove file extensions for page paths
.map((path) => path.replace(/\.mdx?$/, "")) .map((path) => path.replace(/\.mdx?$/, ""))
// Map the path into the static paths object required by Next.js // Map the path into the static paths object required by Next.js

View File

@ -3,7 +3,7 @@ import matter from "gray-matter";
import Link from "next/link"; import Link from "next/link";
import path from "path"; import path from "path";
import Layout from "../components/Layout"; import Layout from "../components/Layout";
import { postFilePaths, POSTS_PATH } from "../utils/mdxUtils"; import { golangPostFilePaths, GOLANG_POSTS_PATH } from "../utils/mdxUtils";
export default function Index({ posts }) { export default function Index({ posts }) {
return ( return (
@ -30,8 +30,8 @@ export default function Index({ posts }) {
} }
export function getStaticProps() { export function getStaticProps() {
const posts = postFilePaths.map((filePath) => { const posts = golangPostFilePaths.map((filePath) => {
const source = fs.readFileSync(path.join(POSTS_PATH, filePath)); const source = fs.readFileSync(path.join(GOLANG_POSTS_PATH, filePath));
const { content, data } = matter(source); const { content, data } = matter(source);
return { return {

View File

@ -2,10 +2,10 @@ import fs from "fs";
import path from "path"; import path from "path";
// POSTS_PATH is useful when you want to get the path to a specific file // POSTS_PATH is useful when you want to get the path to a specific file
export const POSTS_PATH = path.join(process.cwd(), "posts"); export const GOLANG_POSTS_PATH = path.join(process.cwd(), "posts/golang");
// postFilePaths is the list of all mdx files inside the POSTS_PATH directory // postFilePaths is the list of all mdx files inside the POSTS_PATH directory
export const postFilePaths = fs export const golangPostFilePaths = fs
.readdirSync(POSTS_PATH) .readdirSync(GOLANG_POSTS_PATH)
// Only include md(x) files // Only include md(x) files
.filter((path) => /\.mdx?$/.test(path)); .filter((path) => /\.mdx?$/.test(path));