Wednesday, April 18, 2018

Initialize MongoDB container with Rancher



mongodb-config

|_Dockerfile
|_init
       |_create_db_and_user.js

Dockerfile
FROM dockernes.nes.com/docker.io/alpine:3.5
VOLUME /docker-entrypoint-initdb.d
COPY init /docker-entrypoint-initdb.d

create_db_and_user.js
db = db.getSiblingDB('sonarqube-rep-db');
db.createUser(
   {
     user: "user",
     pwd: "password",
     roles: [ { role: "readWrite", db:"sonarqube-rep-db" }]
   }
);


Use Jenkins to build the docker container from the previous Dockerfile and to push it to Artifactory.

The mongodb-config container is then used by Rancher when the following script run to create a Mongodb container for user 'user'.

Rancher deployment file
version: '2'
services:
  mongodb:
    image: mongo:3
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: password
    ports:
      - "27017:27017"
    labels:
      io.rancher.container.pull_image: always
      io.rancher.sidekicks: config
    volumes:
      - mongodb:/data/db
    volumes_from:
      - config
  config:
    image: dockernes.nes.com/mongodb-config:latest
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.start_once: true

Rancher will sidekick the mongodb config to create a user for that DB. Authentication has been required by the declaration of MONGO_INITDB_ROOT_USERNAME: admin

The running mongodb container will look like this in Rancher:




1 comment:

  1. It’s a wonderful blog you shared here, I read the whole content and found it so easy to understand in language and is also full of amazing and useful information. Thanks a lot for sharing it.
    Hire MongoDB Developer India

    ReplyDelete