Seeding data in Mongo db in linked Docker container

I am using fig

to deploy my application Node.js

.

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 vars environments:

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 application 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 fails when 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 would be better off linking to the database just like db

, so I tried

RUN mongo db < seed-mongo

      

But it 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 entered a RUN host db

to see if db

the hostname is valid . And no, despite the fact that there are no documents above him.

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 can I talk to an instance mongo

running in my linked container Docker

?

+3


source to share


3 answers


My solution was a combination of concepts from above, but basically I stopped trying to seed a specific database and db user in the build step and instead use --host db_1

without a username or password. If I really need this functionality, I will work.

Then I could build the project and connect it to mongo

in another image.

In mine config/connections.js

I have included:



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

      

and i changed CMD

to"node data/seeds.js && node app"

+1


source


Your mongo container has an IP address of $ DB_PORT_27017_TCP_ADDR
So give it a try mongo --host $DB_PORT_27017_TCP_ADDR < seed-mongo


I guess you can't use it in your RUN statement because environment variables are not available at build time. You can try using it in CMD I think.



+3


source


You have never shown your Mongolian port.

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

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

      

0


source







All Articles