Docker: Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? Docker: Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? docker docker

Docker: Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?


The issue is that you are trying to connect to localhost inside the container for DB. The port mapping that you do 5432:5432 for postgres map 5432 to localhost of your host machine.

Now your web container code is running inside the container. And there is nothing on its localhost:5432.

So you need to change your connection details in the config to connect to postgres:5432 and this is because you named the postgres DB service as postgres

Change that and it should work.


By default the postgres image is already exposing to 5432 so you can just remove that part in your yml.

Then if you would like to check if web service can connect to your postgres service you can run this docker-compose exec web curl postgres:5432 then it should return:

curl: (52) Empty reply from server

If it cannot connect it will return:

curl: (6) Could not resolve host: postgres or curl: (7) Failed to connect to postgres port 5432: Connection refused

UPDATE:

I know the problem now. It's because you are trying to connect on the localhost you should connect to the postgres service.


I had this same issue when working on a Rails 6 application in Ubuntu 20.04 using Docker and Traefik.

In my case I was trying to connect the Ruby on Rails application running in a docker container to the PostgreSQL database running on the host.

So each time I try to connect to the database I get the error:

Unexpected error while processing request: 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?

Here's how I fixed it:

So first the applications running in the container are exposed to the host via Traefik which maps to the host on port 80.

Firstly, I had to modify my PostgreSQL database configuration file to accept remote connections from other IP addresses. This StackOverflow answer can help with that - PostgreSQL: FATAL - Peer authentication failed for user (PG::ConnectionBad)

Secondly, I had to create a docker network that Traefik and other applications that will proxy through it will use:

docker network create traefik_default

Thirdly, I setup the applications in the docker-compose.yml file to use the same network that I just created:

version: "3"services:  app:    build:      context: .      dockerfile: Dockerfile    env_file:      - .env    environment:      RAILS_ENV: ${RAILS_ENV}      RACK_ENV: ${RACK_ENV}      POSTGRES_USER: ${DATABASE_USER}      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}      POSTGRES_DB: ${DATABASE_NAME}      POSTGRES_HOST_AUTH_METHOD: ${DATABASE_HOST}      POSTGRES_PORT: ${DATABASE_PORT}    expose:      - ${RAILS_PORT}    networks:      - traefik_default    labels:      - traefik.enable=true      - traefik.http.routers.my_app.rule=Host(`${RAILS_HOST}`)      - traefik.http.services.my_app.loadbalancer.server.port=${NGINX_PORT}      - traefik.docker.network=traefik_default    restart: always    volumes:      - .:/app      - gem-cache:/usr/local/bundle/gems      - node-modules:/app/node_modules  web-server:    build:      context: .      dockerfile: ./nginx/Dockerfile    depends_on:    - app    expose:      - ${NGINX_PORT}    restart: always    volumes:      - .:/appnetworks:  traefik_default:    external: truevolumes:  gem-cache:  node-modules:

Finally, in my .env file, I specified the private IP address of my host machine as the DATABASE_HOST as an environment variable this way:

DATABASE_NAME=my_app_developmentDATABASE_USER=my-usernameDATABASE_PASSWORD=passsword1DATABASE_HOST=192.168.0.156DATABASE_PORT=5432RAILS_HOST=my_app.localhostNGINX_PORT=80RAILS_ENV=developmentRACK_ENV=developmentRAILS_MASTER_KEY=e879cbg21ff58a9c50933fe775a74d00RAILS_PORT=3000

That's all.

I hope this helps.