LoginSignup
4
1

More than 5 years have passed since last update.

Docker初心者が練習のため公式のクイックスタートをやった記録

Last updated at Posted at 2018-09-04

はじめに

千里の道も一歩から
Docker初心者が練習のため公式のクイックスタートをやった記録
https://docs.docker.com/compose/rails/

環境

OS: OSX
Docker: Docker for Mac
Editor: emacs
Shell: zsh

手順(記録)

# テキトウな空のディレクトリを作成する
$ mkdir docker_practice
$ cd docker_practice

# Dockerファイルを作成する
$ emacs Dockerfile

Dockerfileの中身は以下の通り

FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

Gemfile を作成する

$ emacs Gemfile

Gemfile の中身は以下の通り

source 'https://rubygems.org'
gem 'rails', '5.2.0'

空でも Gemfile.lock が必要なのでとりあえず作っておく

$ touch Gemfile.lock

docker-compose.yml を作成する。

$ emacs docker-compose.yml

docker-compose.yml の中身は以下の通り
環境変数から取る方が・・・などと深く考えずべた書きする

docker-compose.yml
version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

Railsを動かすコンテナで rails new してみる

$ docker-compose run web rails new . --force --database=postgresql

ここまで来たら web の中身を覗くことができる

$ docker-compose run web bash

/myapp# ls -l
total 36
-rw-r--r--  1 root root  223 Sep  4 18:38 Dockerfile
-rw-r--r--  1 root root 2223 Sep  4 18:41 Gemfile
-rw-r--r--  1 root root 5300 Sep  4 18:42 Gemfile.lock
-rw-r--r--  1 root root  374 Sep  4 18:41 README.md
-rw-r--r--  1 root root  227 Sep  4 18:41 Rakefile
drwxr-xr-x 10 root root  320 Sep  4 18:41 app
drwxr-xr-x  9 root root  288 Sep  4 18:42 bin
drwxr-xr-x 16 root root  512 Sep  4 19:09 config
-rw-r--r--  1 root root  130 Sep  4 18:41 config.ru
drwxr-xr-x  3 root root   96 Sep  4 18:41 db
-rw-r--r--  1 root root  266 Sep  4 18:39 docker-compose.yml
drwxr-xr-x  4 root root  128 Sep  4 18:41 lib
drwxr-xr-x  4 root root  128 Sep  4 18:51 log
-rw-r--r--  1 root root   63 Sep  4 18:41 package.json
drwxr-xr-x  9 root root  288 Sep  4 18:41 public
drwxr-xr-x  3 root root   96 Sep  4 18:41 storage
drwxr-xr-x 11 root root  352 Sep  4 18:41 test
drwxr-xr-x  9 root root  288 Sep  4 18:59 tmp
drwxr-xr-x  3 root root   96 Sep  4 18:41 vendor

/myapp# exit

docker-compose.yml で定義したサービスをビルドする

$ docker-compose build

rails を立ち上げるために db の設定も必要

$ emacs config/database.yml 

postgresql 使ってるので以下の通り

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

development:
  <<: *default
  database: myapp_development


test:
  <<: *default
  database: myapp_test

マイグレーションを流す

$ docker-compose run web rake db:create

立ち上げてみる

$ docker-compose up

http://localhost:3000/
に接続するとRailsの動作を確認できるはず。

image.png

リンク

4
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
4
1