This page gives Dockerfiles that work on Webgune, and the rules that keep images small and builds fast. To turn on the Dockerfile build, see Docker.
The rules
- Listen on
$PORTand on0.0.0.0. - Build in several stages: build tools stay in the first one, the final image receives only the result.
- Copy dependency files before the code, so the install stays cached as long as dependencies do not change.
- Install from the lock file (
npm ci,pip install -r,go mod download). - Never copy a secret into the image. The service's variables are given to the container at runtime.
- Run the process in the foreground, as a non-root user when the base image provides one.
- Write logs to standard output.
Node.js
FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build && npm prune --omit=dev
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build --chown=node:node /app/package.json ./
COPY --from=build --chown=node:node /app/node_modules ./node_modules
COPY --from=build --chown=node:node /app/dist ./dist
USER node
CMD ["node", "dist/server.js"]
npm prune --omit=dev removes development dependencies before the copy into the final image.
Standalone Next.js
With output: "standalone" in next.config:
FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
ARG NEXT_PUBLIC_API_URL
RUN npm run build
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production HOSTNAME=0.0.0.0
COPY --from=build --chown=node:node /app/.next/standalone ./
COPY --from=build --chown=node:node /app/.next/static ./.next/static
COPY --from=build --chown=node:node /app/public ./public
USER node
CMD ["node", "server.js"]
HOSTNAME=0.0.0.0is essential: without it, the Next.js standalone server listens on the machine name and cannot be reached..next/staticandpublicmust be copied next toserver.js, otherwise CSS, JavaScript and images answer 404.ARG NEXT_PUBLIC_API_URLpicks up the service variable at build time (Docker).
With no particular need, auto build does exactly this (Next.js).
Static site with Nginx
FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:1.28-alpine
ENV PORT=80
COPY --from=build /app/dist /usr/share/nginx/html
COPY <<'EOF' /etc/nginx/templates/default.conf.template
server {
listen ${PORT};
root /usr/share/nginx/html;
location / {
try_files $uri $uri/ /index.html;
}
}
EOF
The official Nginx image replaces ${PORT} at startup in the files of /etc/nginx/templates. The Static site nature of auto build produces the same image without a Dockerfile (React / Vite).
Python
FROM python:3.12-slim
WORKDIR /app
ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN useradd --uid 1000 app && chown -R app /app
USER app
CMD ["sh", "-c", "gunicorn monprojet.wsgi:application --bind 0.0.0.0:${PORT:-8000} --workers 2"]
A -slim base (Debian) is the right choice for TensorFlow, PyTorch or opencv-python, which do not work on Alpine.
Go
FROM golang:1.24-alpine AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/api
FROM alpine:3.21
RUN apk add --no-cache ca-certificates tzdata
COPY --from=build /out/server /server
USER 1000
CMD ["/server"]
ca-certificates is needed for outgoing HTTPS calls, tzdata for time zones.
Laravel with its assets
FROM node:22-alpine AS assets
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM php:8.3-cli-alpine
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer
COPY --from=mlocati/php-extension-installer:2 /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions pdo_mysql pdo_pgsql intl zip bcmath gd opcache
WORKDIR /app
COPY composer.json composer.lock ./
RUN composer install --no-dev --no-scripts --no-autoloader --prefer-dist
COPY . .
COPY --from=assets /app/public/build ./public/build
RUN composer dump-autoload --optimize --no-dev
CMD ["sh", "-c", "php artisan config:cache && php artisan serve --host=0.0.0.0 --port=${PORT:-8000}"]
Auto build cannot build the Vite assets of a Laravel project. This Dockerfile does it in a first Node.js stage (PHP).
Subfolder of a monorepo
The Dockerfile stays at the repository root. It copies what the application needs:
FROM node:22-alpine AS build
WORKDIR /repo
COPY package.json package-lock.json ./
COPY apps/api/package.json apps/api/
COPY packages/shared/package.json packages/shared/
RUN npm ci
COPY . .
RUN npm run build --workspace apps/api
FROM node:22-alpine
WORKDIR /repo
COPY --from=build /repo ./
USER node
CMD ["node", "apps/api/dist/index.js"]
Make the application listen on $PORT
| Form | Result |
|---|---|
CMD ["node", "server.js"] | Correct if the code reads process.env.PORT |
CMD ["sh", "-c", "app --port ${PORT:-8000}"] | Correct: the shell replaces the variable |
CMD ["app", "--port", "$PORT"] | Incorrect: the application receives the text $PORT |
CMD app --port 8000 | Correct only if the service's Container port is 8000 |
Checklist
| Item | Checked |
|---|---|
The application listens on 0.0.0.0:$PORT | |
The .dockerignore excludes .git, node_modules, .env | |
| Dependency files are copied before the code | |
| The final image contains neither a compiler nor needless sources | |
No secret in COPY, ENV or ARG | |
The process handles SIGTERM | |
| Files to keep go on a volume |
Test locally
docker build -t mon-app .
docker run --rm -e PORT=8080 -p 8080:8080 mon-app
If http://localhost:8080 answers, the image will behave the same way on Webgune, except that the build there has no access to your internal services.
Updated on