title: Connect docker compose containers without links
body: <p><a href="https://docs.docker.com/compose/networking/" rel="nofollow noreferrer">https://docs.docker.com/compose/networking/</a></p>
  
  <p>At above official docker document, I found the part </p>
  
  <pre><code>version: "3"
  services:
    web:
      build: .
      ports:
        - "8000:8000"
    db:
      image: postgres
      ports:
        - "8001:5432"
  </code></pre>
  
  <blockquote>
    <p>Each container can now look up the hostname web or db and get back the appropriate container’s IP address. For example, <strong>web’s application code could connect to the URL postgres://db:5432</strong> and start using the Postgres database.</p>
  </blockquote>
  
  <p>So I understood this paragraph that I can connect docker containers each other without <code>links:</code> or <code>networks:</code> explicitly. because above <code>docker-compose.yml</code> snippet doesn't have <code>links</code> or <code>networks:</code> part. and the document say <strong>web’s application code could connect to the URL postgres://db:5432</strong></p>
  
  <p>So I tried to test simple docker-compose with nodejs <code>express</code>, <code>mongodb</code> together using above way. I thought I can connect <code>mongodb</code> in express app with just <code>mongodb://mongo:27017/myapp</code> But I cannot connect mongodb in express container. I think I followed docker's official manual but I don't know why it's not working. Of course I can connect mongodb using <code>links:</code> or <code>networks:</code> But I heard links is depreciated and I cannot find the proper way to use <code>networks:</code></p>
  
  <p>I think I might be misunderstood, Please fix me.
  Below is my docker-compose.yml </p>
  
  <pre><code>version: '3'
  services: 
    app: 
      container_name: node
      restart: always
      build: .
      ports:
        - '3000:3000'
    mongo:
      image: mongo
      ports:
        - '27017:27017'
  </code></pre>
  
  <p>In express app, I connect to mongodb with </p>
  
  <pre><code>mongoose.connect('mongodb://mongo:27017/myapp', {
      useMongoClient: true
  });
  // also doesn't work with mongodb://mongo/myapp
  </code></pre>
  
  <p>Plus) Dockerfile</p>
  
  <pre><code>FROM node:10.17-alpine3.9
  ENV NODE_ENV development
  WORKDIR /usr/src/app
  COPY ["package*.json", "npm-shrinkwrap.json*", "./"]
  RUN rm -rf node_modules
  RUN apk --no-cache --virtual build-dependencies add \
      python \
      make \
      g++ \
      &amp;&amp; npm install \
      &amp;&amp; apk del build-dependencies
  COPY . .
  EXPOSE 3000
  CMD npm start
  </code></pre>