Build Nodejs API with Redis, MongoDB in Docker

ifeanyichima

Ifeanyi Chima

Posted on April 1, 2024

Build Nodejs API with Redis, MongoDB in Docker

The benefits of docker can not be understated as it facilitates software development regardless of whatever platform a developer is using.

I built an API using Nodejs, Redis and MongoDB and I wanted to containerise the API using Docker.

Here are the processes or steps, I followed to be able to containerise the API. Here is a Github link to the API.

Redis

Change the API code

We need to change our nodejs API code to configure the redis client to be able to connect to a redis server running in the docker container.




// src/index.js

export const client = createClient({
    socket: {
        port: 6379,
        host: "redis"
    }
});



Enter fullscreen mode Exit fullscreen mode

Download a redis image:



docker pull redis


Enter fullscreen mode Exit fullscreen mode

Create a container: We are going to create a redis container from the image we just downloaded.

Port-forwarding, when you want to create a container remember to implement port-forwarding because when you have a container running, the ports you declare in your code while running in the container will only refer to the port of the container and not the host (local) machine where docker is running on.



docker run -d -p 6379:6379 --name redisCont redis


Enter fullscreen mode Exit fullscreen mode

Image description

MongoDB

We will repeat the same process for mongoDB.

Change the API code:




export const connectDB = async () => {
    try {
        await mongoose.connect('mongodb://mongo/instaCartDB', { // ->this line
            useUnifiedTopology: true,
            useNewUrlParser: true
        }) 
    } catch (error) {
        console.error(error)
    }
}



Enter fullscreen mode Exit fullscreen mode

note

We change the initial hostname (localhost or 127.0.0.1) to mongo in the connection URI, this is to allow the DNS resolve to the appropriate container on docker.

Download a mongoDB image: for some reason, mongoDB version 5.0 docker image was throwing an error of WARNING: MongoDB 5.0+ requires a CPU with AVX support, and your current system does not appear to have that!.

Hence, I had to downgrade to mongoDB version 4.4.6 docker image.



docker pull mongo:4.4.6


Enter fullscreen mode Exit fullscreen mode

Create a Container: We are going to create a mongoDB container from the image we just downloaded. Also, remember to implement port-forwarding here as well.



docker run -d -p 27017:27017 --name mongodbCont mongo:4.4.6


Enter fullscreen mode Exit fullscreen mode

Image description

Dockerfile

So, after building the API I created a dockerfile and then I built a docker image using the following syntax.



FROM node:20-alpine

LABEL maintainer="chima@gmail.com"

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3500

CMD ["node", "index.js"]

Enter fullscreen mode Exit fullscreen mode


docker build -t prod-API-image .

Enter fullscreen mode Exit fullscreen mode




Finally, running containers

You have to link your redis container and mongoDB to your Nodejs API using docker.



docker run -d --link redisCont:redis --link mongodbCont:mongo -p 4000:4000 --name my-nodejs-api prod-API-image

Enter fullscreen mode Exit fullscreen mode




References

How to dockerize a nodejs api

Getting started with docker

Dockerize a Node.js app connected to MongoDb

Buy Me A Coffee

Thank you, Please follow me

HTML GitHub

💖 💪 🙅 🚩
ifeanyichima
Ifeanyi Chima

Posted on April 1, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related