0

I am having three issues:

  1. very similar to How to solve Node.js Error: Cannot find Module?, but mine can't find node at all it seems.

  2. when updating either docker-compose.yml or the logic, running docker compose with or without the --build flag does not detect changes and update the image and I'm running stale broken images

  3. even when specifying the dependency order with depends_on, my node server builds and tries to connect to my postgres server before it is ready.

  4. the server container cannot seem to communicate with my postgresql container, although I can communicate with both from my local environment by running psql postgres://postgres:postgres@localhost:5432/postgres(postgres) or curl localhost:8080/foo(server)

docker-practice_server_1 | error:  Error: connect ECONNREFUSED 127.0.0.1:5432
docker-practice_server_1 |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
docker-practice_server_1 |   errno: -111,
docker-practice_server_1 |   code: 'ECONNREFUSED',
docker-practice_server_1 |   syscall: 'connect',
docker-practice_server_1 |   address: '127.0.0.1',
docker-practice_server_1 |   port: 5432
docker-practice_server_1 | }

Any help on resolving any of these errors is much appreciated.

Error

docker-practice_server_1 | node:internal/modules/cjs/loader:926
docker-practice_server_1 |   throw err;
docker-practice_server_1 |   ^
docker-practice_server_1 | Error: Cannot find module '/node index'
docker-practice_server_1 |     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:923:15)
docker-practice_server_1 |     at Function.Module._load (node:internal/modules/cjs/loader:768:27)
docker-practice_server_1 |     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
docker-practice_server_1 |     at node:internal/main/run_main_module:17:47 {
docker-practice_server_1 |   code: 'MODULE_NOT_FOUND',
docker-practice_server_1 |   requireStack: []
docker-practice_server_1 | }

project structure

--- docker-compose.yml
--- server
----- index.js
----- package.json
----- Dockerfile

index.js

const express = require("express");
const app = express();
const pgp = require("pg-promise")();
const db = pgp("postgres://postgres:postgres@localhost:5432/postgres")
  .connect()
  .then(() => console.log("connected!"))
  .catch((err) => console.error("failed to connect because: ", err));

app.get("/foo", (req, res) => {
  res.send("sup");
  db.any("select * from sups")
    .then((v) => console.log("got: ", v))
    .catch((err) => console.error("error: ", err));
});

app.listen(8080, () => console.log("listening on 8080"));

docker-compose.yml

services:
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    ports:
      - 5432:5432
  server:
    depends_on:
      - db
    build:
      context: ./server
      dockerfile: Dockerfile
    ports:
      - 8080:8080

Dockerfile

FROM node:14

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm i
COPY . .

EXPOSE 8080
CMD [ "node", "index.js" ]
2
  • 1
    What's in your Dockerfile? In particular, what is its CMD? (If you use JSON-array form, you are responsible for breaking into words yourself; that error message looks like you might have multiple words in a single JSON array element.) Commented Feb 27, 2021 at 21:04
  • @DavidMaze thanks! I'll update the original post. but I'm basically using this: nodejs.org/en/docs/guides/nodejs-docker-webapp, meaning that in CMD, I have CMD ["node", "index.js"] Commented Feb 27, 2021 at 21:12

0

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.