4

I am playing with MongoDB and Docker and at this point I am trying to create a useful image for myself to use at work. I have created the following Dockerfile:

FROM mongo:2.6
VOLUME /data/db /data/configdb
CMD ["mongod"]
EXPOSE 27017

And I have added it to my docker-compose.yml file:

version: '2'
services:
    ### PHP/Apache Container
    php-apache:
        container_name: "php55-dev"
        image: reynierpm/php55-dev
        ports:
            - "80:80"
        environment:
            PHP_ERROR_REPORTING: 'E_ALL & ~E_DEPRECATED & ~E_NOTICE'
        volumes:
            - ~/mmi:/var/www
            - ~/data:/data
        links:
            - mongodb
    ### MongoDB Container
    mongodb:
        container_name: "mongodb"
        build: ./mongo
        environment:
            MONGODB_USER: "xxxx"
            MONGODB_DATABASE: "xxxx"
            MONGODB_PASS: "xxxx"
        ports:
            - "27017:27017"
        volumes:
            - ~/data/mongo:/data/db

I have some questions regarding this setup I have made:

  • Do I need VOLUME /data/db /data/configdb at the Dockerfile or would be enough to have this line ~/data/mongo:/data/configdb at docker-compose.yml?
  • I am assuming (and I took it from here) that as soon as I build the Mongo image I will be creating a database and giving full permissions to the user with password as it's on the environment variables? I am right? (I couldn't find anything helpful here)
  • How do I import a current mongo backup (several JSON files) into the database that should be created on the mongo container? I believe I need to run mongorestore command but how? do I need to create an script and run it each time the container start? or should I run during image build? What's the best approach?
Skippy le Grand Gourou
  • 6,976
  • 4
  • 60
  • 76
ReynierPM
  • 17,594
  • 53
  • 193
  • 363

1 Answers1

3

Do I need VOLUME /data/db /data/configdb at the Dockerfile or would be enough to have this line ~/data/mongo:/data/configdb at docker-compose.yml?

VOLUME is not required when you are mounting a host directory but it is helpful as metadata. VOLUME does provide some special "copy data on volume creation" semantics when mounting a Docker volume (non host dir) which will impact your data initialisation method choice.

am assuming (and I took it from here) that as soon as I build the Mongo image I will be creating a database and giving full permissions to the user with password as it's on the environment variables? I am right? (I couldn't find anything helpful here)

MONGO_USER, MONGO_DATABASE and MONGO_PASS do not do anything in the official mongo Docker image or to mongod itself.

The mongo image has added support for similar environment variables:

  • MONGO_INITDB_ROOT_USERNAME
  • MONGO_INITDB_ROOT_PASSWORD
  • MONGO_INITDB_DATABASE

How do I import a current mongo backup (several JSON files) into the database that should be created on the mongo container? I believe I need to run mongorestore command but how? do I need to create an script and run it each time the container start? or should I run during image build? What's the best approach?

Whether you initialise data at build or runtime is up to your usage. As mentioned previously, Docker can copy data from a specified VOLUME into a volume it creates. If you are mounting a host directory you probably need to do the initialisation at run time.

mongorestore requires a running server to restore to. During a build you would need to launch the server and restore in the same RUN step. At runtime you might need to include a startup script that checks for existence of your database.

Mongo is able to initialise any empty directory into a blank mongo instance so you don't need to be worried about mongo not starting.

Matt
  • 68,711
  • 7
  • 155
  • 158
  • I've tried to start with Ansible but is to much for a single task maybe later if this still growing I will consider it again. Even if I accept your answer as the right one could you improve it adding a simple script example for bash that allow to create a DB and add a user? I don't have a clue – ReynierPM Nov 02 '16 at 12:21