difference between localhost and postgres for host in docker

流过昼夜 提交于 2020-01-23 21:20:06

问题


I am developing a django app and trying to run it inside docker. I have an issue that I could not understand so far. while running the app with docker-compose, it seems that the web app cannot connect to the database when i use these configurations:

DATABASES = {
    'default': {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'NAME': 'my_db',
       'USER': 'my_user',
       'PASSWORD': '',
       'HOST': 'localhost',
       'PORT': '5432',
    }

but once I change the host to postgres, it works. like this

DATABASES = {
    'default': {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'NAME': 'my_db',
       'USER': 'my_user',
       'PASSWORD': '',
       'HOST': 'postgres',
       'PORT': '5432',
    }

what is the difference between postgres and localhost. One is running without and issue inside docker and not in development environment in my mac and the other one is the opposite.

# docker-compose.yml    
version: '3'

    services:
      db:
        image: postgres
        expose: 
          - "5432"
      web:
        build: .
        command: python3 manage.py runserver 0.0.0.0:8000
        volumes:
          - .:/code
        ports:
          - "8000:8000"
        depends_on:
          - db

回答1:


Docker Compose actually add the hostnames of all your linked containers to each other.

On you machine, the postgres database is actually running in localhost, that why you have the localhost hostname.

In Compose, it's running in the postgres container, with the hostname postgres, that's why you have the postgres hostname.

If you want, you can create an entry in your host file to redirect postgres to localhost, you will then just have to use postgres everywhere.




回答2:


Each docker container comes with it's own networking namespace by default. That namespace includes it's own private loopback interface, aka localhost. And they are also attached to networks inside of docker where they have their own internal DNS entry and can talk to other containers on that same network.

When you run your application inside a container with a bridge network, localhost will point to the container, not the docker host you are running on. The hostname to use depends on your scenario:

  • To talk to other containers, use the container name in DNS.
  • If it's started by docker-compose, use the service name to talk to one of the containers in that service using DNS round robin.
  • If it's started inside of swarm mode, you can use the service name there to go to a VIP that round robin load balances to all containers providing that service.
  • And if you need to talk to the docker host itself, use a non-loopback IP address of the docker host.


来源:https://stackoverflow.com/questions/44635352/difference-between-localhost-and-postgres-for-host-in-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!