DEV Community

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

Posted on

2

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more