LoginSignup
1

More than 3 years have passed since last update.

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

Posted at

概要

公式のやることだけ書いたやつです。

手順

$ cd ~/projects
$ mkdir project && cd $_
$ vim 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"]
$ vim Gemfile
Gemfile
source 'https://rubygems.org'
gem 'rails', '~>5'
$ touch Gemfile.lock
$ vim entrypoint.sh
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
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
$ 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
$ vim config/database.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
$ docker-compose up
$ docker-compose run web rake db:create

完了

http://localhost:3000/
FireShot Capture 008 - Ruby on Rails - localhost.png

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
1