Guides/Server Setup
Server Setup8 min read

How to Dockerize a Node.js Application

Package your Node.js app into a Docker container — write a production-ready Dockerfile, build the image, run it locally, and push it to Docker Hub or a private registry.

Write the Dockerfile

Create a Dockerfile in your project root: FROM node:20-alpine — WORKDIR /app — COPY package*.json ./ — RUN npm ci --only=production — COPY . . — EXPOSE 3000 — CMD ["node", "server.js"]. Use node:20-alpine (not node:20) for a smaller image — Alpine Linux images are ~5MB vs ~350MB for the full Debian image. Use npm ci instead of npm install for deterministic dependency installs.

Add a .dockerignore File

Create .dockerignore: node_modules — .env — .env.local — .git — *.log — .next (if Next.js). This prevents copying node_modules (which gets reinstalled inside the container anyway), sensitive env files, and build artifacts into the image. Without .dockerignore, your image will be much larger and may accidentally contain secrets.

Build and Run the Image

Build: docker build -t myapp:latest .. Run: docker run -d -p 3000:3000 --env-file .env.local --name myapp myapp:latest. The -p flag maps host port to container port. The --env-file flag passes environment variables from a local file without baking them into the image. View logs: docker logs myapp -f.

Use Multi-Stage Builds for Next.js

For Next.js apps, use a multi-stage build: stage 1 (builder) installs all deps and runs npm run build, stage 2 (runner) copies only the .next/standalone output. This produces images 3-5x smaller than naive Dockerfiles. In next.config.js, set output: "standalone" to enable the standalone build. The final image includes only the Node server and compiled pages.

Push to Docker Hub or a Registry

Tag your image: docker tag myapp:latest yourusername/myapp:latest. Login: docker login. Push: docker push yourusername/myapp:latest. For private registries (AWS ECR, GitHub Container Registry, DigitalOcean), authenticate per their respective CLI tools. Once pushed, pull and run the image on any server: docker pull yourusername/myapp:latest && docker run -d -p 3000:3000 yourusername/myapp:latest.

Need Help?

Want this done for you?

Our engineering team handles implementations like this every week. Get a free scoping call — we will tell you exactly what it takes and what it costs.

Book a free call

© 2026 NexWorldTech — Built for Global Dominance.