Docker for web apps: Dockerfiles that deploy cleanly

Images, layers and caching explained, with Node and Python Dockerfiles you can copy, and a Compose file for local development.

Ribbed ceramic shipping containers stacked in two layers

A Dockerfile describes exactly how to build and run your app, so the same thing runs on your laptop, in CI and on a server. Written carelessly, it also produces slow builds, huge images and secrets baked into places they should never be.

This guide is for web developers who have used Docker a little, or not at all, and want Dockerfiles they can trust in production. You will learn how images and layers work, write Dockerfiles for a Node and a Python app, run a local stack with PostgreSQL, and see how Launchpad uses your Dockerfile.

The examples assume Docker Engine 25 or later with BuildKit (the default builder) and the docker compose plugin.

Images, containers and layers

An image is a read-only template: a filesystem plus metadata such as the command to run and the port. A container is a running instance of an image, with a thin writable layer on top. When the container is replaced, that writable layer is gone, which is why containers should keep no data you care about.

Each instruction in a Dockerfile that changes files (RUN, COPY, ADD) creates a layer. Docker caches layers. When you rebuild, it reuses every layer up to the first instruction whose inputs changed, then rebuilds everything after it.

That one rule shapes good Dockerfiles: put the things that change least at the top. Copy your dependency manifest and install dependencies first, then copy your source code. Editing a source file then reuses the cached dependency layer instead of reinstalling everything.

A multi-stage build uses one stage to compile or install, and copies only the results into a small final image. Compilers, dev dependencies and source maps stay behind.

A build stage installs and compiles, and only the output is copied into a slim runtime image
Multi-stage builds keep build tools out of the image you deploy.

Step by step: a Dockerfile for a Node app

This example assumes a Node.js 22 app with a build script that compiles TypeScript into dist/, and a server that listens on 0.0.0.0 and process.env.PORT. If you don't have a build step, drop the build stage and copy your source instead.

  1. Create Dockerfile at the top of the repository:
Dockerfile
FROM node:22-bookworm-slim AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev

FROM node:22-bookworm-slim AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:22-bookworm-slim
WORKDIR /app
RUN apt-get update \
 && apt-get install -y --no-install-recommends curl \
 && rm -rf /var/lib/apt/lists/*
ENV NODE_ENV=production PORT=3000
COPY --from=deps --chown=node:node /app/node_modules ./node_modules
COPY --from=build --chown=node:node /app/dist ./dist
COPY --chown=node:node package.json ./
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]
  1. Create .dockerignore next to it:
Text
.git
node_modules
dist
.env
.env.*
!.env.example
npm-debug.log
Dockerfile
.dockerignore
  1. Build and run it locally, then check the health route:
Terminal
docker build -t example-app .
docker run --rm -p 3000:3000 example-app
curl -i http://localhost:3000/health

A few details are deliberate. npm ci installs exactly what the lockfile says. The production stage only gets runtime dependencies. The official Node images include an unprivileged node user, so the app doesn't run as root. CMD uses the exec form (a JSON array) and runs node directly rather than through npm start, so the process receives SIGTERM itself and can shut down gracefully.

Step by step: a Dockerfile for a Python app

This one runs a FastAPI app with uvicorn on Python 3.12. The same shape works for Django or Flask with gunicorn.

  1. Create Dockerfile:
Dockerfile
FROM python:3.12-slim-bookworm
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PORT=8000
WORKDIR /app
RUN apt-get update \
 && apt-get install -y --no-install-recommends curl \
 && rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
RUN useradd --create-home --uid 10001 appuser
USER appuser
EXPOSE 8000
CMD ["sh", "-c", "exec uvicorn main:app --host 0.0.0.0 --port ${PORT}"]
  1. Add a .dockerignore with .git, .venv, __pycache__, .env and *.pyc.

The exec form can't expand variables by itself, so the CMD starts a shell to read PORT, and exec replaces that shell with uvicorn so stop signals reach the server. PYTHONUNBUFFERED=1 makes log lines appear immediately instead of sitting in a buffer.

Local development with Docker Compose

Compose runs your app and its database together. Save this as compose.yaml. The top-level version: key is obsolete and no longer needed:

YAML
services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      PORT: "3000"
      DATABASE_URL: postgres://app:app@db:5432/app
    depends_on:
      db:
        condition: service_healthy

  db:
    image: postgres:17
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: app
      POSTGRES_DB: app
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app -d app"]
      interval: 5s
      timeout: 3s
      retries: 10

volumes:
  pgdata:

Run docker compose up --build. The app reaches the database at the host name db, the service name. The named volume keeps data between restarts; docker compose down -v deletes it. These credentials are for local use only.

HEALTHCHECK and platform health checks

Docker has a HEALTHCHECK instruction that Docker itself and Compose use to mark a container healthy. Hosting platforms usually run their own check instead. On Launchpad, the check is the Health check path under the app's Networking tab, and a deploy only goes live once that path answers with a 2xx status. You don't need a HEALTHCHECK line for it.

Launchpad calls the path from inside the container, which is why both Dockerfiles above install curl. Slim images don't include it.

Keeping images small and predictable

  • Start from slim images. -slim variants leave out compilers and documentation you rarely need at runtime.
  • Pin base images. node:22-bookworm-slim fixes the major version and the Debian release. For fully repeatable builds, pin an exact version or an image digest, and update it on purpose.
  • Clean up in the same RUN. Deleting files in a later layer doesn't shrink the earlier one, so remove apt lists in the instruction that created them.
  • Use multi-stage builds so build tools never reach the final image.
  • Keep the build context small with .dockerignore. Sending node_modules or .git to the builder slows every build.

How Launchpad uses your Dockerfile

On GitHub deploys, a Dockerfile in the repository wins: Launchpad builds it instead of detecting a framework. The port comes from the EXPOSE line; without one, port 3000 is assumed. Launchpad also sets the PORT variable to that port, so reading PORT keeps the two in step. Under Settings → Build and run you can change the Dockerfile path and Root directory, which helps in monorepos.

Zip uploads work differently. They ignore a Dockerfile and build the project the usual way if they recognise it, and a project that only works with its Dockerfile can't be built from an upload. Deploy those from GitHub.

Common mistakes

  • Copying source before installing dependencies. Every code change reinstalls everything. Fix: copy the manifest and lockfile first.
  • Listening on 127.0.0.1. Nothing outside the container can connect. Fix: listen on 0.0.0.0.
  • CMD npm start in shell form. Signals go to a shell, not your app, so shutdowns are abrupt. Fix: exec form, ideally running node directly.
  • Running as root. A compromised app gets root inside the container. Fix: USER node or a user you create.
  • Using latest tags. Builds change underneath you. Fix: pin versions.

Security notes

  • Never COPY a .env file into an image, and never put secrets in ENV or ARG. Anyone with the image can read them from its layers and history. Pass secrets as runtime variables; on Launchpad, use the Variables tab.
  • If a build genuinely needs a secret, such as a private package token, use a BuildKit secret mount (RUN --mount=type=secret,...), which doesn't persist in the image.
  • Rebuild regularly so base image security fixes reach you, and scan images with a tool such as docker scout or Trivy.

Troubleshooting

SymptomCauseFix
Deploy fails the health checkWrong port, app bound to localhost, or no curl in the imageMatch EXPOSE to the app's port, bind 0.0.0.0, install curl
npm ci fails with a lockfile errorpackage-lock.json missing or out of dateRun npm install locally and commit the lockfile
Builds are slow every timeSource copied before dependencies, or a large build contextReorder layers; extend .dockerignore
EACCES: permission denied at runtimeFiles owned by root, app runs as nodeUse COPY --chown=node:node
Upload says it can't be built from a DockerfileThe project only builds with DockerDeploy it from GitHub

Frequently asked questions

Do I need Docker to deploy on Launchpad?

No. Launchpad detects Node, Python, PHP and static sites without one. A Dockerfile is for when you want full control of the build.

Alpine or Debian slim?

Both work. Alpine is smaller but uses musl instead of glibc, which occasionally breaks packages with native code. Debian slim is the safer default.

Can Launchpad run my compose.yaml?

No. Compose is for local development. On Launchpad, each service is an app, and databases are created under the app's Databases tab.

Next steps

You now know why layer order matters, how to keep images small and free of secrets, and how Launchpad reads your Dockerfile.

Share this articleLinkedIn (opens in a new tab)X (opens in a new tab)Facebook (opens in a new tab)
Was this article helpful?

Get new guides in your inbox.

One email a month. Unsubscribe anytime.