Skip to content

Instantly share code, notes, and snippets.

@tolu
Created November 14, 2022 21:21
Show Gist options
  • Save tolu/1293977eeaea1d8ae31d06a8e233723e to your computer and use it in GitHub Desktop.
Save tolu/1293977eeaea1d8ae31d06a8e233723e to your computer and use it in GitHub Desktop.
Docker nginx static web server
version: "3.9"
services:
web:
build:
context: relative/path/to/cwd
dockerfile: ./path/from/cwd/to/Dockerfile
ports:
- "40800:443"
args:
- some_override: https://localhost:5000
volumes:
- ./certs/dev.pem:/usr/share/nginx/cert/site.crt
- ./certs/dev.key:/usr/share/nginx/cert/site.key
import { existsSync } from 'fs';
import { mkdir, readdir, writeFile } from 'fs/promises';
import { dirname, join } from 'path';
import mkcert from 'vite-plugin-mkcert';
const dirPath = dirname(process.argv[1]);
const certPath = join(dirPath, 'certs');
const [crtFile, keyFile] = ['localhost.crt', 'localhost.key'];
// Create cert folder if not exists
if (!existsSync(certPath)) {
console.log('Create "cert" folder...');
await mkdir(certPath);
}
// read cert files
const certFiles = await readdir(certPath);
// If all required certs are in folder, exit early 👍
if (
certFiles.includes(keyFile) &&
certFiles.includes(crtFile)) {
console.log('Certificate files ok 🎉');
process.exit();
}
console.log(`
# Setup certificates for nginx
--------------------------------------------------------------
- dirname: ${dirPath}
- certfiles: ${stringify(certFiles)}
--------------------------------------------------------------
`);
// Install certificates
try {
const { config: installMkCert } = mkcert();
const { server: { https: { key, cert } } } = await installMkCert({});
console.log('Writing cert files...')
await writeFile(join(certPath, keyFile), key, { encoding: 'utf-8' });
await writeFile(join(certPath, crtFile), cert, { encoding: 'utf-8' });
console.log('\nAll set 🎉\n');
} catch (writeErr) {
console.log(writeErr.toString());
}
FROM node:lts-alpine as BUILD
WORKDIR /app
# Copy files needed for install
COPY .npmrc ./
COPY *.json ./
# Install deps
RUN npm ci --ignore-scripts
# Copy source files
COPY src/ src/
COPY dockerfiles dockerfiles/
# Use variables from compose file
ARG some_override
ENV SOM_ENV_VAR=$some_override
# build app
RUN npm run build
# Copy build output and nginx config
# Certificates are mounted in compose file
FROM nginx:latest
COPY --from=BUILD /app/dockerfiles/nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=BUILD /app/build/ /usr/share/nginx/html/
# redirect to https (requires separate path bound from outside)
# server {
# listen 80 default_server;
# server_name _;
# return 301 https://$http_host$request_uri;
# }
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /usr/share/nginx/cert/site.crt;
ssl_certificate_key /usr/share/nginx/cert/site.key;
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ =404;
}
error_page 404 /index.html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment