DEV Community

Cover image for Setting up Express Backend API to use Docker
Tonny Kirwa
Tonny Kirwa

Posted on

Setting up Express Backend API to use Docker

To set up a Dockerfile for your Clarek CRM backend using Express.js with port 8000, follow the steps below:

  1. Create a Dockerfile:

Create a file named Dockerfile in the root directory of your Clarek CRM backend project.

# Use an official Node.js runtime as a base image
FROM node:20

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json to the container
COPY package*.json ./

# Install app dependencies
RUN npm install

# Copy the rest of the application code to the container
COPY . .

# Expose port 8000
EXPOSE 8000

# Define the command to run your application
CMD ["node", "app.js"]
Enter fullscreen mode Exit fullscreen mode

Noted that WORKDIR /usr/src/app becomes WORKDIR /clarek-crm-backend

  1. Build the Docker Image:

Open a terminal and navigate to the directory containing your Dockerfile and application code. Run the following command to build the Docker image:

docker build -t clarek-crm-backend .
Enter fullscreen mode Exit fullscreen mode

This command will build the Docker image using the instructions in the Dockerfile and tag it with the name clarek-crm-backend.

  1. Run the Docker Container:

After building the image, you can run the Docker container using the following command:

docker run -p 8000:8000 -d clarek-crm-backend
Enter fullscreen mode Exit fullscreen mode

This command maps port 8000 on your host machine to port 8000 in the Docker container and runs the container in detached mode (-d).

Now, your Clarek CRM backend should be accessible at http://localhost:8000.

Note: Make sure your Express.js application is configured to listen on port 8000. You can set the port in your Express.js application code or through a configuration file.

Remember to replace any placeholder names or values with your actual project details. Adjust the Node.js version in the FROM statement based on your project's compatibility.

Top comments (0)

Some comments have been hidden by the post's author - find out more