Couchbase Node SDK on Docker
Tags: couchbase, docker, node
Published: 2022-11-14 20:50:29

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:
```docker
FROM node:18

WORKDIR /app

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

RUN npm --verbose ci --force

COPY . .

RUN npm --verbose run build
```

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:

```docker
FROM node:16
```

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:

```docker
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
```

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:

```docker
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
```

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

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

## Final result

```docker
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
```

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)