Docker无法与Postgres连接

时间:2019-05-21 10:01:57

标签: ruby-on-rails postgresql docker

我有一个Rails应用,该应用使用docker来运行该应用,甚至用于开发。

这是docker-compose文件

web:
  build: .
  dockerfile: Dockerfile.development
  environment:
    - ALGOLIA_API_KEY
    - ALGOLIA_APP_ID
    - ALGOLIA_INDEX
    - ALGOLIA_SEARCH_KEY
    - AWS_ACCESS_KEY
    - AWS_S3_BUCKET_NAME
    - AWS_SECRET_ACCESS_KEY
    - COVERAGE
    - DEV_EMAIL
    - INTERCOM_API_KEY
    - INTERCOM_API_SECRET
    - INTERCOM_APP_ID
    - LABS_MAILGUN_PASSWORD
    - LABS_MAILGUN_USERNAME
    - MAILGUN_API_KEY
    - MAILGUN_DOMAIN
    - LOB_API_KEY
    - LOG_LEVEL
    - MAX_THREADS
    - NEW_RELIC_KEY
    - OUTBOUND_KEY
    - PAID_API_BASE
    - PAID_DEFAULT_PLAN_ID
    - PAID_PUBLIC_API_BASE
    - PAID_PUBLISHABLE_KEY
    - PAID_SECRET_KEY
    - PLAID_CLIENT_ID
    - PLAID_PUBLIC_KEY
    - PLAID_SECRET_KEY
    - PLATFORM_API_TOKEN
    - RACK_ENV=development
    - RACK_TIMEOUT
    - RAILS_ENV=development
    - REDISCLOUD_URL
    - REDIS_PROVIDER
    - RESQUE_LOG_LEVEL
    - SECRET_KEY_BASE
    - SECRET_KEY_BASE_PRODUCTION
    - SECRET_KEY_BASE_STAGING
    - STRIPE_ACCOUNT_ID
    - STRIPE_CLIENT_ID
    - STRIPE_PUBLISHABLE_KEY
    - STRIPE_SECRET_KEY
    - STRIPE_TEST_CLIENT_ID
    - STRIPE_TEST_PUBLISHABLE_KEY
    - STRIPE_TEST_SECRET_KEY
    - TEST_ENV_NUMBER
    - UNICORN_TIMEOUT
    - VERBOSE
    - WEB_CONCURRENCY
  labels:
    - convox.port.543.protocol=tls
    - convox.port.543.proxy=true
    - convox.port.90.proxy=true
  links:
    - database
    - redis
  ports:
    - 90:4000
    - 543:4001

database:
  image: convox/postgres
  ports:
    - 5432
  volumes:
    - /Users/vishal.awathare/Library/Application\ Support/Postgres/var-11

database-test:
  image: convox/postgres
  ports:
    - 5432
  volumes:
    - /Users/vishal.awathare/Library/Application\ Support/Postgres/var-11

redis:
  environment:
    - RACK_ENV=development
    - RACK_TIMEOUT
    - RAILS_ENV=development
  image: convox/redis
  ports:
    - 6379

resque:
  build: .
  dockerfile: Dockerfile.development
  command: bundle exec rake resque:work
  environment:
    - ALGOLIA_API_KEY
    - ALGOLIA_APP_ID
    - ALGOLIA_INDEX
    - ALGOLIA_SEARCH_KEY
    - AWS_ACCESS_KEY
    - AWS_S3_BUCKET_NAME
    - AWS_SECRET_ACCESS_KEY
    - COVERAGE
    - DEV_EMAIL
    - INTERCOM_API_KEY
    - INTERCOM_API_SECRET
    - INTERCOM_APP_ID
    - LABS_MAILGUN_PASSWORD
    - LABS_MAILGUN_USERNAME
    - MAILGUN_API_KEY
    - MAILGUN_DOMAIN
    - LOB_API_KEY
    - LOG_LEVEL
    - MAX_THREADS
    - NEW_RELIC_KEY
    - OUTBOUND_KEY
    - PAID_API_BASE
    - PAID_DEFAULT_PLAN_ID
    - PAID_PUBLIC_API_BASE
    - PAID_PUBLISHABLE_KEY
    - PAID_SECRET_KEY
    - PLAID_CLIENT_ID
    - PLAID_PUBLIC_KEY
    - PLAID_SECRET_KEY
    - PLATFORM_API_TOKEN
    - QUEUE=*
    - RACK_ENV=development
    - RACK_TIMEOUT
    - RAILS_ENV=development
    - REDISCLOUD_URL
    - REDIS_PROVIDER
    - RESQUE_LOG_LEVEL
    - RESQUE_TERM_TIMEOUT=10
    - SECRET_KEY_BASE
    - SECRET_KEY_BASE_PRODUCTION
    - SECRET_KEY_BASE_STAGING
    - STRIPE_ACCOUNT_ID
    - STRIPE_CLIENT_ID
    - STRIPE_PUBLISHABLE_KEY
    - STRIPE_SECRET_KEY
    - STRIPE_TEST_CLIENT_ID
    - STRIPE_TEST_PUBLISHABLE_KEY
    - STRIPE_TEST_SECRET_KEY
    - TERM_CHILD=1
    - TEST_ENV_NUMBER
    - UNICORN_TIMEOUT
    - VERBOSE
    - WEB_CONCURRENCY
  links:
    - database
    - redis

clock:
  build: .
  dockerfile: Dockerfile.development
  command: bundle exec rake resque:scheduler
  environment:
    - ALGOLIA_API_KEY
    - ALGOLIA_APP_ID
    - ALGOLIA_INDEX
    - ALGOLIA_SEARCH_KEY
    - AWS_ACCESS_KEY
    - AWS_S3_BUCKET_NAME
    - AWS_SECRET_ACCESS_KEY
    - COVERAGE
    - DEV_EMAIL
    - INTERCOM_API_KEY
    - INTERCOM_API_SECRET
    - INTERCOM_APP_ID
    - LABS_MAILGUN_PASSWORD
    - LABS_MAILGUN_USERNAME
    - MAILGUN_API_KEY
    - MAILGUN_DOMAIN
    - LOB_API_KEY
    - LOG_LEVEL
    - MAX_THREADS
    - NEW_RELIC_KEY
    - OUTBOUND_KEY
    - PAID_API_BASE
    - PAID_DEFAULT_PLAN_ID
    - PAID_PUBLIC_API_BASE
    - PAID_PUBLISHABLE_KEY
    - PAID_SECRET_KEY
    - PLAID_CLIENT_ID
    - PLAID_PUBLIC_KEY
    - PLAID_SECRET_KEY
    - PLATFORM_API_TOKEN
    - RACK_ENV=development
    - RACK_TIMEOUT
    - RAILS_ENV=development
    - REDISCLOUD_URL
    - REDIS_PROVIDER
    - RESQUE_LOG_LEVEL
    - SECRET_KEY_BASE
    - SECRET_KEY_BASE_PRODUCTION
    - SECRET_KEY_BASE_STAGING
    - STRIPE_ACCOUNT_ID
    - STRIPE_CLIENT_ID
    - STRIPE_PUBLISHABLE_KEY
    - STRIPE_SECRET_KEY
    - STRIPE_TEST_CLIENT_ID
    - STRIPE_TEST_PUBLISHABLE_KEY
    - STRIPE_TEST_SECRET_KEY
    - TEST_ENV_NUMBER
    - UNICORN_TIMEOUT
    - VERBOSE
    - WEB_CONCURRENCY
  links:
    - database
    - redis

test:
  build: .
  dockerfile: Dockerfile.development
  environment:
    - ALGOLIA_API_KEY
    - ALGOLIA_APP_ID
    - ALGOLIA_INDEX
    - ALGOLIA_SEARCH_KEY
    - AWS_ACCESS_KEY
    - AWS_S3_BUCKET_NAME
    - AWS_SECRET_ACCESS_KEY
    - COVERAGE
    - DEV_EMAIL
    - INTERCOM_API_KEY
    - INTERCOM_API_SECRET
    - INTERCOM_APP_ID
    - LABS_MAILGUN_PASSWORD
    - LABS_MAILGUN_USERNAME
    - MAILGUN_API_KEY
    - MAILGUN_DOMAIN
    - LOB_API_KEY
    - LOG_LEVEL
    - MAX_THREADS
    - NEW_RELIC_KEY
    - OUTBOUND_KEY
    - PAID_API_BASE
    - PAID_DEFAULT_PLAN_ID
    - PAID_PUBLIC_API_BASE
    - PAID_PUBLISHABLE_KEY
    - PAID_SECRET_KEY
    - PLAID_CLIENT_ID
    - PLAID_PUBLIC_KEY
    - PLAID_SECRET_KEY
    - PLATFORM_API_TOKEN
    - RACK_ENV=test
    - RACK_TIMEOUT
    - RAILS_ENV=test
    - REDISCLOUD_URL
    - REDIS_PROVIDER
    - RESQUE_LOG_LEVEL
    - SECRET_KEY_BASE
    - SECRET_KEY_BASE_PRODUCTION
    - SECRET_KEY_BASE_STAGING
    - STRIPE_ACCOUNT_ID
    - STRIPE_CLIENT_ID
    - STRIPE_PUBLISHABLE_KEY
    - STRIPE_SECRET_KEY
    - STRIPE_TEST_CLIENT_ID
    - STRIPE_TEST_PUBLISHABLE_KEY
    - STRIPE_TEST_SECRET_KEY
    - TEST_ENV_NUMBER
    - UNICORN_TIMEOUT
    - VERBOSE
    - WEB_CONCURRENCY
  links:
    - database-test
    - redis

admin:
  build: .
  dockerfile: Dockerfile.development
  environment:
    - ALGOLIA_API_KEY
    - ALGOLIA_APP_ID
    - ALGOLIA_INDEX
    - ALGOLIA_SEARCH_KEY
    - AWS_ACCESS_KEY
    - AWS_S3_BUCKET_NAME
    - AWS_SECRET_ACCESS_KEY
    - COVERAGE
    - DEV_EMAIL
    - INTERCOM_API_KEY
    - INTERCOM_API_SECRET
    - INTERCOM_APP_ID
    - LABS_MAILGUN_PASSWORD
    - LABS_MAILGUN_USERNAME
    - MAILGUN_API_KEY
    - MAILGUN_DOMAIN
    - LOB_API_KEY
    - LOG_LEVEL
    - MAX_THREADS
    - NEW_RELIC_KEY
    - OUTBOUND_KEY
    - PAID_API_BASE
    - PAID_DEFAULT_PLAN_ID
    - PAID_PUBLIC_API_BASE
    - PAID_PUBLISHABLE_KEY
    - PAID_SECRET_KEY
    - PLAID_CLIENT_ID
    - PLAID_PUBLIC_KEY
    - PLAID_SECRET_KEY
    - PLATFORM_API_TOKEN
    - RACK_ENV=development
    - RACK_TIMEOUT
    - RAILS_ENV=development
    - REDISCLOUD_URL
    - REDIS_PROVIDER
    - RESQUE_LOG_LEVEL
    - SECRET_KEY_BASE
    - SECRET_KEY_BASE_PRODUCTION
    - SECRET_KEY_BASE_STAGING
    - STRIPE_ACCOUNT_ID
    - STRIPE_CLIENT_ID
    - STRIPE_PUBLISHABLE_KEY
    - STRIPE_SECRET_KEY
    - STRIPE_TEST_CLIENT_ID
    - STRIPE_TEST_PUBLISHABLE_KEY
    - STRIPE_TEST_SECRET_KEY
    - TEST_ENV_NUMBER
    - UNICORN_TIMEOUT
    - VERBOSE
    - WEB_CONCURRENCY
  links:
    - database
    - redis

这是我的dockerfile.development文件

FROM location/app-base-image

# Docker won't let us use $HOME in `ENV PATH=...` without setting HOME first
ENV HOME /root
ENV PATH $HOME/.rbenv/shims:$PATH

RUN rm -rf `rbenv root`
RUN git clone https://github.com/rbenv/rbenv.git /root/.rbenv
RUN mkdir -p "$(rbenv root)"/plugins
RUN git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build

RUN rbenv install 2.3.7
RUN rbenv global 2.3.7
RUN rbenv local 2.3.7

RUN gem install bundler -v 1.13.1 && rbenv rehash

COPY .convox/bin/web bin/web
COPY .convox/conf/nginx.conf /etc/nginx/nginx.conf

COPY Gemfile ./Gemfile
COPY Gemfile.lock ./Gemfile.lock
RUN bundle install && rbenv rehash

ENV DATABASE_URL postgres://host
ENV ALGOLIA_APP_ID FOO
ENV REDIS_URL redis://host

COPY . .

CMD ["bin/web"]

我先跑

 docker-compose -f docker-compose.development.yml build

它成功运行。然后我运行

docker-compose -f docker-compose.development.yml up

这给了我错误

admin_1          | E, [2019-05-21T09:58:40.069558 #8] ERROR -- : could not translate host name "host" to address: Name or service not known
admin_1          |  (PG::ConnectionBad)

我注意到环境变量可能设置错误。因此,在dockerfile中,我将ENV DATABASE_URL postgres://host更改为ENV DATABASE_URL postgres://localhost:5432。错误消息然后变为

Is the server running on host "localhost" (127.0.0.1) and accepting     TCP/IP connections on port 5432?

我缺少基本的东西吗?

1 个答案:

答案 0 :(得分:1)

您的数据库服务器未在运行您的应用程序的容器中运行。您的容器似乎正在名为database的容器中运行,因此有您的主机名。

我还建议为您的应用创建一个单独的网络,并将容器连接到该网络,而不是默认网络。