title: Seeding data in a Mongo db within a linked Docker container
body: <p>I'm using <a href="http://www.fig.sh" rel="nofollow"><code>fig</code></a> to deploy my <code>Node.js</code> app.</p>
  
  <p><code>fig.yml</code></p>
  
  <pre><code>web:
    build: .
    command: node app.js
    links:
     - db
    ports:
     - "1337:1337"
  
  db:
    image: dockerfile/mongodb
  </code></pre>
  
  <p>Running <code>fig run db env</code> gives me the following environment vars:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>My app's <code>Dockerfile</code> looks like this</p>
  
  <pre><code># 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 &lt; seed-mongo
  
  RUN node/seed.js
  
  EXPOSE 1337
  
  # Define default command.
  CMD ["bash"]
  </code></pre>
  
  <p>However the build is failing at <code>RUN mongo $DB_PORT &lt; seed-mongo</code></p>
  
  <pre><code>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 &lt; seed-mongo] returned a non-zero code: 1
  </code></pre>
  
  <p>According to the <a href="http://www.fig.sh/yml.html#links" rel="nofollow">fig docs</a> I'd be better off referring to the database simply as <code>db</code> so I tried</p>
  
  <pre><code>RUN mongo db &lt; seed-mongo
  </code></pre>
  
  <p>But this gave me</p>
  
  <pre><code>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
  </code></pre>
  
  <p>So I injected a <code>RUN host db</code> in there just to see if <code>db</code> really is a host name.  And no, despite the docs above it's not.</p>
  
  <pre><code>Step 10 : RUN host db
   ---&gt; 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
  </code></pre>
  
  <p>So I'm stumped.</p>
  
  <p>How am I supposed to talk to the <code>mongo</code> instance running in my linked <code>Docker</code> container?</p>