After setting up postgres and app service in docker-compose the Postgres is not reachable
I'm trying to setup a multi service docker environment for a Hanami web app using Postgres as a DB. I'm following Docker and other resources to do so. I currently have two service: postgres and app. I pan to add a separate one for nginx. After I run docker-compose up
everything "seems" to be ok, but after I visit the url in my local browser I get the error about Postgres.
Here are all the details per each file:
EDIT 1>>>
Dockerfile
FROM ruby:2.7.5-bullseye
RUN apt-get update && apt-get install vim -y
# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1
RUN adduser --disabled-login app_owner
USER app_owner
WORKDIR /usr/src/app
COPY --chown=app_owner Gemfile Gemfile.lock ./
COPY --chown=app_owner . ./
RUN gem install bundler:1.17.3
RUN bundle install
ENV HANAMI_HOST=0.0.0.0
ENV HANAMI_ENV=development
EXPOSE 2300
CMD ["bundle", "exec", "hanami", "server"]
docker-compose.yml
version: '3'
services:
db:
image: postgres
restart: always
environment:
- POSTGRES_USER=some_user
- POSTGRES_PASSWORD=some_pass
- POSTGRES_DB=tmsr_development
ports:
- 5432:5432
#volumes:
# - ./db:/var/lib/postgresql/data
web:
build: .
command: bundle exec hanami server
ports:
- 2300:2300
depends_on:
- db
links:
- db
#volumes:
# db:
Startup log
Creating network "tmsr_default" with the default driver
Creating tmsr_db_1 ... done
Creating tmsr_web_1 ... done
Attaching to tmsr_db_1, tmsr_web_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 /var/lib/postgresql/data ... ok
db_1 | creating subdirectories ... ok
db_1 | selecting dynamic shared memory implementation ... posix
db_1 | selecting default max_connections ... 100
db_1 | selecting default shared_buffers ... 128MB
db_1 | selecting default time zone ... Etc/UTC
db_1 | creating configuration files ... ok
db_1 | running bootstrap script ... ok
db_1 | performing post-bootstrap initialization ... ok
db_1 | syncing data to disk ... ok
db_1 |
db_1 |
db_1 | Success. You can now start the database server using:
db_1 |
db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1 |
db_1 | initdb: 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 | waiting for server to start....2022-01-18 06:44:49.111 UTC [49] 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
db_1 | 2022-01-18 06:44:49.114 UTC [49] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2022-01-18 06:44:49.125 UTC [50] LOG: database system was shut down at 2022-01-18 06:44:48 UTC
db_1 | 2022-01-18 06:44:49.131 UTC [49] 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 | 2022-01-18 06:44:49.343 UTC [49] LOG: received fast shutdown request
db_1 | waiting for server to shut down....2022-01-18 06:44:49.346 UTC [49] LOG: aborting any active transactions
db_1 | 2022-01-18 06:44:49.347 UTC [49] LOG: background worker "logical replication launcher" (PID 56) exited with exit code 1
db_1 | 2022-01-18 06:44:49.347 UTC [51] LOG: shutting down
db_1 | 2022-01-18 06:44:49.369 UTC [49] 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 | 2022-01-18 06:44:49.474 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
db_1 | 2022-01-18 06:44:49.474 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2022-01-18 06:44:49.474 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2022-01-18 06:44:49.487 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2022-01-18 06:44:49.500 UTC [63] LOG: database system was shut down at 2022-01-18 06:44:49 UTC
db_1 | 2022-01-18 06:44:49.510 UTC [1] LOG: database system is ready to accept connections
web_1 | Puma starting in single mode...
web_1 | * Puma version: 5.5.2 (ruby 2.7.5-p203) ("Zawgyi")
web_1 | * Min threads: 0
web_1 | * Max threads: 5
web_1 | * Environment: development
web_1 | * PID: 1
web_1 | * Listening on http://0.0.0.0:2300
web_1 | Use Ctrl-C to stop
Error
Boot Error
Something went wrong while loading /usr/src/app/config.ru
Hanami::Model::Error: PG::ConnectionBad:
could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1)
and accepting TCP/IP connections on port 5432? could not connect to server:
Cannot assign requested address Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
<<<EDIT 1
EDIT 2>>>
I've compared the terminal output when running via docker or plain server start from my app root.
Docker output:
web_1 | * Listening on http://0.0.0.0:2300
App server output on local machine:
* Listening on http://127.0.0.1:2300
* Listening on http://[::1]:2300
Might this be the issue?
Solution 1:
Do not refer to your connection as localhost
Cannot assign requested address Is the server running on host "localhost"
from your app reference the container name db
you are working inside docker which have it's own network so when you use localhost
you are referencing the host network which your app isn't running on