我正在嘗試為使用 Postgres 作為資料庫的 Hanami Web 應用程式設定多服務 docker 環境。我正在關注 Docker 和其他資源來這樣做。我目前有兩個服務:postgres 和 app。我平移為 nginx 添加一個單獨的。在我運行docker-compose up“似乎”一切正常之后,但是在我訪問本地瀏覽器中的 url 之后,我得到了關于 Postgres 的錯誤。
以下是每個檔案的所有詳細資訊:
編輯 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"]
碼頭工人-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:
啟動日志
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
錯誤
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?
<<<編輯1
編輯 2>>>
我比較了通過 docker 運行時的終端輸出或從我的應用程式根目錄啟動的普通服務器。
碼頭工人輸出:
web_1 | * Listening on http://0.0.0.0:2300
本地機器上的應用服務器輸出:
* Listening on http://127.0.0.1:2300
* Listening on http://[::1]:2300
這可能是問題嗎?
uj5u.com熱心網友回復:
不要將您的連接稱為 localhost
Cannot assign requested address Is the server running on host "localhost"
從您的應用程式中參考您在 docker 中作業的容器名稱,該名稱db
具有自己的網路,因此當您使用時,localhost您正在參考您的應用程式未在其上運行的主機網路
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/415500.html
標籤:
