Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Last active February 20, 2023 12:28
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x-yuri/32cd42ebd78d73f6d8f03de44b495210 to your computer and use it in GitHub Desktop.
Save x-yuri/32cd42ebd78d73f6d8f03de44b495210 to your computer and use it in GitHub Desktop.
docker + mongo + mongoid

The gist aims to show settings needed to run mongo in a docker container. Along with essential files come files needed to reproduce the case.

Table of contents

mongo-3.x + mongoid-5.x

Authentication is enabled if both MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD are specified. The app user is created in the destination database, so no need to specify auth_source (authentication database).

docker-compose.yml:

version: '3'

services:
  app:
    build: .
    env_file: .env.development
    networks:
      - app
    volumes:
      - ./:/app
    depends_on:
      - mongo

  mongo:
    image: mongo:3.6.16-xenial
    env_file: .env.development
    networks:
      - app
    volumes:
      - db:/data/db
      - ./init-mongo.sh:/docker-entrypoint-initdb.d/init-mongo.sh

networks:
  app:

volumes:
  db:

init-mongo.sh:

#!/usr/bin/env bash
set -eu
mongo -- "$MONGO_DB" <<EOF
    var rootUser = '$MONGO_INITDB_ROOT_USERNAME';
    var rootPassword = '$MONGO_INITDB_ROOT_PASSWORD';
    var admin = db.getSiblingDB('admin');
    admin.auth(rootUser, rootPassword);

    var user = '$MONGO_USER';
    var passwd = '${MONGO_PASSWORD-}' || user;
    db.createUser({user: user, pwd: passwd, roles: ["readWrite"]});
EOF

mongoid.yml:

development:
  clients:
    default:
      database: '<%= ENV["MONGO_DB"] %>'
      hosts:
        - '<%= ENV["MONGO_HOST"] %>:27017'
      options:
        user: '<%= ENV["MONGO_USER"] %>'
        password: '<%= ENV["MONGO_PASSWORD"] || ENV["MONGO_USER"] %>'

.env.development:

MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=rootpwd

MONGO_HOST=mongo
MONGO_USER=user
MONGO_PASSWORD=userpwd
MONGO_DB=db

The rest of the files:

Dockerfile:

FROM ruby:2.6.5-alpine3.10
RUN apk add build-base
WORKDIR /app
CMD ["./entrypoint.sh"]

entrypoint.sh:

#!/bin/sh
set -eu
bundle
exec ./entrypoint.rb

entrypoint.rb:

#!/usr/bin/env ruby
require 'bundler/setup'
require 'mongoid'
Mongoid.load!("mongoid.yml", :development)
class Person
  include Mongoid::Document
  field :first_name, type: String
end
Person.create(first_name: 'p1')

Gemfile:

source 'https://rubygems.org'
gem 'mongoid', '~> 5.0'

recreate.sh:

#!/usr/bin/env bash
set -eu
docker-compose down -v --rmi local
docker-compose pull
docker-compose build
docker-compose up
$ docker-compose exec mongo bash
# mongo -u user -p userpwd db
> db.people.find()
{ "_id" : ObjectId("5df96820a97ff80001573a3c"), "first_name" : "p1" }

mongo-3.x + mongoid-5.x (no auth)

docker-compose.yml:

version: '3'

services:
  app:
    build: .
    env_file: .env.development
    networks:
      - app
    volumes:
      - ./:/app
    depends_on:
      - mongo

  mongo:
    image: mongo:3.6.16-xenial
    env_file: .env.development
    networks:
      - app
    volumes:
      - db:/data/db

networks:
  app:

volumes:
  db:

mongoid.yml:

development:
  clients:
    default:
      database: '<%= ENV["MONGO_DB"] %>'
      hosts:
        - '<%= ENV["MONGO_HOST"] %>:27017'

.env.development:

MONGO_HOST=mongo
MONGO_DB=db

The rest of the files:

Dockerfile:

FROM ruby:2.6.5-alpine3.10
RUN apk add build-base
WORKDIR /app
CMD ["./entrypoint.sh"]

entrypoint.sh:

#!/bin/sh
set -eu
bundle
exec ./entrypoint.rb

entrypoint.rb:

#!/usr/bin/env ruby
require 'bundler/setup'
require 'mongoid'
Mongoid.load!("mongoid.yml", :development)
class Person
  include Mongoid::Document
  field :first_name, type: String
end
Person.create(first_name: 'p1')

Gemfile:

source 'https://rubygems.org'
gem 'mongoid', '~> 5.0'

recreate.sh:

#!/usr/bin/env bash
set -eu
docker-compose down -v --rmi local
docker-compose pull
docker-compose build
docker-compose up
$ docker-compose exec mongo bash
# mongo db
> db.people.find()
{ "_id" : ObjectId("5df96976a5f2e60001072c36"), "first_name" : "p1" }

mongo-2.x + mongoid-4.x (no auth)

mongoid-4.x doesn't work with mongo-3.x (can't authenticate). mongo-2.x image doesn't have means to enable authentication:

https://github.com/docker-library/mongo/blob/0ac2867637ef5989e4dc051efa0ae296010e58c9/2.6/docker-entrypoint.sh
https://github.com/docker-library/mongo/blob/ae499b5e6637539d21129f84ab636e0ec1816fc5/3.6/docker-entrypoint.sh#L199

docker-compose.yml:

version: '3'

services:
  app:
    build: .
    env_file: .env.development
    networks:
      - app
    volumes:
      - ./:/app
    depends_on:
      - mongo

  mongo:
    image: mongo:2.6.12
    env_file: .env.development
    networks:
      - app
    volumes:
      - db:/data/db

networks:
  app:

volumes:
  db:

mongoid.yml:

development:
  sessions:
    default:
      database: '<%= ENV["MONGO_DB"] %>'
      hosts:
        - '<%= ENV["MONGO_HOST"] %>:27017'

.env.development:

MONGO_HOST=mongo
MONGO_DB=db

The rest of the files:

Dockerfile:

FROM ruby:2.6.5-alpine3.10
RUN apk add build-base
WORKDIR /app
CMD ["./entrypoint.sh"]

entrypoint.sh:

#!/bin/sh
set -eu
bundle
exec ./entrypoint.rb

entrypoint.rb:

#!/usr/bin/env ruby
require 'bundler/setup'
require 'mongoid'
Mongoid.load!("mongoid.yml", :development)
class Person
  include Mongoid::Document
  field :first_name, type: String
end
Person.create(first_name: 'p1')

Gemfile:

source 'https://rubygems.org'
gem 'mongoid', '~> 4.0'

recreate.sh:

#!/usr/bin/env bash
set -eu
docker-compose down -v --rmi local
docker-compose pull
docker-compose build
docker-compose up
$ docker-compose exec mongo bash
# mongo db
> db.people.find()
{ "_id" : ObjectId("5df96e80a0dc090001000000"), "first_name" : "p1" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment