在docker-compose之后无法连接到Postgres。

时间:2017-01-02 21:38:15

标签: ruby-on-rails postgresql docker-compose

我正在尝试使用大量在线教程来Docker化rails应用程序。我已经达到了可以使用docker-compose up成功启动docker容器的程度。但是在那之后,我无法连接到我的数据库。以下是我的docker-compose up输出:

docker-compose up
Pulling redis (redis:latest)...
latest: Pulling from library/redis
75a822cd7888: Pull complete
e40c2fafe648: Pull complete
ce384d4aea4f: Pull complete
5e29dd684b84: Pull complete
29a3c975c335: Pull complete
a405554540f9: Pull complete
4b2454731fda: Pull complete
Digest: sha256:eed4da4937cb562e9005f3c66eb8c3abc14bb95ad497c03dc89d66bcd172fc7f
Status: Downloaded newer image for redis:latest
Pulling postgres (postgres:9.5.4)...
9.5.4: Pulling from library/postgres
43c265008fae: Pull complete
215df7ad1b9b: Pull complete
833a4a9c3573: Pull complete
e5716357a052: Pull complete
6552dfce18a3: Pull complete
b75b371d1e9f: Pull complete
ecc63fd465b8: Pull complete
8eb11995a95a: Pull complete
9c82fb17fc44: Pull complete
389787480cc2: Pull complete
01988d09a399: Pull complete
Digest: sha256:1480f2446dabb1116fafa426ac530d2404277873a84dc4a4d0d9d4b37a5601e8
Status: Downloaded newer image for postgres:9.5.4
Creating redis
Creating postgres
Attaching to postgres, redis
postgres    | The files belonging to this database system will be owned by user "postgres".
postgres    | This user must also own the server process.
postgres    | 
redis       | 1:C 02 Jan 21:08:36.583 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis       |                 _._                                                  
postgres    | The database cluster will be initialized with locale "en_US.utf8".
redis       |            _.-``__ ''-._                                             
postgres    | The default database encoding has accordingly been set to "UTF8".
redis       |       _.-``    `.  `_.  ''-._           Redis 3.2.6 (00000000/0) 64 bit
postgres    | The default text search configuration will be set to "english".
redis       |   .-`` .-```.  ```\/    _.,_ ''-._                                   
redis       |  (    '      ,       .-`  | `,    )     Running in standalone mode
postgres    | 
redis       |  |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
postgres    | Data page checksums are disabled.
redis       |  |    `-._   `._    /     _.-'    |     PID: 1
postgres    | 
postgres    | fixing permissions on existing directory /var/lib/postgresql/data ... ok
redis       |   `-._    `-._  `-./  _.-'    _.-'                                   
redis       |  |`-._`-._    `-.__.-'    _.-'_.-'|                                  
postgres    | creating subdirectories ... ok
redis       |  |    `-._`-._        _.-'_.-'    |           http://redis.io        
redis       |   `-._    `-._`-.__.-'_.-'    _.-'                                   
redis       |  |`-._`-._    `-.__.-'    _.-'_.-'|                                  
redis       |  |    `-._`-._        _.-'_.-'    |                                  
redis       |   `-._    `-._`-.__.-'_.-'    _.-'                                   
postgres    | selecting default max_connections ... 100
redis       |       `-._    `-.__.-'    _.-'                                       
redis       |           `-._        _.-'                                           
redis       |               `-.__.-'                                               
redis       | 
redis       | 1:M 02 Jan 21:08:36.584 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
postgres    | selecting default shared_buffers ... 128MB
postgres    | selecting dynamic shared memory implementation ... posix
redis       | 1:M 02 Jan 21:08:36.584 # Server started, Redis version 3.2.6
redis       | 1:M 02 Jan 21:08:36.584 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis       | 1:M 02 Jan 21:08:36.584 * The server is now ready to accept connections on port 6379
postgres    | creating configuration files ... ok
postgres    | creating template1 database in /var/lib/postgresql/data/base/1 ... ok
postgres    | initializing pg_authid ... ok
postgres    | initializing dependencies ... ok
postgres    | creating system views ... ok
postgres    | loading system objects' descriptions ... ok
postgres    | creating collations ... ok
postgres    | creating conversions ... ok
postgres    | creating dictionaries ... ok
postgres    | setting privileges on built-in objects ... ok
postgres    | creating information schema ... ok
postgres    | loading PL/pgSQL server-side language ... ok
postgres    | vacuuming database template1 ... ok
postgres    | copying template1 to template0 ... ok
postgres    | copying template1 to postgres ... ok
postgres    | syncing data to disk ... ok
postgres    | 
postgres    | Success. You can now start the database server using:
postgres    | 
postgres    |     pg_ctl -D /var/lib/postgresql/data -l logfile start
postgres    | 
postgres    | 
postgres    | WARNING: enabling "trust" authentication for local connections
postgres    | You can change this by editing pg_hba.conf or using the option -A, or
postgres    | --auth-local and --auth-host, the next time you run initdb.
postgres    | ****************************************************
postgres    | WARNING: No password has been set for the database.
postgres    |          This will allow anyone with access to the
postgres    |          Postgres port to access your database. In
postgres    |          Docker's default configuration, this is
postgres    |          effectively any other container on the same
postgres    |          system.
postgres    | 
postgres    |          Use "-e POSTGRES_PASSWORD=password" to set
postgres    |          it in "docker run".
postgres    | ****************************************************
postgres    | waiting for server to start....LOG:  database system was shut down at 2017-01-02 21:08:37 UTC
postgres    | LOG:  MultiXact member wraparound protections are now enabled
postgres    | LOG:  database system is ready to accept connections
postgres    | LOG:  autovacuum launcher started
postgres    |  done
postgres    | server started
postgres    | CREATE DATABASE
postgres    | 
postgres    | ALTER ROLE
postgres    | 
postgres    | 
postgres    | /docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgres    | 
postgres    | LOG:  received fast shutdown request
postgres    | waiting for server to shut down...LOG:  aborting any active transactions
postgres    | .LOG:  autovacuum launcher shutting down
postgres    | LOG:  shutting down
postgres    | LOG:  database system is shut down
postgres    |  done
postgres    | server stopped
postgres    | 
postgres    | PostgreSQL init process complete; ready for start up.
postgres    | 
postgres    | LOG:  database system was shut down at 2017-01-02 21:08:39 UTC
postgres    | LOG:  MultiXact member wraparound protections are now enabled
postgres    | LOG:  database system is ready to accept connections
postgres    | LOG:  autovacuum launcher started
postgres    | FATAL:  role "boguthrie" does not exist
postgres    | FATAL:  role "boguthrie" does not exist
postgres    | FATAL:  role "user" does not exist

你可以在最终输出中看到我在我的database.yml中尝试了许多不同的用户角色,我知道存在(例如,当我使用Postgres应用程序时,我可以使用这些角色成功访问db)。当我尝试使用psql <dbname>psql -U user -d <dbname> -h localhost查看正在运行的数据库时出现以下错误。

psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

最后,这里有潜在的相关文件。

的database.yml

# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

  database: example

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user that initialized the database.
  username: boguthrie

  # The password associated with the postgres role (username).
  password:

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

development:
  <<: *default

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: example_test

搬运工-compose.yml

version: '2'
services:
  postgres:
    container_name: a
    image: postgres:9.5.4
    environment:
      POSTGRES_PASSWORD:
      POSTGRES_USER:
      POSTGRES_DB: example
    ports:
      - "5432:5432"
  redis:
    container_name: redis
    image: redis
    ports:
      - "6379:6379"

Dockerfile

# The following are in the Dockerfile instructions

# The first non-comment instruction must be `FROM` in order to specify the Base Image from which you are building.
#   'FROM' can appear multiple times within a single Dockerfile in order to create multiple images.
#   Simply make a note of the last image ID output by the commit before each new FROM command.
FROM ruby:2.3

MAINTAINER Bo

# The LABEL instruction adds metadata to an image.
#   A LABEL is a key-value pair.
#   To include spaces within a LABEL value, use quotes and backslashes as you would in command-line parsing.
#   User docker inspect command to see labels.
LABEL version="0.1"
LABEL description="Example App"

# 'RUN' has two forms:
#   The shell form or the executable form. All of the run commands in this file are in the shell form.

# This will throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1

# Here we're creating the directory /usr/src/app and using it as or working directory.
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

RUN apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y mysql-client postgresql-client sqlite3 --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y imagemagick --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y graphviz --no-install-recommends && rm -rf /var/lib/apt/lists/*

# The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.
COPY Gemfile /usr/src/app/
COPY Gemfile.lock /usr/src/app/

RUN bundle install

COPY . /usr/src/app

# The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime.
#   EXPOSE does not make the ports of the container accessible to the host.
EXPOSE 3000

# The main purpose of a CMD is to provide defaults for an executing container.
#   These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.
#   There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.
#   Example common usage: CMD ["rails", "server", "-b", "0.0.0.0", "-P", "/tmp/server.pid"]. This will store the pid in a location not persisted between boots

# Define the script we want run once the container boots
#   Use the "exec" form of CMD so our script shuts down gracefully on SIGTERM (i.e. `docker stop`)
CMD [ "config/containers/app_cmd.sh" ]

这里的任何帮助将不胜感激。谢谢你的时间。

1 个答案:

答案 0 :(得分:1)

您的角色不存在。这是因为没有在docker-compose.yml文件中设置POSTGRES_USER。如果设置该值并重新创建容器,则将创建该容器。 POSTGRES_USER需要匹配rails。的database.yml文件中的用户。