0

I am running a postgres image in a docker container and am trying to connect to it from another container using docker-compose. When I use the standard port of 5432 I am able to connect fine, but when I try to use a nonstandard port along with a port mapping I am getting a ECONNREFUSED error.

Here is my compose file:

networks:
  production-net:
    driver: bridge
services:
  conn-test:
    depends_on:
    - db
    environment:
      DB_DIALECT: postgres
      DB_HOST: db
      DB_NAME: db-name
      DB_PASSWORD: pass
      DB_PORT: '54321'
      DB_USER: user
    image: my-image
    networks:
      production-net: null
  db:
    environment:
      PGDATA: /pgdata
      POSTGRES_DB: db-name
      POSTGRES_PASSWORD: pass
      POSTGRES_USER: user
    image: postgres
    networks:
      production-net: null
    ports:
    - 54321:5432/tcp
version: '3.0'

And here is the output when I run docker-compose up

Creating network "composer_production-net" with driver "bridge"
Creating composer_db_1 ... done
Creating composer_conn-test_1 ... done
Attaching to composer_db_1, composer_conn-test_1
db_1         | The files belonging to this database system will be owned by user "postgres".
db_1         | This user must also own the server process.
db_1         |
db_1         | The database cluster will be initialized with locale "en_US.utf8".
db_1         | The default database encoding has accordingly been set to "UTF8".
db_1         | The default text search configuration will be set to "english".
db_1         |
db_1         | Data page checksums are disabled.
db_1         |
db_1         | fixing permissions on existing directory /pgdata ... ok
db_1         | creating subdirectories ... ok
db_1         | selecting default max_connections ... 100
db_1         | selecting default shared_buffers ... 128MB
db_1         | selecting dynamic shared memory implementation ... posix
db_1         | creating configuration files ... ok
db_1         | running bootstrap script ... ok
db_1         | performing post-bootstrap initialization ... ok
conn-test_1  | Connecting to database
db_1         | syncing data to disk ...
db_1         | WARNING: enabling "trust" authentication for local connections
db_1         | You can change this by editing pg_hba.conf or using the option -A, or
db_1         | --auth-local and --auth-host, the next time you run initdb.
db_1         | ok
db_1         |
db_1         | Success. You can now start the database server using:
db_1         |
db_1         |     pg_ctl -D /pgdata -l logfile start
db_1         |
db_1         | waiting for server to start....2019-02-28 20:52:02.208 UTC [41] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1         | 2019-02-28 20:52:02.534 UTC [42] LOG:  database system was shut down at 2019-02-28 20:51:58 UTC
db_1         | 2019-02-28 20:52:02.618 UTC [41] LOG:  database system is ready to accept connections
db_1         |  done
db_1         | server started
db_1         | CREATE DATABASE
db_1         |
db_1         |
db_1         | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1         |
db_1         | waiting for server to shut down....2019-02-28 20:52:04.355 UTC [41] LOG:  received fast shutdown request
db_1         | 2019-02-28 20:52:04.432 UTC [41] LOG:  aborting any active transactions
db_1         | 2019-02-28 20:52:04.436 UTC [41] LOG:  background worker "logical replication launcher" (PID 48) exited with exit code 1
db_1         | 2019-02-28 20:52:04.436 UTC [43] LOG:  shutting down
db_1         | 2019-02-28 20:52:04.853 UTC [41] LOG:  database system is shut down
db_1         |  done
db_1         | server stopped
db_1         |
db_1         | PostgreSQL init process complete; ready for start up.
db_1         |
db_1         | 2019-02-28 20:52:04.941 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1         | 2019-02-28 20:52:04.941 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1         | 2019-02-28 20:52:05.091 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1         | 2019-02-28 20:52:05.342 UTC [59] LOG:  database system was shut down at 2019-02-28 20:52:04 UTC
db_1         | 2019-02-28 20:52:05.419 UTC [1] LOG:  database system is ready to accept connections
conn-test_1  | Error during start up process
conn-test_1  | connect ECONNREFUSED 172.26.0.2:54321
conn-test_1  | { SequelizeConnectionRefusedError: connect ECONNREFUSED 172.26.0.2:54321
conn-test_1  |     at connection.connect.err (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:116:24)
conn-test_1  |     at Connection.connectingErrorHandler (/app/node_modules/pg/lib/client.js:140:14)
conn-test_1  |     at emitOne (events.js:116:13)
conn-test_1  |     at Connection.emit (events.js:211:7)
conn-test_1  |     at Socket.reportStreamError (/app/node_modules/pg/lib/connection.js:71:10)
conn-test_1  |     at emitOne (events.js:116:13)
conn-test_1  |     at Socket.emit (events.js:211:7)
conn-test_1  |     at emitErrorNT (internal/streams/destroy.js:66:8)
conn-test_1  |     at _combinedTickCallback (internal/process/next_tick.js:139:11)
conn-test_1  |     at process._tickCallback (internal/process/next_tick.js:181:9)
conn-test_1  |   name: 'SequelizeConnectionRefusedError',
conn-test_1  |   parent:
conn-test_1  |    { Error: connect ECONNREFUSED 172.26.0.2:54321
conn-test_1  |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
conn-test_1  |      errno: 'ECONNREFUSED',
conn-test_1  |      code: 'ECONNREFUSED',
conn-test_1  |      syscall: 'connect',
conn-test_1  |      address: '172.26.0.2',
conn-test_1  |      port: 54321 },
conn-test_1  |   original:
conn-test_1  |    { Error: connect ECONNREFUSED 172.26.0.2:54321
conn-test_1  |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
conn-test_1  |      errno: 'ECONNREFUSED',
conn-test_1  |      code: 'ECONNREFUSED',
conn-test_1  |      syscall: 'connect',
conn-test_1  |      address: '172.26.0.2',
conn-test_1  |      port: 54321 } }
conn-test_1  | Shutting down the application ...

I've confirmed that the docker host is resolving to the correct IP address, but it won't connect.

Can anyone point out what I am missing here?

1 Answer 1

1

When you're communicating directly between containers, you use the port number the service inside the target container is listening on. If you have a ports: declaration or a docker run -p option, it's the second port number; but in this setup, unless you want to access the service from outside of Docker space, that setting is strictly optional.

So you should set DB_PORT: '5432' to point at the "normal" PostgreSQL port, even though you've published it to the host on a different port number.

Sign up to request clarification or add additional context in comments.

1 Comment

I see. So I should not be using ports: at all since I want this to live entirely in Docker space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.