1
votes

I try to set up Docker with my node.js app which uses sequelize to connect to postgres.

const sequelize = new Sequelize(
    process.env.DB_NAME,
    process.env.DB_USER,
    process.env.DB_PASS,
    {
        host: process.env.DB_HOST,
        port: process.env.DB_PORT,
        dialect: 'postgres',
    },
);

In my .env file I declare DB_HOST=postgres (which is the name of the service declared in the docker-conpose.yml) and DB_PORT=5432 among all the other environment variables.

My Dockerfile looks as follows:

FROM node:8.6.0

# Working directory for application
WORKDIR /usr/src/app
EXPOSE 8080
COPY . /usr/src/app

# In this file I create a user and a DB and give him the privlages 
ADD init.sql /docker-entrypoint-initdb.d/
RUN npm install

And my docker-compose.yml looks as follows:

version: "2"
services:
  postgres:
    image: "postgres:9.4"
    restart: always
    ports:
      - "5432:5432"
    env_file:
      - .env
  node:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - postgres
    command: ["npm", "start"]

When I docker-compose up I get the error that the Sequelize is not able to connect to the DB.

Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 172.18.0.2:5431

Can someone help me with this error?

all the docker logs:

WARNING: Image for service node was built because it did not already exist. To rebuild this image you must use docker-compose build or docker-compose up --build.

Creating graphqlpostgrestemplate_postgres_1 ...

Creating graphqlpostgrestemplate_postgres_1 ... done

Creating graphqlpostgrestemplate_node_1 ...

Creating graphqlpostgrestemplate_node_1 ... done

Attaching to graphqlpostgrestemplate_postgres_1, graphqlpostgrestemplate_node_1

postgres_1 | The files belonging to this database system will be owned by user "postgres".

postgres_1 | This user must also own the server process.

postgres_1 |

postgres_1 | The database cluster will be initialized with locale "en_US.utf8".

postgres_1 | The default database encoding has accordingly been set to "UTF8".

postgres_1 | The default text search configuration will be set to "english".

postgres_1 |

postgres_1 | Data page checksums are disabled.

postgres_1 |

postgres_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok

postgres_1 | creating subdirectories ... ok

postgres_1 | selecting default max_connections ... 100

postgres_1 | selecting default shared_buffers ... 128MB

postgres_1 | selecting dynamic shared memory implementation ... posix

postgres_1 | creating configuration files ... ok

postgres_1 | creating template1 database in /var/lib/postgresql/data/base/1 ... ok

postgres_1 | initializing pg_authid ... ok

postgres_1 | initializing dependencies ... ok

postgres_1 | creating system views ... ok

node_1 | npm info it worked if it ends with ok

node_1 | npm info using [email protected]

node_1 | npm info using [email protected]

postgres_1 | loading system objects' descriptions ... ok

node_1 | npm info lifecycle [email protected]~prestart: [email protected]

node_1 | npm info lifecycle [email protected]~start: [email protected]

node_1 |

node_1 | > [email protected] start /usr/src/app

node_1 | > nodemon --exec babel-node index.js

node_1 |

postgres_1 | creating collations ... ok

postgres_1 | creating conversions ... ok

postgres_1 | creating dictionaries ... ok

postgres_1 | setting privileges on built-in objects ... ok

postgres_1 | creating information schema ... ok

postgres_1 | loading PL/pgSQL server-side language ... ok

node_1 | [nodemon] 1.12.1

node_1 | [nodemon] to restart at any time, enter rs

node_1 | [nodemon] watching: .

node_1 | [nodemon] starting babel-node index.js

postgres_1 | vacuuming database template1 ... ok

postgres_1 | copying template1 to template0 ... ok

postgres_1 | copying template1 to postgres ... ok

postgres_1 | syncing data to disk ... ok

postgres_1 |

postgres_1 | Success. You can now start the database server using:

postgres_1 |

postgres_1 | postgres -D /var/lib/postgresql/data

postgres_1 | or

postgres_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start

postgres_1 |

postgres_1 |

postgres_1 | WARNING: enabling "trust" authentication for local connections

postgres_1 | You can change this by editing pg_hba.conf or using the option -A, or

postgres_1 | --auth-local and --auth-host, the next time you run initdb.

postgres_1 | ****************************************************

postgres_1 | WARNING: No password has been set for the database.

postgres_1 | This will allow anyone with access to the

postgres_1 | Postgres port to access your database. In

postgres_1 | Docker's default configuration, this is

postgres_1 | effectively any other container on the same

postgres_1 | system.

postgres_1 |

postgres_1 | Use "-e POSTGRES_PASSWORD=password" to set

postgres_1 | it in "docker run".

postgres_1 | ****************************************************

postgres_1 | waiting for server to start....LOG: could not bind IPv6 socket: Cannot assign requested address

postgres_1 | HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.

postgres_1 | LOG: database system was shut down at 2017-10-10 12:17:15 UTC

postgres_1 | LOG: MultiXact member wraparound protections are now enabled

postgres_1 | LOG: database system is ready to accept connections

postgres_1 | LOG: autovacuum launcher started

postgres_1 | done

postgres_1 | server started

postgres_1 | ALTER ROLE

postgres_1 |

postgres_1 |

postgres_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

postgres_1 |

postgres_1 | waiting for server to shut down....LOG: received fast shutdown request

postgres_1 | LOG: aborting any active transactions

postgres_1 | LOG: autovacuum launcher shutting down

postgres_1 | LOG: shutting down

postgres_1 | LOG: database system is shut down

node_1 | Tue, 10 Oct 2017 12:17:16 GMT sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators at node_modules/sequelize/lib/sequelize.js:236:13

node_1 | WARNING: No configurations found in configuration directory:/usr/src/app/config

node_1 | WARNING: To disable this warning set SUPPRESS_NO_CONFIG_WARNING in the environment.

node_1 | Tue, 10 Oct 2017 12:17:17 GMT body-parser deprecated undefined extended: provide extended option at index.js:53:30

node_1 | Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 172.18.0.2:5431

node_1 | at connection.connect.err (/usr/src/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:96:24)

node_1 | at Connection.connectingErrorHandler (/usr/src/app/node_modules/pg/lib/client.js:123:14)

node_1 | at emitOne (events.js:115:13)

node_1 | at Connection.emit (events.js:210:7)

node_1 | at Socket. (/usr/src/app/node_modules/pg/lib/connection.js:71:10)

node_1 | at emitOne (events.js:115:13)

node_1 | at Socket.emit (events.js:210:7)

node_1 | at emitErrorNT (internal/streams/destroy.js:64:8)

node_1 | at _combinedTickCallback (internal/process/next_tick.js:138:11)

node_1 | at process._tickDomainCallback (internal/process/next_tick.js:218:9)

node_1 | [nodemon] clean exit - waiting for changes before restart

postgres_1 | done

postgres_1 | server stopped

postgres_1 |

postgres_1 | PostgreSQL init process complete; ready for start up.

postgres_1 |

postgres_1 | LOG: database system was shut down at 2017-10-10 12:17:16 UTC

postgres_1 | LOG: MultiXact member wraparound protections are now enabled

postgres_1 | LOG: database system is ready to accept connections

postgres_1 | LOG: autovacuum launcher started

1
could you post your docker logs please? - Sergiu

1 Answers

1
votes

To your Docker-compose add link configuration option in your service node point to service postgres something like this:

node:
  links:
    - postgres

Then you can connect to postgresdb point with name service postgres