docker-compose でRails+Postgresql開発環境構築
docker-compose でRails+Postgresqlの二つのコンテナを実行して
開発環境を準備しました。
db:migrateを実行したところ、成功せずに
下記のエラーが発生してしまいます。
docker-compose run web rake db:migrateで発生のエラー
rake aborted! PG::ConnectionBad: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
調べたところDBのサービスが立ち上がってないとのことだったので下記を試したのですがエラーです。
docker-compose run db service postgresql startで発生のエラー
[warn] No PostgreSQL clusters exist; see "man pg_createcluster" ... (warning).
docker ps ではwebコンテナもdbコンテナも起動しています
docker-compose up -d でコンテナを起動すれば
DBサービスもWebサービスも起動して
連携して使えるようになる認識ですが他にもコマンドが必要なのでしょうか
あるいは設定等に不備があるのかと思うのですが
原因がつかめず困っています。
以下にに設定ファイルを載せておきます。
原因と解決策がわからないので
教えて頂きたいです。
難しければ
ヒントでも構いません。
足りない情報があれば、言っていただければ追記いたします。
よろしくお願いいたします。
Dockerfile
FROM ruby:2.5.3 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs 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: unicode pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> development: <<: *default database: e_development test: <<: *default database: e_test production: <<: *default database: e_production username: e password: <%= ENV['E_DATABASE_PASSWORD'] %>
docker-compose.yml
version: '3' services: web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/app ports: - 3000:3000 depends_on: - db tty: true stdin_open: true environment: DATABASE_USER: postgres DATABASE_PASSWORD: DATABASE_PORT: 5432 DATABASE_HOST: db db: image: postgres:10 user: postgres ports: - "5432" volumes: - db-volume:/var/lib/postgres environment: MYSQL_ROOT_PASSWORD: password volumes: db-volume:
Gemfile
source 'https://rubygems.org' git_source(:github) do |repo_name| repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") "https://github.com/#{repo_name}.git" end # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.1.4' # Use postgresql as the database for Active Record gem 'pg', '~> 0.18' # Use Puma as the app server gem 'puma', '~> 3.7' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.2' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.5' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development gem 'devise' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an IRB console on exception pages or by using <%= console %> anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]