What is Docker?
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.
Step 1: Create a Dockerfile
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
# Use an official Node.js runtime as a parent image
FROM node:18-alpine
# Set the working directory
WORKDIR /app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install app dependencies
RUN npm install
# Bundle app source
COPY . .
# Your app binds to port 3000, so you'll use the EXPOSE instruction
EXPOSE 3000
# Define the command to run your app
CMD [ "npm", "start" ]
Step 2: Build the Docker Image
Navigate to your project directory in the terminal and run the following command:
docker build -t my-node-app .
Step 3: Run the Docker Container
Once the image is built, you can run it as a container:
docker run -p 4000:3000 my-node-app
Now, you can access your application at http://localhost:4000.