0
2

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 5 years have passed since last update.

SQL練習環境を整える

Last updated at Posted at 2017-04-30

はじめに

dockerでSQLを練習するための環境を構築する話です。ここで書いているのはRDBMSがPostgreSQLの場合です。psqlだけだと不便なので、rubyでも操作できるようにします。

ディレクトリの構成

 .
 ├── Dockerfile
 ├── Gemfile
 ├── Gemfile.lock
 ├── docker-compose.yml

各ファイルについて

Dockerfile
 FROM ruby:2.2.0
 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev
 RUN mkdir /myapp
 WORKDIR /myapp
 ADD Gemfile /myapp/Gemfile
 ADD Gemfile.lock /myapp/Gemfile.lock
 RUN bundle install
 ADD . /myapp
docker-compose.yml
 version: '2'
 services:
   db:
     image: postgres
   cmd:
     build: .
     volumes:
       - .:/myapp
     depends_on:
       - db
 source 'https://rubygems.org'
 gem 'pg'

環境構築

 $ docker-compose build
 $ docker-compose up -d

停止

 $ docker-compose stop

2回目移行の開始

 $docker-compose start

rubyスクリプトの実行

 $docker-compose run cmd ruby <file_name>

psqlの実行

 $docker-compose run db psql -n -h db -U postgres

nオプションは日本語を使うためです。日本語を使わない場合は不要です。

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?