I am trying to launch an app I made in deno's fresh framework with Docker, and it keeps failing. The app works perfectly with native postgres (for the backend) and manually launching the app with deno task start or deno task startNoWatch. I tried to implement this in docker containers. I added the following files to the root directory of the app:
docker-compose.yml
version: '3.8'
volumes:
data:
services:
postgresql_addrs_col:
image: postgres
container_name: postgresql_adds_cl
env_file:
- .env
ports:
- 5432:5432
volumes:
- data:/var/lib/postgresql_adds_cl
my_deno_app_main:
container_name: my_deno_app_main
env_file:
- .env
links:
- postgresql_addrs_col
depends_on:
- postgresql_addrs_col
build:
context: .
dockerfile: Dockerfile
ports:
- "8000:80"
Dockerfile:
FROM denoland/deno:alpine-1.25.3
EXPOSE 8000
WORKDIR /app
COPY . .
RUN deno cache main.ts --import-map=import_map.json
CMD ["deno", "task", "start"]
I try to run the app with the command docker-compose up, but it fails. This is what I get in my terminal:
<MYUSERNAME>@<MYCOMPUTERNAME> my-deno-app % docker-compose up
Creating network "my-deno-app_default" with the default driver
Creating volume "my-deno-app_data" with default driver
Building my_deno_app_main
[+] Building 0.5s (9/9) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 293B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/denoland/deno:alpine-1.25.3 0.4s
=> [1/4] FROM docker.io/denoland/deno:alpine-1.25.3@sha256:<A SHA256 HASH HERE> 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 2.53kB 0.0s
=> CACHED [2/4] WORKDIR /app 0.0s
=> CACHED [3/4] COPY . . 0.0s
=> CACHED [4/4] RUN deno cache main.ts --import-map=import_map.json 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:<A SHA256 HASH HERE> 0.0s
=> => naming to docker.io/library/my-deno-app_my_deno_app_main 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
WARNING: Image for service my_deno_app_main was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating postgresql_adds_cl ... done
Creating my_deno_app_main ... done
Attaching to postgresql_adds_cl, my_deno_app_main
postgresql_adds_cl | The files belonging to this database system will be owned by user "postgres".
postgresql_adds_cl | This user must also own the server process.
postgresql_adds_cl |
postgresql_adds_cl | The database cluster will be initialized with locale "en_US.utf8".
postgresql_adds_cl | The default database encoding has accordingly been set to "UTF8".
postgresql_adds_cl | The default text search configuration will be set to "english".
postgresql_adds_cl |
postgresql_adds_cl | Data page checksums are disabled.
postgresql_adds_cl |
postgresql_adds_cl | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgresql_adds_cl | creating subdirectories ... ok
postgresql_adds_cl | selecting dynamic shared memory implementation ... posix
postgresql_adds_cl | selecting default max_connections ... 100
postgresql_adds_cl | selecting default shared_buffers ... 128MB
postgresql_adds_cl | selecting default time zone ... Etc/UTC
postgresql_adds_cl | creating configuration files ... ok
postgresql_adds_cl | running bootstrap script ... ok
my_deno_app_main | Warning deno task is unstable and may drastically change in the future
postgresql_adds_cl | performing post-bootstrap initialization ... ok
my_deno_app_main | Task startNoWatch deno run --allow-net --allow-read --allow-env -A dev.ts
postgresql_adds_cl | syncing data to disk ... initdb: warning: enabling "trust" authentication for local connections
postgresql_adds_cl | You can change this by editing pg_hba.conf or using the option -A, or
postgresql_adds_cl | --auth-local and --auth-host, the next time you run initdb.
postgresql_adds_cl | ok
postgresql_adds_cl |
postgresql_adds_cl |
postgresql_adds_cl | Success. You can now start the database server using:
postgresql_adds_cl |
postgresql_adds_cl | pg_ctl -D /var/lib/postgresql/data -l logfile start
postgresql_adds_cl |
postgresql_adds_cl | waiting for server to start....2022-09-22 00:10:29.040 UTC [48] LOG: starting PostgreSQL 14.5 (Debian 14.5-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgresql_adds_cl | 2022-09-22 00:10:29.041 UTC [48] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgresql_adds_cl | 2022-09-22 00:10:29.046 UTC [49] LOG: database system was shut down at 2022-09-22 00:10:28 UTC
postgresql_adds_cl | 2022-09-22 00:10:29.049 UTC [48] LOG: database system is ready to accept connections
postgresql_adds_cl | done
postgresql_adds_cl | server started
postgresql_adds_cl | CREATE DATABASE
postgresql_adds_cl |
postgresql_adds_cl |
postgresql_adds_cl | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgresql_adds_cl |
postgresql_adds_cl | waiting for server to shut down...2022-09-22 00:10:29.236 UTC [48] LOG: received fast shutdown request
postgresql_adds_cl | .2022-09-22 00:10:29.237 UTC [48] LOG: aborting any active transactions
postgresql_adds_cl | 2022-09-22 00:10:29.238 UTC [48] LOG: background worker "logical replication launcher" (PID 55) exited with exit code 1
postgresql_adds_cl | 2022-09-22 00:10:29.239 UTC [50] LOG: shutting down
postgresql_adds_cl | 2022-09-22 00:10:29.249 UTC [48] LOG: database system is shut down
postgresql_adds_cl | done
postgresql_adds_cl | server stopped
postgresql_adds_cl |
postgresql_adds_cl | PostgreSQL init process complete; ready for start up.
postgresql_adds_cl |
postgresql_adds_cl | 2022-09-22 00:10:29.349 UTC [1] LOG: starting PostgreSQL 14.5 (Debian 14.5-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgresql_adds_cl | 2022-09-22 00:10:29.349 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgresql_adds_cl | 2022-09-22 00:10:29.349 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgresql_adds_cl | 2022-09-22 00:10:29.351 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgresql_adds_cl | 2022-09-22 00:10:29.354 UTC [62] LOG: database system was shut down at 2022-09-22 00:10:29 UTC
postgresql_adds_cl | 2022-09-22 00:10:29.358 UTC [1] LOG: database system is ready to accept connections
my_deno_app_main | Download https://deno.land/x/fresh@1.1.1/dev.ts
my_deno_app_main | Download https://deno.land/x/fresh@1.1.1/src/dev/mod.ts
my_deno_app_main | Download https://deno.land/x/fresh@1.1.1/src/dev/deps.ts
my_deno_app_main | Download https://deno.land/x/fresh@1.1.1/src/dev/error.ts
my_deno_app_main | Download https://deno.land/std@0.150.0/flags/mod.ts
my_deno_app_main | Download https://deno.land/std@0.150.0/semver/mod.ts
my_deno_app_main | Download https://deno.land/x/ts_morph@16.0.0/mod.ts
my_deno_app_main | Download https://deno.land/x/ts_morph@16.0.0/ts_morph.js
my_deno_app_main | Download https://deno.land/x/ts_morph@16.0.0/ts_morph.d.ts
my_deno_app_main | Download https://deno.land/x/ts_morph@16.0.0/common/mod.ts
my_deno_app_main | Download https://deno.land/x/code_block_writer@11.0.3/mod.ts
my_deno_app_main | Download https://deno.land/x/ts_morph@16.0.0/common/ts_morph_common.js
my_deno_app_main | Download https://deno.land/x/ts_morph@16.0.0/common/ts_morph_common.d.ts
my_deno_app_main | Download https://deno.land/x/code_block_writer@11.0.3/utils/string_utils.ts
my_deno_app_main | Download https://deno.land/x/ts_morph@16.0.0/common/typescript.js
my_deno_app_main | Download https://deno.land/x/ts_morph@16.0.0/common/typescript.d.ts
my_deno_app_main | Download https://deno.land/x/ts_morph@16.0.0/common/DenoRuntime.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/fs/ensure_dir.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/fs/expand_glob.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/path/mod.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/fs/_util.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/_util/assert.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/_util/os.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/fs/walk.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/path/_interface.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/path/common.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/path/glob.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/path/posix.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/path/separator.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/path/win32.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/path/_constants.ts
my_deno_app_main | Download https://deno.land/std@0.140.0/path/_util.ts
my_deno_app_main | The manifest has been generated for 10 routes and 2 islands.
my_deno_app_main | error: Uncaught (in promise) ConnectionRefused: Connection refused (os error 111)
my_deno_app_main | this.#conn = await Deno.connect(options);
my_deno_app_main | ^
my_deno_app_main | at async Object.connect (deno:ext/net/01_net.js:333:17)
my_deno_app_main | at async Connection.#openConnection (https://deno.land/x/postgres@v0.16.1/connection/connection.ts:253:18)
my_deno_app_main | at async Connection.#startup (https://deno.land/x/postgres@v0.16.1/connection/connection.ts:338:7)
my_deno_app_main | at async Connection.startup (https://deno.land/x/postgres@v0.16.1/connection/connection.ts:491:11)
my_deno_app_main | at async Client.connect (https://deno.land/x/postgres@v0.16.1/client.ts:220:7)
my_deno_app_main | at async PostgresConnector._makeConnection (https://raw.githubusercontent.com/joeldesante/denodb/master/lib/connectors/postgres-connector.ts:54:5)
my_deno_app_main | at async PostgresConnector.query (https://raw.githubusercontent.com/joeldesante/denodb/master/lib/connectors/postgres-connector.ts:73:5)
my_deno_app_main | at async Database.query (https://raw.githubusercontent.com/joeldesante/denodb/master/lib/database.ts:240:21)
my_deno_app_main | at async Function.createTable (https://raw.githubusercontent.com/joeldesante/denodb/master/lib/model.ts:172:5)
my_deno_app_main | at async Database.sync (https://raw.githubusercontent.com/joeldesante/denodb/master/lib/database.ts:210:7)
my_deno_app_main exited with code 1
^CGracefully stopping... (press Ctrl+C again to force)
Stopping postgresql_adds_cl ... done
<MYUSERNAME>@<MYCOMPUTERNAME> my-deno-app %
These are the important lines where the actual deno-fresh app is failing:
my_deno_app_main | error: Uncaught (in promise) ConnectionRefused: Connection refused (os error 111)
my_deno_app_main | this.#conn = await Deno.connect(options);
my_deno_app_main | ^
my_deno_app_main | at async Object.connect (deno:ext/net/01_net.js:333:17)
my_deno_app_main | at async Connection.#openConnection (https://deno.land/x/postgres@v0.16.1/connection/connection.ts:253:18)
my_deno_app_main | at async Connection.#startup (https://deno.land/x/postgres@v0.16.1/connection/connection.ts:338:7)
my_deno_app_main | at async Connection.startup (https://deno.land/x/postgres@v0.16.1/connection/connection.ts:491:11)
my_deno_app_main | at async Client.connect (https://deno.land/x/postgres@v0.16.1/client.ts:220:7)
my_deno_app_main | at async PostgresConnector._makeConnection (https://raw.githubusercontent.com/joeldesante/denodb/master/lib/connectors/postgres-connector.ts:54:5)
my_deno_app_main | at async PostgresConnector.query (https://raw.githubusercontent.com/joeldesante/denodb/master/lib/connectors/postgres-connector.ts:73:5)
my_deno_app_main | at async Database.query (https://raw.githubusercontent.com/joeldesante/denodb/master/lib/database.ts:240:21)
my_deno_app_main | at async Function.createTable (https://raw.githubusercontent.com/joeldesante/denodb/master/lib/model.ts:172:5)
my_deno_app_main | at async Database.sync (https://raw.githubusercontent.com/joeldesante/denodb/master/lib/database.ts:210:7)
my_deno_app_main exited with code 1
It looks like my deno fresh app container isn't able to connect to my postgres server container. This is the database.ts file which is where the db connection happens. Keep in mind the app works perfectly and connects to postgres perfectly when there are not run in docker containers.
database.ts
import { DataTypes, Database, Model, PostgresConnector } from "denodb";
import { config as cfg } from "dotenv";
const config = cfg();
const connection = new PostgresConnector({
host: 'localhost',
username: config.POSTGRES_USER,
password: config.POSTGRES_PASSWORD,
database: config.POSTGRES_DB,
port: 5432
});
const db = new Database(connection);
class UserDb extends Model {
static table = 'users';
//static timestamps = true;
static fields = {
id: { primaryKey: true, autoIncrement: true },
email: DataTypes.STRING,
key: DataTypes.STRING,
wallet_address: DataTypes.STRING,
created_at: DataTypes.DATETIME,
updated_at: DataTypes.DATETIME
};
static defaults = {
wallet_address: '',
};
}
db.link([UserDb]);
db.sync();
export default UserDb;
So what am I doing wrong?