Docker:服务器是否在主机“ localhost”上运行? (:: 1)并在端口5432上接受TCP / IP连接? [英] Docker: Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
问题描述
在使用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
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新手
I have applied some solution but it did not work for me, please guide me I am new to docker and AWS.
推荐答案
问题是您正在尝试连接数据库容器中的localhost。您对postgres 5432:5432
执行的端口映射将5432映射到主机的本地主机。
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 DB服务命名为 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连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!