I did Docker construction of rails + postgresql as official, but it didn't work, so I fixed some

Premise

macOS Catalina Docker for Mac 2.3.0.5

official

Quickstart: Compose and Rails

code

Dockerfile When I tried to follow the official description, I suddenly got various errors, so I added something that was missing from Gugu. Besides, the official ruby is version 2.5, but I made it the latest 2.7.2 on 2020/11/1

official

FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

FROM ruby:2.7.2
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
    && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client yarn
#same as below

Gemfile Officially, the version of rails is 6, but I made it the latest 6 on 2020/11/1

official

source 'https://rubygems.org'
gem 'rails', '~>5'

source 'https://rubygems.org'
gem 'rails', '~> 6.0.3', '>= 6.0.3.4'

docker-compose.yml

For some reason, the DB wasn't made permanent even if I followed the official and other articles, but I was at a loss, but I solved it by this method. https://github.com/docker/compose/issues/5012#issuecomment-316518185

(official)docker-compose.yml


version: "3"
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: password
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

docker-compose.yml


version: '3'
services: 
  db:
    image: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      PGDATA: /var/postgres_data #add to
    volumes: 
      - ./tmp/db:/var/postgres_data #Fix
    ports:
      - "5432:5432"
  web: #the same

ports: "3001:3000"


 When connecting to the rails server, specify 3001.
 (Or change the port on the front side)

## procedure
 Please refer to this video for the basic procedure because it was easy to understand.
 [[Rails environment construction] environment construction with docker + rails + mysql (even beginners can complete in 30 minutes!)](Https://www.youtube.com/watch?v=Fq1PH0Gwi8I)
 * This video is rails5, ruby2.5 with mysql instead of postgresql

## DB connection

 * You cannot connect unless the container is started.
 ![スクリーンショット 2020-11-03 9.55.57.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/566220/2a8fc39b-a393-d577-f135-21c61a2d8697.png)

 `Name` Freedom
 ʻUser` Set what is described in POSTGRES_USER in docker-compose.yml
 `Password` Set what is described in POSTGRES_PASSWORD in docker-compose.yml
 Set what is described in `development: database` of` Database` `myapp / config / database.yml`


Recommended Posts

I did Docker construction of rails + postgresql as official, but it didn't work, so I fixed some
I introduced Docker to Rails 6, so I summarized it (beginner)
I didn't understand the meaning of injection such as DI or @Autowired, so I looked it up.
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
I used it without knowing the O / R mapping of rails, so I checked it.