Dockerizing a web application makes its runtime easier to reproduce across development, testing, and deployment—but only when the image, configuration, networking, and operational checks are designed together. This guide provides a reusable workflow for choosing a base image, writing a Dockerfile, testing a container, connecting services with Compose, and checking production readiness before release.
Overview
To containerize a web application reliably, treat the container image as a build artifact rather than a substitute for application design. The image should contain the code and runtime dependencies required to start the service. Configuration, secrets, persistent data, and environment-specific values should usually be supplied at runtime.
A practical workflow has six stages:
- Identify how the application is built and started.
- Choose a supported, appropriately sized base image.
- Write a Dockerfile that installs dependencies efficiently and runs as safely as possible.
- Build and run the image locally.
- Test ports, environment variables, health behavior, and service-to-service networking.
- Publish and deploy the image using a repeatable release process.
The exact commands vary by language and hosting platform, but the checks remain similar. A Node application, for example, may build with npm run build and start with npm start. A Python service may install requirements and start a WSGI or ASGI server. Before writing the Dockerfile, document the application’s build command, start command, listening port, required environment variables, and writable directories.
Keep the container boundary clear. A web process should generally handle web traffic, while a database, cache, or background worker should have its own service when the architecture calls for one. This separation makes individual components easier to build, test, scale, and replace.
Checklist by scenario
Scenario 1: Containerize a development application
- Create a
.dockerignorefile that excludes dependency directories, build output, local environment files, logs, and version-control metadata. - Choose a base image that matches the application’s runtime and dependency requirements.
- Copy dependency manifests before the rest of the source code so dependency installation can be cached when the manifests have not changed.
- Expose the application’s listening port for documentation and local tooling. Remember that
EXPOSEdoes not publish the port by itself. - Use a development command only when the image is explicitly intended for development, such as a command that watches mounted source files.
- Bind the development server to
0.0.0.0inside the container when the framework otherwise listens only on localhost.
Scenario 2: Build a production image
- Use a multi-stage Dockerfile when build tools, compilers, or development dependencies are not needed at runtime.
- Install only the dependencies required by the production process.
- Run the service as a non-root user when the base image and application support it.
- Make the startup command explicit and ensure the process remains in the foreground.
- Pass configuration through runtime environment variables or a platform-managed configuration system.
- Build with a deliberate image tag, preferably tied to a commit or release identifier rather than only a mutable label.
Scenario 3: Run an application with Docker Compose
Compose is useful for local multi-service workflows. A minimal example might connect an application to a database without hard-coding service addresses:
services:
app:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://app:password@db:5432/app
depends_on:
- db
db:
image: postgres:latest
environment:
POSTGRES_DB: app
POSTGRES_USER: app
POSTGRES_PASSWORD: password
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
Use this as a development pattern, not a complete production configuration. The application reaches the database through the service name db, not localhost. For real deployments, handle credentials securely, pin images according to your update policy, protect persistent data, and confirm that the platform’s networking and storage behavior match your needs.
Dockerfile pattern comparison
| Pattern | Best fit | Main advantage | Watch for |
|---|---|---|---|
| Single-stage image | Small prototypes and simple services | Easy to understand and maintain | Build tools and development files may remain in the final image |
| Multi-stage build | Compiled apps and frontend bundles | Keeps build dependencies out of the runtime image | Copy paths and runtime assets must be verified carefully |
| Development image with bind mounts | Local iteration and hot reload | Source changes appear without rebuilding | Host files can hide files created during the image build |
| Distroless or minimal runtime | Services with a well-understood runtime | Fewer unnecessary components in production | Interactive debugging and shell-based diagnostics may be limited |
Example multi-stage Dockerfile for a Node application
FROM node:22 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:22-slim AS runtime
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]
Adjust the Node version, build output, start command, and user configuration to your project. If the application does not compile to a dist directory, copy the files it actually needs. The important pattern is to separate the build environment from the runtime environment and to make the final command unambiguous.
What to double-check
Environment variables and secrets
- List required variables in documentation or an example environment file that contains placeholders, not real credentials.
- Do not copy
.envfiles containing secrets into an image. - Confirm whether variables are needed during the build, at startup, or only by a particular request path.
- Validate required configuration early so a misconfigured container fails clearly rather than serving partial or unsafe responses.
Configuration debugging is easier when values are checked systematically. For API payloads, the JSON formatting and validation guide can help distinguish malformed data from an application or network problem. Authentication issues may require inspecting claims and expiration without exposing tokens; use the JWT debugging guide for a safer troubleshooting workflow.
Image size and build repeatability
- Review the build context and remove files that do not belong in it.
- Use layer ordering that preserves useful dependency-installation cache layers.
- Prefer a runtime image that contains only the files and packages the service needs.
- Review dependencies periodically and remove unused packages.
- Use a lockfile and a repeatable installation command.
Health checks and process behavior
A published port only indicates how traffic may be routed; it does not prove that the application is ready. Define a health endpoint or equivalent check that verifies the service can handle its intended work. Decide whether the check should test only process availability or also dependencies such as a database. A deep dependency check can be useful for readiness but may be inappropriate for a simple liveness check.
Also verify graceful shutdown. The main process should receive termination signals, stop accepting new work, finish or cancel active work according to the application’s policy, and exit within the platform’s termination window.
Networking and persistence
- Use container or service names for internal communication instead of assuming every service runs on localhost.
- Confirm the host-to-container port mapping before testing from a browser.
- Store persistent database data in a managed volume or external service rather than relying on a disposable container filesystem.
- Check outbound network access, DNS resolution, TLS certificate validation, and allowed origins where relevant.
Common mistakes
- Using
latesteverywhere. A moving image tag can make a later rebuild behave differently. Choose a versioning approach that lets you reproduce and roll back a known image. - Running the development server in production. Development servers often include reloaders, verbose diagnostics, or performance characteristics that are unsuitable for deployment. Use the application’s production server or runtime command.
- Putting secrets in Dockerfile instructions. Values included in build instructions or copied files may persist in image history or layers. Supply secrets through the deployment environment and review build logs.
- Assuming
depends_onmeans ready. Startup order is not the same as service readiness. Applications should retry connections or use an explicit readiness strategy where necessary. - Ignoring file ownership. A process that runs as a non-root user may be unable to write to a required directory. Create and permission only the directories the application genuinely needs.
- Skipping a clean-environment test. A container that works only because files, credentials, or services exist on the developer’s machine is not portable. Test with a clean build and explicitly supplied configuration.
Keep deployment changes reviewable. A small Dockerfile change can alter caching, permissions, startup behavior, or the available operating-system packages. Use a pull-request checklist such as this Git branching and pull request checklist to record what was changed and how it was tested.
When to revisit
Revisit the containerization checklist whenever you update the application runtime, package manager, base image, build tooling, or deployment platform. It is also worth reviewing before a seasonal planning cycle, a major release, a service split, or a move from local Compose workflows to a hosted environment.
At minimum, schedule a review when:
- the runtime or base image receives a major-version update;
- dependency installation or build commands change;
- the application adds a worker, database, queue, or new external integration;
- image size, startup time, memory usage, or restart behavior changes noticeably;
- security requirements or secret-management practices change;
- the deployment platform changes its networking, storage, or health-check configuration.
Before each release, run a clean image build, start the container with only documented configuration, exercise the health endpoint, test the required application paths, inspect logs, and confirm the image can be rolled back. Record the image identifier and the deployment configuration used. This creates a practical trail for debugging later.
Finally, update the Dockerfile and deployment documentation together. A container workflow is successful when another developer—or your future self—can build the same artifact, understand its required inputs, verify its health, and deploy it without relying on undocumented machine-specific steps.