在 docker-compose 中设置 postgres 和应用程序服务后,无法访问 Postgres
After setting up postgres and app service in docker-compose the Postgres is not reachable
我正在尝试使用 Postgres 作为数据库为 Hanami 网络应用程序设置多服务 docker 环境。我正在关注 Docker 和其他资源来这样做。我目前有两项服务:postgres 和 app。我平移为 nginx 添加一个单独的。在我 运行 docker-compose up
之后,一切“似乎”都正常,但是在我访问本地浏览器中的 url 之后,我收到了关于 Postgres 的错误。
以下是每个文件的所有详细信息:
编辑 1>>>
Docker文件
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:
启动日志
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 宁 运行 或从我的应用程序根目录启动普通服务器时的终端输出。
Docker 输出:
web_1 | * Listening on http://0.0.0.0:2300
本地计算机上的应用程序服务器输出:
* Listening on http://127.0.0.1:2300
* Listening on http://[::1]:2300
这可能是问题所在吗?
不要将您的连接称为 localhost
Cannot assign requested address Is the server running on host "localhost"
从您的应用引用容器名称db
你在 docker 内部工作,它有自己的网络,所以当你使用 localhost
时,你引用的是你的应用程序不在 运行 上的主机网络
我正在尝试使用 Postgres 作为数据库为 Hanami 网络应用程序设置多服务 docker 环境。我正在关注 Docker 和其他资源来这样做。我目前有两项服务:postgres 和 app。我平移为 nginx 添加一个单独的。在我 运行 docker-compose up
之后,一切“似乎”都正常,但是在我访问本地浏览器中的 url 之后,我收到了关于 Postgres 的错误。
以下是每个文件的所有详细信息:
编辑 1>>>
Docker文件
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:
启动日志
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 宁 运行 或从我的应用程序根目录启动普通服务器时的终端输出。
Docker 输出:
web_1 | * Listening on http://0.0.0.0:2300
本地计算机上的应用程序服务器输出:
* Listening on http://127.0.0.1:2300
* Listening on http://[::1]:2300
这可能是问题所在吗?
不要将您的连接称为 localhost
Cannot assign requested address Is the server running on host "localhost"
从您的应用引用容器名称db
你在 docker 内部工作,它有自己的网络,所以当你使用 localhost
时,你引用的是你的应用程序不在 运行 上的主机网络