title: Docker container runs with docker-compose up but doesn't with docker run
body: <p>First attempt into docker and I've hit a wall.
  I don't understand how the docker works and when my local machine is "detached" from the container. At the end the project runs with docker-compose run, but doesn't with docker run because it cannot connect to postgres.</p>
  
  <p>When I run docker-compose run with these settings everything works:</p>
  
  <p>settings.py:</p>
  
  <pre><code>DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql_psycopg2',
          'NAME': 'postgres',
          'USER': 'user',
          'PASSWORD': 'password',
          'HOST': 'db',
          'PORT': '5432',
      }
  }
  </code></pre>
  
  <p>docker-compose.yml</p>
  
  <pre><code>version: '2'
  services:
    db:
      image: postgres
      environment:
        - POSTGRES_USER=user
        - POSTGRES_PASSWORD=password
      ports:
        - '5432:5432'
    redis:
      image: redis
      ports:
        - '6379:6379'
    celery:
      build:
        context: .
        dockerfile: Dockerfile
      env_file: common.env
      command: celery -A saleor worker --app=saleor.celeryconf:app --loglevel=info
      volumes:
        - .:/app:Z
      links:
        - redis
      depends_on:
        - redis
    search:
      image: elasticsearch:5.4.3
      mem_limit: 512m
      environment:
        - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      ports:
        - '127.0.0.1:9200:9200'
    web:
      build: .
      command: python manage.py runserver 0.0.0.0:8000
      env_file: common.env
      depends_on:
        - db
        - redis
        - search
      ports:
        - '8000:8000'
      volumes:
        - .:/app:Z
    makemigrations:
      build: .
      command: python manage.py makemigrations --noinput
      volumes:
        - .:/app:Z
    migration:
      build: .
      command: python manage.py migrate --noinput
      volumes:
        - .:/app:Z
  </code></pre>
  
  <p>However, if I run </p>
  
  <ol>
  <li>docker build -t db_test .</li>
  <li>docker run -p 4000:8000 db_test</li>
  </ol>
  
  <p>Dockerfile:</p>
  
  <pre><code>FROM python:3.5
  ENV PYTHONUNBUFFERED 1
  
  RUN \
    apt-get -y update &amp;&amp; \
    apt-get install -y gettext &amp;&amp; \
    apt-get clean
  
  ADD requirements.txt /app/
  RUN pip install -r /app/requirements.txt
  
  ADD . /app
  WORKDIR /app
  
  EXPOSE 8000
  ENV PORT 8000
  
  CMD ["uwsgi", "/app/saleor/wsgi/uwsgi.ini"]
  </code></pre>
  
  <p>I am getting an exception: </p>
  
  <h2>OperationalError at / could not translate host name "db" to address: Name or service not known</h2>
  
  <p>If I change the DB settings to:</p>
  
  <pre><code>DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql_psycopg2',
          'NAME': 'postgres',
          'USER': 'user',
          'PASSWORD': 'password',
          'HOST': '**localhost**',
          'PORT': '5432',
      }
  }
  </code></pre>
  
  <p>the build and run I get:</p>
  
  <h2>could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Cannot assign requested address Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?</h2>
  
  <p>This is the postgres port on my machine. Then if I turn of postgres and the port is free nothing changes.</p>
  
  <p>Any help will be highly appreciated!</p>