title: Docker Compose cannot connect to database
body: <p>I'm using nestjs for my backend and using typeorm as ORM.
  I tried to define my database and my application in an docker-compose file. </p>
  
  <p>If I'm running my database as a container and my application from my local machine it works well. My program connects and creates the tables etc.</p>
  
  <p>But if I try to connect the database from within my container or to start the container with docker-compose up it fails.</p>
  
  <p>Always get an ECONNREFUSED Error.</p>
  
  <p>Where is my mistake ?</p>
  
  <p>docker-compose.yml</p>
  
  <pre><code>version: '3.1'
  volumes:
   dbdata:
  
  services:
    db:
      image: postgres:10
      volumes:
        - ./dbData/:/var/lib/postgresql/data
      restart: always
      environment:
        - POSTGRES_PASSWORD=${TYPEORM_PASSWORD}
        - POSTGRES_USER=${TYPEORM_USERNAME}
        - POSTGRES_DB=${TYPEORM_DATABASE}
      ports:
        - ${TYPEORM_PORT}:5432
  
    backend:
      build: .
      ports:
        - "3001:3000"
      command: npm run start
      volumes:
        - .:/src
  </code></pre>
  
  <p>Dockerfile</p>
  
  <pre><code>FROM node:10.5
  
  WORKDIR /home
  
  # Bundle app source
  COPY . /home
  
  # Install app dependencies
  #RUN npm install -g nodemon
  # If you are building your code for production
  # RUN npm install --only=production
  RUN npm i -g @nestjs/cli
  RUN npm install
  
  EXPOSE 3000
  </code></pre>
  
  <p>.env</p>
  
  <pre><code># .env
  HOST=localhost
  PORT=3000
  NODE_ENV=development
  LOG_LEVEL=debug
  
  TYPEORM_CONNECTION=postgres
  TYPEORM_HOST=localhost
  TYPEORM_USERNAME=postgres
  TYPEORM_PASSWORD=postgres
  TYPEORM_DATABASE=mariokart
  TYPEORM_PORT=5432
  TYPEORM_SYNCHRONIZE=true
  TYPEORM_DROP_SCHEMA=true
  TYPEORM_LOGGING=all
  TYPEORM_ENTITIES=src/database/entity/*.ts
  TYPEORM_MIGRATIONS=src/database/migrations/**/*.ts
  TYPEORM_SUBSCRIBERS=src/database/subscribers/**/*.ts
  </code></pre>
  
  <p>I tried to use links but it don't work in the container.</p>