November 6, 2017 · openkb mongodb Docker dockerfile

openKB + mongoDB with Docker

openKB is a "Open Source Nodejs Markdown based knowledge base (FAQ) app". I will demonstrate how to run openKB with Docker and also how to change from the embedded database to a mongoDB, also running in Docker.

I will use a standard CentOS 7 as the Docker host but this approach will work on any Linux-based Docker host.

There is a Dockerfile provided by the openKB project but I have made some adjustments so we will be using "my" Dockerfile.

Start by creating a new directory that we will be using to build our own openkb Docker image.

mkdir ~/openkb && cd ~/openkb

Create a new Dockerfile
vim Dockerfile

And paste the following

FROM alpine:3.6  
MAINTAINER dodgydudes  
RUN apk add --update nodejs nodejs-npm git && git clone /var/openKB  
WORKDIR /var/openKB  
RUN npm install  
VOLUME /var/openKB/public/uploads/  
VOLUME /var/openKB/config  
EXPOSE 4444  
ENTRYPOINT ["npm", "start"]  

Build the Docker image
docker build -t openkb .

As we want the content in openKB to be persistent we need to create the following directories
mkdir -p /docker/openkb/config /docker/openkb/data /docker/openkb/upload

Feel free to change the directories to fit your needs and environment.

Fetch a copy of the config file and put it in the config directory
wget -O /docker/openkb/config/config.json

Modify the config.json to use mongoDB instead of the embedded db. Specify the user and password of the user that will be used to connect to the mongoDB. We will soon create the actual user in mongoDB
vim /docker/openkb/config/config.json

"database": {
    "type": "mongodb",
    "connection_string": "mongodb://<user>:<pass>@mongo:27017/openkb"

If you are using RHEL/CentOS as the Docker host you need to apply the following command as per the mongoDB documentation or else SELinux will give it a hard time
chcon -Rt svirt_sandbox_file_t /docker/openkb/data

In order to create the user that we will use to connect to the mongoDB we first need to start the mongoDB docker container
docker run --name mongo -v /docker/openkb/data:/data/db --rm -d mongo --auth

Notice the --auth at the end, this tells mongodb to run with authentication enabled.

When the mongoDB container has started we connect to it with the following command
docker exec -it mongo mongo admin

First of we need to create the initial admin account which we then will use to create the new user.

db.createUser({ user: '<some-admin-user>', pwd: '<some-admin-password>', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });  

Type exit to exit the mongoDB container.

Connect to the mongoDB container with the newly created admin user account

docker exec -it mongo mongo -u "<some-admin-user>" -p "<some-admin-pass>" --authenticationDatabase "admin"  

Switch from the "admin" db to the "openkb" db
use openkb

And create the new user with the same username and password as you typed in to the config.json file

db.createUser({ user: '<user>', pwd: '<pass>', roles: [ { role: "readWrite", db: "openkb" } ] });  

Type exit to exit the mongoDB container.

Now we are ready to try the database connection. We will use --link to create a connection between the openkb container and the mongodb container in order to save the data in mongoDB

docker run --name openkb --link mongo:mongo -itd --rm -p 4444:4444 -v /docker/openkb/config:/var/openKB/config -v /docker/openkb/uploads:/var/openKB/public/uploads openkb  

Open port 4444/tcp on your host if it is blocked by a firewall

firewall-cmd --zone=public --permanent --add-port=4444/tcp  
firewall-cmd --reload  

Then verify access to the openKB website, either via your browser or via curl
curl localhost:4444

If you want to run openKB and mongoDB with docker-compose I have made a docker-compose.yml that you can use. Create a new file called docker-compose.yml and paste the following

version: "2"  
   image: mongo:latest
   restart: always
     - /docker/openkb/data:/data/db
   command: mongod --auth
     - mongo
    restart: always
    image: openkb
     - "4444:4444"
     - /docker/openkb/uploads:/var/openKB/public/uploads
     - /docker/openkb/config:/var/openKB/config
     - mongo:mongo

Stop mongoDB and openKB if they are already running
docker stop openkb && docker stop mongo

Then we can start them again using the docker-compose file
docker-compose -f /path/to/docker-compose.yml up -d