Rav*_*dav 6 ruby-on-rails amazon-ec2 docker ruby-on-rails-5 docker-compose
我在使用Ruby on Rail的本地系统上设置和运行docker实例时遇到问题。请查看我的docker配置文件:-
Docker文件
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
Run Code Online (Sandbox Code Playgroud)
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:
Run Code Online (Sandbox Code Playgroud)
当我运行docker-compose build和docker-compose up -d这两个命令时,它会成功运行,但是当我从url中命中时,它会导致内部服务器错误,错误是
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?
Run Code Online (Sandbox Code Playgroud)
我已经应用了一些解决方案,但是它对我不起作用,请指导我,我是docker和AWS的新手。
问题是您正在尝试连接到DB容器内的localhost。您5432:5432为postgres 做的端口映射将5432映射到主机的localhost。
现在,您的web容器代码正在容器内运行。它的本地主机上没有任何内容:5432。
因此,您需要在配置中更改要连接的连接详细信息postgres:5432,这是因为您将postgres DB服务命名为postgres
更改它,它应该可以工作。
默认情况下,postgres 图像已经暴露给 5432,因此您可以在 yml 中删除该部分。
然后,如果您想检查 Web 服务是否可以连接到您的 postgres 服务,您可以运行它,docker-compose exec web curl postgres:5432那么它应该返回:
curl: (52) 来自服务器的空回复
如果无法连接,它将返回:
curl:(6) 无法解析主机:postgres或curl:(7) 无法连接到 postgres 端口 5432:连接被拒绝
更新:
我现在知道问题所在了。这是因为您尝试在本地主机上进行连接,因此您应该连接到该postgres服务。
| 归档时间: |
|
| 查看次数: |
4986 次 |
| 最近记录: |