Seeding data in a Mongo db within a linked Docker container

4.6k views Asked by At

I'm using fig to deploy my Node.js app.

fig.yml

web:
  build: .
  command: node app.js
  links:
   - db
  ports:
   - "1337:1337"

db:
  image: dockerfile/mongodb

Running fig run db env gives me the following environment vars:

DB_PORT=tcp://172.17.0.29:27017
DB_PORT_27017_TCP=tcp://172.17.0.29:27017
DB_PORT_27017_TCP_ADDR=172.17.0.29
DB_PORT_27017_TCP_PORT=27017
DB_PORT_27017_TCP_PROTO=tcp
DB_PORT_28017_TCP=tcp://172.17.0.29:28017
DB_PORT_28017_TCP_ADDR=172.17.0.29
DB_PORT_28017_TCP_PORT=28017
DB_PORT_28017_TCP_PROTO=tcp
DB_NAME=/pos_db_run_3/db

My app's Dockerfile looks like this

# Pull base image.
FROM dockerfile/nodejs

# install mongo client
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list

RUN apt-get update
RUN apt-get install -y dnsutils
RUN apt-get install -y mongodb-org-shell

# copy the source files into the image
COPY . /data/myapp

# Define working directory.
WORKDIR /data/myapp

# Install dependencies
RUN npm install

# Create default database and user
RUN mongo $DB_PORT < seed-mongo

RUN node/seed.js

EXPOSE 1337

# Define default command.
CMD ["bash"]

However the build is failing at RUN mongo $DB_PORT < seed-mongo

MongoDB shell version: 2.6.5
connecting to: test
2014-11-20T20:47:59.193+0000 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2014-11-20T20:47:59.195+0000 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
Service 'web' failed to build: The command [/bin/sh -c mongo $DB_PORT < seed-mongo] returned a non-zero code: 1

According to the fig docs I'd be better off referring to the database simply as db so I tried

RUN mongo db < seed-mongo

But this gave me

MongoDB shell version: 2.6.5
connecting to: db
2014-11-20T21:08:11.154+0000 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2014-11-20T21:08:11.156+0000 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed

So I injected a RUN host db in there just to see if db really is a host name. And no, despite the docs above it's not.

Step 10 : RUN host db
 ---> Running in 73b4dc787c79
Host db not found: 3(NXDOMAIN)
Service 'web' failed to build: The command [/bin/sh -c host db] returned a non-zero code: 1

So I'm stumped.

How am I supposed to talk to the mongo instance running in my linked Docker container?

3

There are 3 answers

0
Dave Sag On BEST ANSWER

My solution was a combination of insights from above, but mainly I stopped trying to seed a specific database and db user in the build phase and instead use --host db_1 with no username or password. If I really need that functionality later I'll work it out.

I could then build the project and connect it to mongo in the other image.

In my config/connections.js I included:

development: {
  adapter  : 'sails-mongo',
  host     :  'db_1',
  port     :  27017,
  user     :  '',
  password :  '',
  database :  process.env.DB_NAME
},

and I changed the CMD to be "node data/seeds.js && node app"

2
Thijs Schnitger On

Your mongo container has ip address $DB_PORT_27017_TCP_ADDR
So try with mongo --host $DB_PORT_27017_TCP_ADDR < seed-mongo
You can't use it in a RUN statement I think, because the environment variables are not available at build-time. You could try using it in a CMD I guess.

2
Jake Hoffner On

You never exposed your Mongo port.

web:
  build: .
  command: node app.js
  links:
   - db
  ports:
   - "1337:1337"

db:
  image: dockerfile/mongodb
  ports:
    - "27017:27017"