Home>

Build a Rails 6 environment with Docker.

version
ruby 2.6.5
rails 6.0.3

In the terminal, I get an error when I run the following:
docker build.
docker-compose exec web bash
rails new. --force --database = postgresql --skip-bundle * Error occurred here

I don't understand why the gem doesn't install properly.

run rails new. --force --database = postgresql --skip-bundle
      //abridgement
      create test/application_system_test_case.rb
      create storage
      create storage/.keep
      create tmp/storage
      create tmp/storage/.keep
      remove config/initializers/cors.rb
      remove config/initializers/new_framework_defaults_6_0.rb
       rails webpacker: install
Could not find gem'pg (>= 0.18,<2.0)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
Corresponding source code
FROM ruby: 2.6.5
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add-
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update
RUN apt-get install -y \
    build-essential \
    libpq-dev \
    nodejs \
    postgresql-client \
    yarn
WORKDIR/rails6-container
COPY Gemfile Gemfile.lock /rails6-container/
# 2 copy destinations are rails6-container
RUN bundle install
source'https://rubygems.org'
gem'rails','~>6.0.3'
version: "3"
volumes:
  db-data:
services: services:
  web:
    build: ..
    ports: ports:
      -"3000: 3000"
    volumes:
      -".:/rails6-container"
    environment: environment:
      -"DATABASE_PASSWORD = postgres"
    tty: true
    stdin_open: true
    depends_on:
      --db
    links: links:
      --db
  db:
    image: postgres
    volumes:
      -"db-data:/var/lib/postgresql/data"
    environment: environment:
      -"POSTGRES_USER = postgres"
      -"POSTGRES_PASSWORD = postgres"
      -"POSTGRES_HOST_AUTH_METHOD = trust"
What I tried

If i follow the steps below, it will work.

① Enter the container with docker-compose exec web bash and execute bundle install.

② rails new. --force --database = postgresql --skip-bundle
Delete the files and folders created when you executed in, and make only Dockerfile Gemfile Gemfil.lock Docker-compose.yml.

③ Run rails new. --force --database = postgresql --skip-bundle again.

You can do it this way.
However, since the work is troublesome, I would like to have bundle install installed in one shot. What kind of description should I make? ?? Thanks for your professor.

  • Answer # 1

    Is postgresql included?
    I think that it is common to put it in brew.

    Since the gem called pg depends on postgresql, does it mean that the gem cannot be installed without postgresql?