12
12

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のテストをDockerでやるとすごく便利だった話

Posted at

DBのダンプを取得して、少し加工して別DBサーバーに持っていく作業が必要でSQLのテストを行う際にDockerでDBサーバーを使ってみたらとても便利だったのでメモ

結論

  • DBサーバーの起動、破棄が高速でとても便利
  • PostgreSQLやMySQLの公式リポジトリがあるのでわざわざDockerfileを作らなくても良い。タグによって複数のバージョンもあるので試験環境も合わせやすい
  • docker commitコマンドによって任意のタイミングの状態のDBサーバーをイメージ化する事もできる(DDL定義後など)
  • DBが関係するようなテストでもDocker使うととても便利そう

参考

やってみる

Dockerは既にインストール済みの想定

# PostgreSQLのイメージを取得。バージョン9.3.10のものを取得
$docker pull postgres:9.3.10

# psql時のパスワード省略するためにファイルを配置
$echo "*:5432:*:*:mysecretpassword" >> ~/.pgpass

# DBサーバー起動
$docker run --name test-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres:9.3.10

# SQLをテスト
$psql -h $(docker inspect --format "{{ .NetworkSettings.IPAddress }}" test-postgres) -U postgres -f abc_dump.sql

# psqlコマンドで登録内容を確認
$psql -h $(docker inspect --format "{{ .NetworkSettings.IPAddress }}" test-postgres) -U postgres

# 停止、破棄
$docker stop test-postgres
$docker rm test-postgres

以降、テストする際も同じ流れ(docker pullと.pgpassの配置以外)で簡単にテストができる。

docker commitコマンドを使えば任意のタイミングの状態のイメージが作成できるので、DDLのみ流した状態のイメージ作るのとかも簡単。

12
12
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
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?