Docker ComposeでRailsプロジェクトを立ち上げる

## 概要
[公式](https://docs.docker.com/compose/rails/)のやることだけ書いたやつです。

## 手順

```shell
$ cd ~/projects
$ mkdir project && cd $_
$ vim Dockerfile
```

```dockerfile:Dockerfile
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"]
```

```shell
$ vim Gemfile
```

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

```shell
$ touch Gemfile.lock
$ vim entrypoint.sh
```

```shell:entrypoint.sh
#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
```

```
$ vim docker-compose.yml
```

```yml:docker-compose.yml
version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  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
```

```shell
$ docker-compose run web rails new . --force --no-deps --database=postgresql
$ ls -l
total 80
-rw-r--r--   1 hondy12345  staff   453  4 14 11:08 Dockerfile
-rw-r--r--   1 hondy12345  staff  2214  4 14 11:27 Gemfile
-rw-r--r--   1 hondy12345  staff  5355  4 14 11:28 Gemfile.lock
-rw-r--r--   1 hondy12345  staff   374  4 14 11:27 README.md
-rw-r--r--   1 hondy12345  staff   227  4 14 11:27 Rakefile
drwxr-xr-x  10 hondy12345  staff   320  4 14 11:27 app
drwxr-xr-x   9 hondy12345  staff   288  4 14 11:28 bin
drwxr-xr-x  16 hondy12345  staff   512  4 14 11:27 config
-rw-r--r--   1 hondy12345  staff   130  4 14 11:27 config.ru
drwxr-xr-x   3 hondy12345  staff    96  4 14 11:27 db
-rw-r--r--   1 hondy12345  staff   305  4 14 11:27 docker-compose.yml
-rw-r--r--   1 hondy12345  staff   202  4 14 11:27 entrypoint.sh
drwxr-xr-x   4 hondy12345  staff   128  4 14 11:27 lib
drwxr-xr-x   3 hondy12345  staff    96  4 14 11:27 log
-rw-r--r--   1 hondy12345  staff    63  4 14 11:27 package.json
drwxr-xr-x   9 hondy12345  staff   288  4 14 11:27 public
drwxr-xr-x   3 hondy12345  staff    96  4 14 11:27 storage
drwxr-xr-x  11 hondy12345  staff   352  4 14 11:27 test
drwxr-xr-x   6 hondy12345  staff   192  4 14 11:27 tmp
drwxr-xr-x   3 hondy12345  staff    96  4 14 11:27 vendor
$ sudo chown -R $USER .
$ docker-compose build
```

```shell
$ vim config/database.yml
```

```yml:config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password:
  pool: 5

development:
  <<: *default
  database: project_development


test:
  <<: *default
  database: project_test
```

```shell
$ docker-compose up
$ docker-compose run web rake db:create
```

## 完了
http://localhost:3000/
![FireShot Capture 008 - Ruby on Rails - localhost.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/42587/a820325c-c078-c622-bf91-205503eaa344.png)