Selected topic
Deployment
Prefer practical output? Use related tools below while reading.
In this summary, we'll cover the basics of Docker and its use in deploying an Express.js application.
### What is Docker?
Docker is a containerization platform that allows developers to package, ship, and run applications in containers. Containers are lightweight and portable, ensuring that applications work consistently across environments.
To containerize an Express.js application using Docker:
Dockerfile:dockerfile
# Use the official Node.js 14 image as a base
FROM node:14# Set the working directory in the container to /app
WORKDIR /app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy application code
COPY . .
# Expose port 3000 (the default Express.js port)
EXPOSE 3000
# Run command when container starts
CMD ["node", "server.js"]
bash
docker build -t my-express-app .bash
docker run -p 3000:3000 my-express-appmy-express-app image, mapping port 3000 on the host machine to port 3000 in the container.### Example Use Case
Let's say you have an Express.js application in a directory called express-app. Inside this directory, you'll create a Dockerfile with the contents above. Then:
docker build -t my-express-app . to create a Docker image.docker run -p 3000:3000 my-express-app to start a new container from that image.