Docker:端口5342上的TCP/IP连接?连接到服务器:拒绝连接是在主机上运行的服务器

发布于 2025-01-20 12:23:24 字数 1895 浏览 5 评论 0 原文

我正在使用 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 连接?

如何解决这个问题,任何建议都会有帮助,提前致谢。

I am using rails 3.0.0 with rails 7.

My dockerfile is as:

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 is as:

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 file is as:

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"] %>

using ubunut 20.04 LTS.
getting error as:
ActiveRecord::ConnectionNotEstablished
could not connect to server: Connection refused
Is the server running on host "172.22.0.3" and accepting
TCP/IP connections on port 5433?

How to resolve this issue, any suggestion would help, thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2025-01-27 12:23:24

您的 web 服务正在使用内部 docker 网络与 db (postgresql) 服务连接;这样做时,它直接连接到容器的 IP 地址,该容器(对于 postgresql)正在侦听端口 5432。通过 Docker 网络的连接不需要发布/公开端口,因此不使用 5433:5432 端口映射。

我应该补充一点,您的撰写文件正在发布(端口映射)dbredis 服务;发布它们意味着它们可以在主机的网络接口上公开访问(如果您不在内部网络上,可以在互联网上公开访问)。

确保发布必须可公开访问(或通过其他方式保护)的服务的端口;如上所述,容器/服务之间的连接不需要需要发布这些端口。

Your web service is connecting with the db (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 port 5432. Connections over the docker network does not require ports to be published / exposed, so the 5433:5432 port-mapping is not used for that.

I should add that your compose file is publishing (port-mapping) both the db and redis 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.

朮生 2025-01-27 12:23:24

从上述共同建议到达解决方案:

docker-compose.yml

 version: '3.8'
    services:
      db:
        image: postgres:latest
        environment:
          POSTGRES_PASSWORD: password
          command: -p 5000
          ports: "5000: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:5432/postgres
          - REDIS_URL=redis://redis:6379
        volumes:
          - .:/app
    
    volumes:
      dbdata:

数据库

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

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"] %>

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


请更新如何做得更好,谢谢。

reached to solution from above shared suggestions:

docker-compose.yml

 version: '3.8'
    services:
      db:
        image: postgres:latest
        environment:
          POSTGRES_PASSWORD: password
          command: -p 5000
          ports: "5000: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:5432/postgres
          - REDIS_URL=redis://redis:6379
        volumes:
          - .:/app
    
    volumes:
      dbdata:

database.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

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"] %>

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

Now with these configurations rails app is running.
please update how to do it better, thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文