Docker:端口5342上的TCP/IP连接?连接到服务器:拒绝连接是在主机上运行的服务器
我正在使用 Rails 3.0.0 和 Rails 7。
我的 dockerfile 为:
FROM ruby:3.0.0-alpine
RUN apk add --update --virtual \
runtime-deps \
postgresql-client\
build-base \
libxml2-dev \
libxslt-dev \
yarn \
libffi-dev \
readline \
build-base \
postgresql-dev \
libc-dev \
linux-headers \
readline-dev \
file \
imagemagick \
git \
tzdata \
&& rm -rf /var/cache/apk*
WORKDIR /app
COPY . /app/
ENV BUNDLE_PATH /gems
RUN yarn install
RUN bundle install
ENTRYPOINT ["bin/rails"]
CMD ["s", "-b", "0.0.0.0"]
EXPOSE 3000
docker-compose.yml 为:
version: '3.8'
services:
db:
image: postgres:latest
environment:
- POSTGRES_PASSWORD=password
ports:
- "5433:5432"
volumes:
- "dbdata:/var/lib/postgresql/data"
redis:
image: redis:latest
ports:
- "6380:6379"
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
- redis
environment:
- DATABASE_URL=postgres://postgres:password@db:5433/postgres
- REDIS_URL=redis://redis:6380
volumes:
- .:/app
volumes:
dbdata:
database.yml 文件为:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
port: 5432
development:
<<: *default
database: my_app_development
username: postgres
password: password
test:
<<: *default
database: my_app_test
# production:
# url: <%= ENV["MY_APP_DATABASE_URL"] %>
production:
<<: *default
database: my_app_production
username: my_app
password: <%= ENV["MY_APP_DATABASE_PASSWORD"] %>
使用 ubunut 20.04 LTS。 得到错误为: ActiveRecord::连接未建立 无法连接到服务器:连接被拒绝 服务器是否在主机“172.22.0.3”上运行并接受 端口 5433 上的 TCP/IP 连接?
如何解决这个问题,任何建议都会有帮助,提前致谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
web
服务正在使用内部 docker 网络与db
(postgresql) 服务连接;这样做时,它直接连接到容器的 IP 地址,该容器(对于 postgresql)正在侦听端口5432
。通过 Docker 网络的连接不需要发布/公开端口,因此不使用5433:5432
端口映射。我应该补充一点,您的撰写文件正在发布(端口映射)
db
和redis
服务;发布它们意味着它们可以在主机的网络接口上公开访问(如果您不在内部网络上,可以在互联网上公开访问)。确保仅发布必须可公开访问(或通过其他方式保护)的服务的端口;如上所述,容器/服务之间的连接不需要需要发布这些端口。
Your
web
service is connecting with thedb
(postgresql) service using the internal docker network; when doing so, it connects directly to the IP address of the container, which (for postgresql) is listening on port5432
. Connections over the docker network does not require ports to be published / exposed, so the5433:5432
port-mapping is not used for that.I should add that your compose file is publishing (port-mapping) both the
db
andredis
services; publishing them means that they'll be publicly accessible on the host's network interface (which, if you're not on an internal network, may be publicly accessible on the internet).Make sure to only publish ports for services that must be publicly accessible (or are protected through other ways); as described above, connections between containers / services does not require those ports to be published.
从上述共同建议到达解决方案:
docker-compose.yml
数据库
。
请更新如何做得更好,谢谢。
reached to solution from above shared suggestions:
docker-compose.yml
database.yml:
Dockerfile:
Now with these configurations rails app is running.
please update how to do it better, thanks.