Skip to content

Instantly share code, notes, and snippets.

@NimJay
Last active December 30, 2022 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NimJay/170c6678735a8f1dd68d89328e4f23e4 to your computer and use it in GitHub Desktop.
Save NimJay/170c6678735a8f1dd68d89328e4f23e4 to your computer and use it in GitHub Desktop.
Dockerfile that hosts a Preact app, using NGINX.
# Install Node dependencies.
FROM node:16-slim AS builder-stage
WORKDIR /app
COPY package.json package.json
COPY package-lock.json package-lock.json
RUN npm install
# Compile our TypeScript code.
COPY src/ src/
COPY tsconfig.json tsconfig.json
RUN npm run build
# Use NGINX in a new Docker image to serve the static files.
FROM nginx:1.23
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=builder-stage /app/build/ /usr/share/nginx/html
@NimJay
Copy link
Author

NimJay commented Dec 30, 2022

Next to this Dockerfile, you will also need a file called nginx.conf with contents:

server {
  listen        80;
  server_name   localhost;
  root          /usr/share/nginx/html;
  location / {
    index index.html;
    try_files $uri /index.html;
  }
}

The main purpose of this nginx.conf is to make sure non-home routes in your Preact app do not 404 (via the try_files $uri /index.html; line).
The only problem with this is that your server will never actually respond with a 404 status code — even when it should.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment