How to Deploy Express Using Docker: Simple Guide
To deploy an Express app using
Docker, create a Dockerfile that sets up Node.js, copies your app files, installs dependencies, and exposes the port. Then build the Docker image with docker build and run it using docker run to start your Express server inside a container.Syntax
A typical Dockerfile for Express deployment includes these steps:
- FROM: Specifies the base image, usually Node.js.
- WORKDIR: Sets the working directory inside the container.
- COPY: Copies your app files into the container.
- RUN: Runs commands like
npm installto install dependencies. - EXPOSE: Opens the port your app listens on.
- CMD: Defines the command to start your app.
Dockerfile
FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "index.js"]
Example
This example shows a simple Express app Docker deployment. It uses Node.js 18 Alpine image, installs dependencies, copies app files, exposes port 3000, and runs index.js.
plaintext
/* index.js */ import express from 'express'; const app = express(); const PORT = 3000; app.get('/', (req, res) => { res.send('Hello from Express in Docker!'); }); app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); }); /* package.json */ { "name": "express-docker", "version": "1.0.0", "type": "module", "scripts": { "start": "node index.js" }, "dependencies": { "express": "^4.18.2" } } /* Dockerfile */ FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "index.js"]
Output
Server running on port 3000
When you visit http://localhost:3000 in your browser, you see: Hello from Express in Docker!
Common Pitfalls
Common mistakes when deploying Express with Docker include:
- Not copying
package.jsonandpackage-lock.jsonbefore runningnpm install, causing rebuilds to be slow. - Forgetting to expose the correct port with
EXPOSE. - Not setting
WORKDIR, leading to files copied in wrong places. - Using
CMDincorrectly, causing the container to exit immediately.
Dockerfile
/* Wrong Dockerfile snippet */ FROM node:18-alpine COPY . . RUN npm install CMD ["node", "index.js"] /* Right Dockerfile snippet */ FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "index.js"]
Quick Reference
Remember these quick tips for deploying Express with Docker:
- Use a lightweight Node.js base image like
node:18-alpine. - Set
WORKDIRto keep files organized. - Copy
package.jsonfiles first to leverage Docker cache. - Expose the port your app listens on (default 3000).
- Use
CMDto start your app withnode index.js.
Key Takeaways
Create a Dockerfile that installs dependencies and runs your Express app inside a container.
Use lightweight Node.js images and set WORKDIR for clean builds.
Copy package files before installing to speed up Docker builds.
Expose the correct port and use CMD to start the server.
Test your container locally with docker run before deploying.