我刚接触Docker。
当我运行docker-compose up
时,会出现以下错误。
PG::ConnectionBad
could not translate host name "postgres" to address: Name does not resolve
在日志中,显示以下内容
postgres_1 | The database cluster will be initialized with locale "C".
postgres_1 | The default database encoding has accordingly been set to "SQL_ASCII".
postgres_1 | The default text search configuration will be set to "english".
postgres_1 |
postgres_1 | Data page checksums are disabled.
postgres_1 |
postgres_1 | initdb: directory "/var/lib/postgresql/data" exists but is not empty
postgres_1 | If you want to create a new database system, either remove or empty
postgres_1 | the directory "/var/lib/postgresql/data" or run initdb
postgres_1 | with an argument other than "/var/lib/postgresql/data".
这是我的docker-compose设置。
# docker-compose.yml
version: '3'
services:
postgres:
image: postgres:9.6-alpine
volumes:
- postgresql-data:/var/lib/postgresql/data
ports:
- "5432:5432"
app:
build:
context: .
command: "bundle exec rails server -b 0.0.0.0"
ports:
- "3020:3000"
depends_on:
- postgres
volumes:
postgresql-data:
# Dockerfile
FROM ruby:2.3.7-alpine
ENV LANG C.UTF-8
ENV RAILS_ENV=development
RUN apk add --update --no-cache --virtual=builders \
alpine-sdk build-base linux-headers ruby-dev zlib-dev postgresql-dev
RUN apk add --update --no-cache \
libc6-compat libc-dev zlib ruby-json tzdata yaml less curl postgresql
RUN mkdir /app
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app
# database.yml
default: &default
adapter: postgresql
encoding: utf8
host: postgres
username: postgres
password:
port: 5432
development:
<<: *default
template: template0
database: todo_development
我在这里做什么错了?
如果我将host
中的postgres
从localhost
更改为database.yml
,则会收到以下错误消息。
Connection refused Is the server running on host “localhost” (127.0.0.1)