6 min read

[box type=”note” align=”” class=”” width=””]This article is an excerpt from a book written by Shahid Shaikh titled Mastering RethinkDB. This book will help you develop efficient and real-time applications in RethinkDB with ease.[/box]

In today’s tutorial, we will learn to install Docker, create an Docker image and deploy RethinkDB using Docker.

Your code is not working in Production? But it’s working on the QA (quality analysis server)!

I am sure you have heard statements like these in your team during the deployment phase. Well no more of that, Docker everything and forget about the infrastructure of different environments, say, QA, Staging and Production, because your code is going to run Docker container not in those machines, hence write once, run everywhere. In this section, we will learn how to use Docker to deploy a RethinkDB Server or PaaS services. I am going to cover a few docker basics too; if you are already aware of them, please skip to the next section.

Installing Docker

Docker is available for all major platforms, such as, Linux-based distributions, Mac, and Windows. Visit the official website at h t t p s ://w w w . d o c k e r . c o m / and download the package suitable for your platform.

We are installing Docker in our machine to create a new Docker image. Docker images are independent of platform and should not be confused with Docker for Mac or Docker for Windows. It’s referred to as a Docker client too.

Once you have installed the Docker, you need to start the Daemon process first; I am using a Mac so I can view this in the launchpad, as shown here:

RethinkDB in Docker

Upon clicking that, it will open up a nice console showing the Docker official logo and an indication that Docker is successfully booted, as shown in the following screenshot:

RethinkDB in Docker

Now we can begin creating our Docker image that in turn will run RethinkDB.

Creating a Docker image

For installing our RethinkDB on Ubuntu inside the Docker, we need to install the Ubuntu operating system. Run the following command to install a Ubuntu image from the official Docker hub repository:

docker pull ubuntu

This will download and install the Ubuntu image in our system. We will later use this Ubuntu image and install our RethinkDB instance; you can choose different operating systems as well.

Before going to the Docker configuration code, I would like to point out the steps we require to install RethinkDB on a fresh Ubuntu installation:

  • Update the system
  • Add the RethinkDB repository to the known repository list
  • Install RethinkDB
  • Set the data folder
  • Expose the port

We are going to do this using Docker. To create a Docker image, we require DockerfileCreate a file called Dockerfile with no extension and apply the code shown here:

FROM ubuntu:latest

# Install RethinkDB.

RUN 

apt-get update && 

echo "deb http://download.rethinkdb.com/apt `lsb_release -cs` main" >

/etc/apt/sources.list.d/rethinkdb.list && 

apt-get install -y wget && 

wget -O- http://download.rethinkdb.com/apt/pubkey.gpg | apt-key add - &&



apt-get update && 

apt-get install -y rethinkdb python-pip && 

rm -rf /var/lib/apt/lists/*

# Install python driver for rethinkdb

RUN pip install rethinkdb

# Define mountable directories.

VOLUME ["/data"]

# Define working directory.

WORKDIR /data

# Define default command.

CMD ["rethinkdb", "--bind", "all"]

# Expose ports.

# - 8080: web UI

# - 28015: process

# - 29015: cluster

EXPOSE 8080

EXPOSE 28015

EXPOSE 29015

The first line is our entry point to the Ubuntu operating system, then we are performing an
update of the system and using the installation commands recommended by RethinkDB
here: h t t p s ://w w w . r e t h i n k d b . c o m /d o c s /i n s t a l l /u b u n t u /.

Once the installation is complete, we install the rethinkdb python driver to perform the import/export operation. The next two commands mount a new volume in Ubuntu and telling RethinkDB to use that volume. The next command runs rethinkdb by binding all the ports and exposing the ports to be used by the client driver and web console. In order to make this a docker image, save the file and run the following command within the project directory:
docker build -t docker-rethinkdb.

Here, we are building our docker image and giving it a name docker-rethinkdb; upon
running this command, Docker will execute the Dockerfile and you’re on.

The representation of the previous steps is shown here:

RethinkDB in Docker

Once everything works, and I am sure it will, you will see a success message in the console, as shown here:

RethinkDB in Docker

Congratulations! You have successfully created a docker image for RethinkDB. If you want to see your image and its properties, run the following command:

docker images

And this will list all the images of Docker, as shown in the following screenshot:

RethinkDB in Docker

Awesome! Now let’s run it.

To access the web portal, we need to run our docker image and bind port 8080 of the docker image to some port of our machine; here is the command to do so:

docker run -p 3000:8080 -d docker-rethinkdb

As per the command above, -p is used to specify port binding, the first is the target and second port is source, that is, Docker port and -d is used to run it in the background or Daemon.

This will run the docker image in the background; to extract more information about this process, we need to run the following command:

docker ps

This will list all the running images called as a container, along with the information, as shown in the following screenshot:

RethinkDB in Docker

You can also check the logs of specific containers using the following command:

docker logs <container id>

Now, in order to access the RethinkDB web console from our machine, we need to find out the IP address on which the Docker machine is running. To get that, we need to run the following command:

docker-machine ip default

This will print out the IP. Copy the IP and hit IP:3000 from the browser to view the RethinkDB web console, as shown here:

ReThinkDB in Docker

So we have docker running and accessible from the browser. In order to import and export the data, we need to log in to our Docker image. To do that, run the following command:

docker exec -i -t <container-id> /bin/bash

This will log in to the docker image running Ubuntu; refer to the following screenshot:

RethinkDB in Docker

You can now run the rethinkdb command to perform the data import to the existing RethinkDB cluster.

Deploying the Docker image

Almost every PaaS service we have covered in earlier sections provides support for Docker. You can submit your Dockerfile to git and clone it anywhere if you want to create Docker image.

You can submit the whole docker image (not Dockerfile) to Dockerhub and pull your docker image directly using the docker pull command, which is no doubt an easy way because you will be directly working on the image running on the server.

We covered RethinkDB deployment using Docker and learned how to create our own RethinkDB image.

You can learn more about RethinkDB Query Language and Performance Tuning in RethinkDB from this book Mastering RethinkDB.

Mastering RethinkDB

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here