DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Abdolah Keshtkar
Abdolah Keshtkar

Posted on

Couchbase Node SDK on Docker

Hey guys,
I have been working on a project using Remix (Node image), Couchbase server, etc. All dockerized.

The problem was to connect to Couchbase, I needed its Node SDK which is current version is 4.2.0 but I hit multiple errors in the way.

I didn't find any instruction about it on the Couchbase website, so I decided to write one.

In the start, your frontend Dockerfile may look like this:

FROM node:18

WORKDIR /app

COPY package.json .
COPY package-lock.json .

RUN npm --verbose ci --force

COPY . .

RUN npm --verbose run build
Enter fullscreen mode Exit fullscreen mode

Wanting Couchbase SDK to be on the project, needs multiple things to be fixed:
First, I can not use node 18, I need node 16.
Second, I need to have cmake.

To do so first let's change the base image and downgrade it to node 16:

FROM node:16
Enter fullscreen mode Exit fullscreen mode

Then we need to pull cmake installation script, you can find them here: https://cmake.org/download/

IMPORTANT: Choose from "Binary distributions" section.

Then here you have two options:
First, download the file and place it beside the docker file to copy it and run it in the Dockerfile
Or, using wget, you can download it inside the Docker build process and run it.

First Option

For the first option, after placing the .sh file beside the docker file:

You need to copy it and run it inside the docker file:

WORKDIR /cmake

COPY cmake-3.25.0-rc4-linux-x86_64.sh ./

RUN ./cmake-3.25.0-rc4-linux-x86_64.sh --skip-license && rm cmake-3.25.0-rc4-linux-x86_64.sh
Enter fullscreen mode Exit fullscreen mode

In the third line, we got rid of it after installing cmake.

Now we have it installed under /cmake/bin folder.

Second option

We need to have wget installed and then download and run the file like the following:

RUN apt update && apt install -y g++ wget bash

WORKDIR /cmake

RUN wget https://github.com/Kitware/CMake/releases/download/v3.25.0-rc4/cmake-3.25.0-rc4-linux-x86_64.sh

RUN ./cmake-3.25.0-rc4-linux-x86_64.sh --skip-license && rm cmake-3.25.0-rc4-linux-x86_64.sh
Enter fullscreen mode Exit fullscreen mode

Adding to the path

Cmake.js which is a library that Couchbase needs cmake, so to make it happen, add the following line:

ENV PATH="$PATH:/cmake/bin" 
Enter fullscreen mode Exit fullscreen mode

Final result

FROM node:16

WORKDIR /cmake

COPY cmake-3.25.0-rc4-linux-x86_64.sh ./

# OR

# RUN apt update && apt install -y g++ wget bash
# RUN wget https://github.com/Kitware/CMake/releases/download/v3.25.0-rc4/cmake-3.25.0-rc4-linux-x86_64.sh


RUN ./cmake-3.25.0-rc4-linux-x86_64.sh --skip-license && rm cmake-3.25.0-rc4-linux-x86_64.sh

ENV PATH="$PATH:/cmake/bin" 

WORKDIR /app

RUN npm i couchbase@4.2.0

COPY package.json .
COPY package-lock.json .

RUN npm --verbose i 

COPY . .

RUN npm --verbose run build
Enter fullscreen mode Exit fullscreen mode

Note: I separated Couchbase installing because it takes so much time to be installed but also "package.json" and "package-lock.json" changes more frequently, this way, whenever "package.json" gets updated, we won't need to reinstall Couchbase and we will use the cached layer.

Note: Couchbase takes so much time to be installed, on my system, it toke me 35 minutes :)) so let's use everything we have to avoid reinstalling again and again which this is what I did. (On the server, it took 10 mins)

Top comments (0)

Hey 😍

Want to help the DEV Community feel more like a community?

Head over to the Welcome Thread and greet some new community members!

It only takes a minute of your time, and goes a long way!