Docker:服务器是否在主机“localhost"上运行?(::1) 并接受端口 5432 上的 TCP/IP 连接?

编程入门 行业动态 更新时间:2024-10-25 22:29:38
本文介绍了Docker:服务器是否在主机“localhost"上运行?(::1) 并接受端口 5432 上的 TCP/IP 连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用 Ruby on Rail 在本地系统上设置和运行 docker 实例时遇到问题.请查看我的 docker 配置文件:-

I am getting issues while setup and run the docker instance on my local system with Ruby on Rail. Please see my docker configuration files:-

Dockerfile

FROM ruby:2.3.1 RUN useradd -ms /bin/bash web RUN apt-get update -qq && apt-get install -y build-essential RUN apt-get -y install nginx RUN apt-get -y install sudo # for postgres RUN apt-get install -y libpq-dev # for nokogiri RUN apt-get install -y libxml2-dev libxslt1-dev # for a JS runtime RUN apt-get install -y nodejs RUN apt-get update # For docker cache WORKDIR /tmp ADD ./Gemfile Gemfile ADD ./Gemfile.lock Gemfile.lock ENV BUNDLE_PATH /bundle RUN gem install bundler --no-rdoc --no-ri RUN bundle install # END ENV APP_HOME /home/web/cluetap_app RUN mkdir -p $APP_HOME WORKDIR $APP_HOME ADD . $APP_HOME RUN chown -R web:web $APP_HOME ADD ./nginx/nginx.conf /etc/nginx/ RUN unlink /etc/nginx/sites-enabled/default ADD ./nginx/cluetap_nginx.conf /etc/nginx/sites-available/ RUN ln -s /etc/nginx/sites-available/cluetap_nginx.conf /etc/nginx/sites-enabled/cluetap_nginx.conf RUN usermod -a -G sudo web

docker-compose.yml

docker-compose.yml

version: '2' services: postgres: image: 'postgres:9.6' environment: - PGDATA=/var/lib/postgresql/data/pgdata - POSTGRES_PASSWORD= - POSTGRES_USER=postgres - POSTGRES_HOST=cluetapapi_postgres_1 networks: - default - service-proxy ports: - '5432:5432' volumes: - 'postgres:/var/lib/postgresql/data' labels: description: "Postgresql Database" service: "postgresql" web: container_name: cluetap_api build: . command: bash -c "thin start -C config/thin/development.yml && nginx -g 'daemon off;'" volumes: - .:/home/web/cluetap_app ports: - "80:80" depends_on: - 'postgres' networks: service-proxy: volumes: postgres:

当我运行 docker-compose build 和 docker-compose up -d 这两个命令时,它运行成功,但是当我从 url 中点击它时,它通过 内部服务器错误,错误是

When I have run docker-compose build and docker-compose up -d these two commands it run succesfully but when I have hit from the url the it thorught internal server error and the error is

Unexpected error while processing request: 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?

我已经应用了一些解决方案,但它对我不起作用,请指导我我是 docker 和 AWS 的新手.

I have applied some solution but it did not work for me, please guide me I am new to docker and AWS.

推荐答案

问题是您正在尝试连接到 DB 容器内的 localhost.您为 postgres 执行的端口映射 5432:5432 将 5432 映射到主机的 localhost.

The issue is that you are trying to connect to localhost inside the container for DB. The port mapping that you do 5432:5432 for postgres map 5432 to localhost of your host machine.

现在您的 web 容器代码正在容器内运行.它的 localhost:5432 上什么也没有.

Now your web container code is running inside the container. And there is nothing on its localhost:5432.

因此您需要更改配置中的连接详细信息以连接到 postgres:5432,这是因为您将 postgres 数据库服务命名为 postgres

So you need to change your connection details in the config to connect to postgres:5432 and this is because you named the postgres DB service as postgres

改变它,它应该可以工作.

Change that and it should work.

更多推荐

Docker:服务器是否在主机“localhost"上运行?(::1) 并接受端口 5432 上的 TCP/IP 连接?

本文发布于:2023-10-15 00:50:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492748.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:端口   主机   服务器   localhost   Docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!