porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.09k stars 259 forks source link

Docker container exits with code 139 after connecting postgresjs as dependency #782

Closed guuseh closed 6 months ago

guuseh commented 6 months ago

Hi.

I'm pretty new to this so apologies in advance if my wording is not clear or if the formatting is wrong, or if this is entirely the wrong place to post this.

I'm trying to connect to postgres through a docker container with deno, but as soon as I import postgres (from "https://deno.land/x/postgresjs@v3.4.3/mod.js") the container with my api service exits with code 139. I will write my code down below and try to be clear about when the problems start. I'm getting the code from a course I'm following, and it works for everyone else. Previously I also had trouble connecting to postgresjs but I was told with the new version 4.3.2 it should be fixed however it still won't let me connect. I am using a MacBook Pro with intel i5 on Ventura 13.2.

My folder structure: | api |-- app.js |-- app-run.js |-- deps.js |-- Dockerfile | docker-compose.yml | project.env

app.js:

import { Hono } from "./deps.js";
import { postgres } from "./deps.js";

const app = new Hono();
const sql = postgres();

app.get("/", async (c) => {
    const todos = await sql `SELECT * FROM todos`;
    return c.json(todos);
});

export default app;

app-run.js:

import app from "./app.js";

Deno.serve(app.fetch);

deps.js:

export { Hono } from "https://deno.land/x/hono@v3.7.4/mod.ts";
import postgres from "https://deno.land/x/postgresjs@v3.4.3/mod.js"; // have also tried many other versions 3.3.1, 3.2.., 3.4.2 etc...
export { postgres };

Dockerfile:

FROM denoland/deno:alpine-1.34.0 
# if I use version 1.37.0 (which is the one used in my course) the app doesn't start before I even add the database 

WORKDIR /app

COPY deps.js .

RUN deno cache deps.js

COPY . .

EXPOSE 8000

CMD [ "run", "--allow-net", "--allow-env", "--unstable", "--watch", "app-run.js" ]

docker-compose.yml:

services:
  api:
    build: api
    restart: unless-stopped
    volumes:
      - ./api:/app
    ports:
      - 8000:8000
    depends_on:
      - database
    env_file:
      - project.env

  database:
    container_name: postgresql_database
    image: postgres:16.1
    # I also tried 14.1 because that's the one I used before but didn't help
    restart: unless-stopped
    env_file:
      - project.env
     # I tried to add a healthcheck here but it didn't change anything

project.env:

POSTGRES_USER=username
POSTGRES_PASSWORD=password
POSTGRES_DB=database

PGUSER=username
PGPASSWORD=password
PGDATABASE=database

PGHOST=postgresql_database
PGPORT=5432

The container runs fine before I add the dependency - I can access it with $ docker exec -it postgresql_database psql -U username database so adding the database itself is not a problem. It's when I want to access it from within the app with the database client that it stops running.

Before I add the env_file to the api service in docker-compose.yml it obviously gives me an error that the connection was refused, but the api container doesn't exit with code 139. I don't know if that's of any use but I figured the more information the better. This is the console when trying to run the container without the env_file added to the api service:

guushoeberechts@MacBook-Pro-van-Guus 20-containerisation % docker compose up --build
[+] Building 2.5s (11/11) FINISHED                                                      docker:desktop-linux
 => [api internal] load build definition from Dockerfile                                                0.0s
 => => transferring dockerfile: 233B                                                                    0.0s
 => [api internal] load .dockerignore                                                                   0.0s
 => => transferring context: 2B                                                                         0.0s
 => [api internal] load metadata for docker.io/denoland/deno:alpine-1.34.0                              2.4s
 => [api auth] denoland/deno:pull token for registry-1.docker.io                                        0.0s
 => [api 1/5] FROM docker.io/denoland/deno:alpine-1.34.0@sha256:b00e3a76f21e2c4a2105566f129fec67a61ff8  0.0s
 => [api internal] load build context                                                                   0.0s
 => => transferring context: 573B                                                                       0.0s
 => CACHED [api 2/5] WORKDIR /app                                                                       0.0s
 => CACHED [api 3/5] COPY deps.js .                                                                     0.0s
 => CACHED [api 4/5] RUN deno cache deps.js                                                             0.0s
 => CACHED [api 5/5] COPY . .                                                                           0.0s
 => [api] exporting to image                                                                            0.0s
 => => exporting layers                                                                                 0.0s
 => => writing image sha256:d210b9d2e7dfe18d90063955b34843f49033ba5b105ad9123925c39aefe09b54            0.0s
 => => naming to docker.io/library/20-containerisation-api                                              0.0s
[+] Running 3/2
 ✔ Network 20-containerisation_default  Created                                                         0.1s 
 ✔ Container postgresql_database        Created                                                         0.1s 
 ✔ Container 20-containerisation-api-1  Created                                                         0.0s 
Attaching to 20-containerisation-api-1, postgresql_database
postgresql_database        | The files belonging to this database system will be owned by user "postgres".
postgresql_database        | This user must also own the server process.
postgresql_database        | 
postgresql_database        | The database cluster will be initialized with locale "en_US.utf8".
postgresql_database        | The default database encoding has accordingly been set to "UTF8".
postgresql_database        | The default text search configuration will be set to "english".
postgresql_database        | 
postgresql_database        | Data page checksums are disabled.
postgresql_database        | 
postgresql_database        | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgresql_database        | creating subdirectories ... 
postgresql_database        | ok
postgresql_database        | selecting dynamic shared memory implementation ... 
postgresql_database        | posix
postgresql_database        | selecting default max_connections ... 
postgresql_database        | 100
postgresql_database        | selecting default shared_buffers ... 
postgresql_database        | 128MB
postgresql_database        | selecting default time zone ... 
postgresql_database        | Etc/UTC
postgresql_database        | creating configuration files ... 
postgresql_database        | ok
postgresql_database        | running bootstrap script ... 
20-containerisation-api-1  | Watcher Process started.
postgresql_database        | ok
postgresql_database        | performing post-bootstrap initialization ... 
20-containerisation-api-1  | Listening on http://localhost:8000/
postgresql_database        | ok
postgresql_database        | syncing data to disk ... 
postgresql_database        | ok
postgresql_database        | 
postgresql_database        | 
postgresql_database        | Success. You can now start the database server using:
postgresql_database        | 
postgresql_database        |     pg_ctl -D /var/lib/postgresql/data -l logfile start
postgresql_database        | 
postgresql_database        | 
postgresql_database        | initdb: warning: enabling "trust" authentication for local connections
postgresql_database        | You can change this by editing pg_hba.conf or using the option -A, or
postgresql_database        | --auth-local and --auth-host, the next time you run initdb.
postgresql_database        | waiting for server to start....
postgresql_database        | 2024-01-21 13:07:19.118 UTC [48] LOG:  starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgresql_database        | 2024-01-21 13:07:19.122 UTC [48] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgresql_database        | 2024-01-21 13:07:19.128 UTC [49] LOG:  database system was shut down at 2024-01-21 13:07:18 UTC
postgresql_database        | 2024-01-21 13:07:19.133 UTC [48] LOG:  database system is ready to accept connections
postgresql_database        |  done
postgresql_database        | server started
postgresql_database        | CREATE DATABASE
postgresql_database        | 
postgresql_database        | 
postgresql_database        | 
postgresql_database        | 
postgresql_database        | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgresql_database        | 
postgresql_database        | 
postgresql_database        | 2024-01-21 13:07:19.392 UTC [48] LOG:  received fast shutdown request
postgresql_database        | waiting for server to shut down....
postgresql_database        | 2024-01-21 13:07:19.393 UTC [48] LOG:  aborting any active transactions
postgresql_database        | 2024-01-21 13:07:19.395 UTC [48] LOG:  background worker "logical replication launcher" (PID 55) exited with exit code 1
postgresql_database        | 2024-01-21 13:07:19.396 UTC [50] LOG:  shutting down
postgresql_database        | 2024-01-21 13:07:19.409 UTC [48] LOG:  database system is shut down
postgresql_database        |  done
postgresql_database        | server stopped
postgresql_database        | 
postgresql_database        | 
postgresql_database        | PostgreSQL init process complete; ready for start up.
postgresql_database        | 
postgresql_database        | 
postgresql_database        | 2024-01-21 13:07:19.572 UTC [1] LOG:  starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgresql_database        | 2024-01-21 13:07:19.572 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgresql_database        | 2024-01-21 13:07:19.572 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgresql_database        | 2024-01-21 13:07:19.606 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgresql_database        | 2024-01-21 13:07:19.611 UTC [62] LOG:  database system was shut down at 2024-01-21 13:07:19 UTC
postgresql_database        | 2024-01-21 13:07:19.617 UTC [1] LOG:  database system is ready to accept connections
20-containerisation-api-1  | Trace: ConnectionRefused: Connection refused (os error 111)
20-containerisation-api-1  |     at async Object.connect (ext:deno_net/01_net.js:473:55)
20-containerisation-api-1  |     at cachedError (https://deno.land/x/postgresjs@v3.4.3/src/query.js:170:23)
20-containerisation-api-1  |     at new Query (https://deno.land/x/postgresjs@v3.4.3/src/query.js:36:24)
20-containerisation-api-1  |     at sql (https://deno.land/x/postgresjs@v3.4.3/src/index.js:113:11)
20-containerisation-api-1  |     at Array.<anonymous> (file:///app/app.js:9:29) {
20-containerisation-api-1  |   name: "ConnectionRefused",
20-containerisation-api-1  |   code: "ECONNREFUSED"
20-containerisation-api-1  | }
20-containerisation-api-1  |     at Hono.errorHandler (https://deno.land/x/hono@v3.7.4/hono-base.ts:55:11)
20-containerisation-api-1  |     at Hono.handleError (https://deno.land/x/hono@v3.7.4/hono-base.ts:277:19)
20-containerisation-api-1  |     at https://deno.land/x/hono@v3.7.4/hono-base.ts:335:23
20-containerisation-api-1  |     at eventLoopTick (ext:core/01_core.js:182:11)
20-containerisation-api-1  |     at async ext:deno_http/00_serve.js:514:22

And this is the console when trying to run it with all the code as described above. It exits first with code 0 and then on every subsequent refresh of localhost it exits with code 139:

guushoeberechts@MacBook-Pro-van-Guus 20-containerisation % docker compose up --build
[+] Building 4.0s (10/10) FINISHED                                                      docker:desktop-linux
 => [api internal] load build definition from Dockerfile                                                0.0s
 => => transferring dockerfile: 233B                                                                    0.0s
 => [api internal] load .dockerignore                                                                   0.0s
 => => transferring context: 2B                                                                         0.0s
 => [api internal] load metadata for docker.io/denoland/deno:alpine-1.34.0                              0.9s
 => [api 1/5] FROM docker.io/denoland/deno:alpine-1.34.0@sha256:b00e3a76f21e2c4a2105566f129fec67a61ff8  0.0s
 => [api internal] load build context                                                                   0.1s
 => => transferring context: 372B                                                                       0.1s
 => CACHED [api 2/5] WORKDIR /app                                                                       0.0s
 => [api 3/5] COPY deps.js .                                                                            0.0s
 => [api 4/5] RUN deno cache deps.js                                                                    2.8s
 => [api 5/5] COPY . .                                                                                  0.0s 
 => [api] exporting to image                                                                            0.2s 
 => => exporting layers                                                                                 0.2s
 => => writing image sha256:d210b9d2e7dfe18d90063955b34843f49033ba5b105ad9123925c39aefe09b54            0.0s
 => => naming to docker.io/library/20-containerisation-api                                              0.0s
[+] Running 3/3
 ✔ Network 20-containerisation_default  Created                                                         0.1s 
 ✔ Container postgresql_database        Created                                                         0.0s 
 ✔ Container 20-containerisation-api-1  Created                                                         0.1s 
Attaching to 20-containerisation-api-1, postgresql_database
postgresql_database        | The files belonging to this database system will be owned by user "postgres".
postgresql_database        | This user must also own the server process.
postgresql_database        | 
postgresql_database        | 
postgresql_database        | The database cluster will be initialized with locale "en_US.utf8".
postgresql_database        | The default database encoding has accordingly been set to "UTF8".
postgresql_database        | The default text search configuration will be set to "english".
postgresql_database        | 
postgresql_database        | Data page checksums are disabled.
postgresql_database        | 
postgresql_database        | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgresql_database        | creating subdirectories ... 
postgresql_database        | ok
postgresql_database        | selecting dynamic shared memory implementation ... 
postgresql_database        | posix
postgresql_database        | selecting default max_connections ... 
postgresql_database        | 100
postgresql_database        | selecting default shared_buffers ... 
postgresql_database        | 128MB
postgresql_database        | selecting default time zone ... 
postgresql_database        | Etc/UTC
postgresql_database        | creating configuration files ... 
postgresql_database        | ok
postgresql_database        | running bootstrap script ... 
20-containerisation-api-1  | Watcher Process started.
postgresql_database        | ok
postgresql_database        | performing post-bootstrap initialization ... 
20-containerisation-api-1  | Listening on http://localhost:8000/
postgresql_database        | ok
postgresql_database        | syncing data to disk ... 
postgresql_database        | initdb: warning: enabling "trust" authentication for local connections
postgresql_database        | You can change this by editing pg_hba.conf or using the option -A, or
postgresql_database        | --auth-local and --auth-host, the next time you run initdb.
postgresql_database        | ok
postgresql_database        | 
postgresql_database        | 
postgresql_database        | Success. You can now start the database server using:
postgresql_database        | 
postgresql_database        |     pg_ctl -D /var/lib/postgresql/data -l logfile start
postgresql_database        | 
postgresql_database        | 
postgresql_database        | waiting for server to start....
postgresql_database        | 2024-01-21 12:21:06.612 UTC [48] LOG:  starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgresql_database        | 2024-01-21 12:21:06.614 UTC [48] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgresql_database        | 2024-01-21 12:21:06.622 UTC [49] LOG:  database system was shut down at 2024-01-21 12:21:06 UTC
postgresql_database        | 2024-01-21 12:21:06.627 UTC [48] LOG:  database system is ready to accept connections
postgresql_database        |  done
postgresql_database        | server started
postgresql_database        | CREATE DATABASE
postgresql_database        | 
postgresql_database        | 
postgresql_database        | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgresql_database        | 
postgresql_database        | 
postgresql_database        | 2024-01-21 12:21:06.861 UTC [48] LOG:  received fast shutdown request
postgresql_database        | waiting for server to shut down....
postgresql_database        | 2024-01-21 12:21:06.862 UTC [48] LOG:  aborting any active transactions
postgresql_database        | 2024-01-21 12:21:06.864 UTC [48] LOG:  background worker "logical replication launcher" (PID 55) exited with exit code 1
postgresql_database        | 2024-01-21 12:21:06.866 UTC [50] LOG:  shutting down
postgresql_database        | 2024-01-21 12:21:06.877 UTC [48] LOG:  database system is shut down
postgresql_database        |  done
postgresql_database        | server stopped
postgresql_database        | 
postgresql_database        | PostgreSQL init process complete; ready for start up.
postgresql_database        | 
postgresql_database        | 
postgresql_database        | 2024-01-21 12:21:06.992 UTC [1] LOG:  starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgresql_database        | 2024-01-21 12:21:06.992 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgresql_database        | 2024-01-21 12:21:06.992 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgresql_database        | 2024-01-21 12:21:06.996 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgresql_database        | 2024-01-21 12:21:07.001 UTC [62] LOG:  database system was shut down at 2024-01-21 12:21:06 UTC
postgresql_database        | 2024-01-21 12:21:07.007 UTC [1] LOG:  database system is ready to accept connections
20-containerisation-api-1 exited with code 0
20-containerisation-api-1  | Listening on http://localhost:8000/
20-containerisation-api-1 exited with code 139
20-containerisation-api-1  | Listening on http://localhost:8000/
20-containerisation-api-1 exited with code 139
20-containerisation-api-1  | Listening on http://localhost:8000/
20-containerisation-api-1 exited with code 139
20-containerisation-api-1  | Listening on http://localhost:8000/

Does anyone know how to resolve this? I would really like to use this postgresjs client since that's the one we use in the course but I'm afraid that if I can't solve it I will have to use a different one and do a lot of extra work trying to figure out how to make that work AND hand in my assignments correctly...

If any more information is needed please let me know, I'll try my best to be as precise as possible!

Thank you!

guuseh commented 6 months ago

so my teacher and I found the answer and it's very frustrating but easy - in my Docker Desktop general settings, my sharing implementation was on 'VirtioFS' and 'Use Visualization framework' was checked. When changing that to 'gRPC FUSE' and unchecked all my problems disappeared. These were the settings Docker came with so had no idea that they should be changed but happy we found out!