0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Dockerでrails環境構築

Last updated at Posted at 2021-04-17

Dockerの勉強用にDockerでrailsの環境構築をした
環境構築手順をまとめる

Dockerのインストール

下記URLよりDockerをダウンロードしインストールする.
https://docs.docker.com/get-docker/

image.png

インストールされていることを確認する

$ docker version                                                                [:|✔ ]
Client: Docker Engine - Community
 Cloud integration: 1.0.9
 Version:           20.10.5
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        55c4c88
 Built:             Tue Mar  2 20:13:00 2021
 OS/Arch:           darwin/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.5
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       363e9a8
  Built:            Tue Mar  2 20:15:47 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

railsの環境構築

インストールしたDockerを利用して、早速railsの環境を構築する

1. 作業用のディレクトリを作る

$ mkdir docker_for_rails27
$ cd docker_for_rails27

2. Dockerfileを作成する

$ vi Dockerfile
# rails2.7.0を指定
FROM ruby:2.7.0
# パッケージインストール
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /docker_for_rails27
WORKDIR /docker_for_rails27
COPY Gemfile /docker_for_rails27/Gemfile
COPY Gemfile.lock /docker_for_rails27/Gemfile.lock
RUN bundle install
COPY . /docker_for_rails27

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# rails起動コマンド
CMD ["rails", "server", "-b", "0.0.0.0"]

3. Gemfileを作る

vi Gemfile
source 'https://rubygems.org'
gem 'rails', '~>5.2.4'

空のGemfile.lockを作成する

$ touch Gemfile.lock

4. entrypoint.shを作る

dockerの初回起動時に実行されるファイル

vi entrypoint.sh
#!/bin/bash
set -e

rm -f /docker_for_rails27/tmp/pids/server.pid
exec "$@"

5. docker-compose.ymlを作る

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

6. 作成したファイルをもとに環境を構築する

$ docker-compose run web rails new . --force --no-deps --database=postgresql --skip-bun
$ docker-compose build
db uses an image, skipping
Building web
[+] Building 77.7s (15/15) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                              0.2s
 => => transferring dockerfile: 559B                                                                                                                              0.1s
 => [internal] load .dockerignore                                                                                                                                 0.1s
 => => transferring context: 2B                                                                                                                                   0.0s
 => [internal] load metadata for docker.io/library/ruby:2.7.0                                                                                                     0.0s
 => [ 1/10] FROM docker.io/library/ruby:2.7.0                                                                                                                     0.3s
 => [internal] load build context                                                                                                                                 5.9s
 => => transferring context: 41.27MB                                                                                                                              5.8s
 => [ 2/10] RUN apt-get update -qq && apt-get install -y nodejs postgresql-client                                                                                16.2s
 => [ 3/10] RUN mkdir /docker_for_rails27                                                                                                                         0.5s
 => [ 4/10] WORKDIR /docker_for_rails27                                                                                                                           0.1s
 => [ 5/10] COPY Gemfile /docker_for_rails27/Gemfile                                                                                                              0.1s
 => [ 6/10] COPY Gemfile.lock /docker_for_rails27/Gemfile.lock                                                                                                    0.1s
 => [ 7/10] RUN bundle install                                                                                                                                   56.1s
 => [ 8/10] COPY . /docker_for_rails27                                                                                                                            0.6s
 => [ 9/10] COPY entrypoint.sh /usr/bin/                                                                                                                          0.1s
 => [10/10] RUN chmod +x /usr/bin/entrypoint.sh                                                                                                                   0.7s
 => exporting to image                                                                                                                                            2.6s
 => => exporting layers                                                                                                                                           2.6s
 => => writing image sha256:ea82de4e78548b8d350390c1146a3b486a8dbc00b196d6e7c98f03570552c517                                                                      0.0s
 => => naming to docker.io/library/docker_for_rails27_web                                                                                                         0.0s
Successfully built ea82de4e78548b8d350390c1146a3b486a8dbc00b196d6e7c98f03570552c517

7. DBの設定を作成する

$ vi config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: postgres
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: docker_for_rails27_development

test:
  <<: *default
  database: docker_for_rails27_test

production:
  <<: *default
  database: docker_for_rails27_production
  username: docker_for_rails27
  password: <%= ENV['DOCKER_FOR_RAILS27_DATABASE_PASSWORD'] %>

8. コンテナを起動する

$ docker-compose up

9. DBを作成する

docker-compose upした後に別タブで

$ docker-compose run web rake db:create

起動したサーバーにアクセスする

http://localhost:3000にアクセスする
image.png

詰まった部分

config/database.ymlでhostを設定していなかったため、デフォルトのunix domain socketに接続しようとしてdb:createが出来なかった
=> host: dbを記載することで解決

その際のエラー

connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

参考

https://qiita.com/chisaki0606/items/a4b42af5c4735c94057a
https://qiita.com/sagahi/items/ef4b04b5498658bf6210
https://teratail.com/questions/178489

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?