Thursday, 4 May 2017

Docker node.js mongodb authentication error

Was reading this: Using authenticationDatabase option while connecting to mongodb from nodejs

However it doesn't seem to help me.

I'm running all of the following commands in a Dockerfile. I Do the same setup on my local computer and it works, just no when building the container.

Start up mongod without auth to add users:

/usr/bin/mongod --fork --smallfiles --logpath /var/log/mongodb.log

Bash command to create users:

mongo insta /tmp/create_db.js

create_db.js file:

db.createUser(
    {
        user: "owner",
        pwd: "insta1",
        roles: [
            { role: "dbOwner", db: "insta" }
        ]
    }
);
db.createUser(
    {
        user: "normal",
        pwd: "insta1",
        roles: [
            { role: "readWrite", db: "insta" }
        ]
    }
);
db.createCollection("test");

Now I stop the mongod instance:

/usr/bin/mongod --shutdown

Start up mongod instance with auth:

/usr/bin/mongod --auth --fork --smallfiles --logpath /var/log/mongodb.log

I can connect with the following:

mongo -u normal -p insta1 --authenticationDatabase insta

Now I try to connect in node.js:

MongoClient.connect('mongodb://normal:insta1@127.0.0.1:27017/insta', async (error, db) => {
  console.log(error);
  //...more code
})

I get the error:

MongoError: Authentication failed

My Dockerfile:

FROM    ubuntu:16.04

COPY    package.json /tmp/package.json
COPY    files/create_db.js /tmp/create_db.js

RUN     apt-get -qq update && \
        apt-get -y upgrade && \
        apt-get -y install curl supervisor && \
        mkdir -p /var/log/supervisor && \
        curl -sL https://deb.nodesource.com/setup_7.x | bash - && \
        apt-get -y install nodejs

VOLUME  ["/data/db"]

RUN     apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6 && \
        echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.4.list && \
        apt-get install -y software-properties-common && \
        add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) universe" && \
        apt-get -qq update && \
        apt-get install -y mongodb-org && \
        /usr/bin/mongod --fork --smallfiles --logpath /var/log/mongodb.log && \
        sleep 5 && \
        mongo instanty /tmp/create_db.js && \
        /usr/bin/mongod --shutdown && \
        apt-get -qq autoremove

EXPOSE 27017
EXPOSE 28017

RUN     cd /tmp && \
        npm install --silent && \
    mkdir -p /srv/www && \
    cp -a /tmp/node_modules /srv/www/ && \
    chmod -R 777 /srv/www

WORKDIR /srv/www
COPY    . /srv/www

RUN     cd /srv/www && \
        /usr/bin/mongod --auth --fork --smallfiles --logpath /var/log/mongodb.log && \
        sleep 5 && \
        npm test

EXPOSE  8080

# setup supervisord config files
COPY    ./files/supervisord.conf.dev /etc/supervisor/supervisord.conf

CMD     ["/usr/bin/supervisord"]

npm test is where it screws up. I don't really know where the error is?



via Karl Morrison

No comments:

Post a Comment