LoginSignup
3
0

【Day 18】API 環境構築 - Docker Postgresql

Last updated at Posted at 2023-12-17

はじめに

スライド19.PNG


2023年アドベントカレンダー18日目です。

本日は前回に引き続き「開発者はAPIの開発を進めることができる」を進めています。

image.png

今回はPostgresqlをDockerで立てて、初期データを入れるところまでやっていこうと思います

Postgresql

Postgres on Docker Compose

今回はdocker composeで構築していきます

compose.yaml
version: '3'

services:
  db:
    image: postgres:16
    ports:
      - 5432:5432
    environment:
      POSTGRES_DB: blog
      POSTGRES_USER: blog_user
      POSTGRES_PASSWORD: blog_password
    tty: true
    stdin_open: true

これで起動はできるはずです

$ docker compose up

ただし、テスト用の初期値(seed)を入れておきたいので、次のSQLを作成しました。

init/init.sql
CREATE TABLE blog (
    id UUID PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    author VARCHAR(255) NOT NULL,
    body TEXT NOT NULL,
    created_at VARCHAR(255) NOT NULL
);

INSERT INTO blog (id, title, author, body, created_at)
VALUES 
('123e4567-e89b-12d3-a456-426614174000', 'カフェの隅で見つけた幸せ:私のお気に入りの隠れ家', '田中太郎', 'ブログの本文', '2023-12-09 09:00:00');

作った初期化SQLを/docker-entrypoint-initdb.dに設置します

compose.yaml
version: '3'

services:
  db:
    image: postgres:16
    ports:
      - 5432:5432
    environment:
      POSTGRES_DB: blog
      POSTGRES_USER: blog_user
      POSTGRES_PASSWORD: blog_password
+   volumes:
+     - ./init:/docker-entrypoint-initdb.d
    tty: true
    stdin_open: true

この状態で起動することで、Seedが入った状態でPostgresqlを起動することができます

$ docker compose up

これでRustの開発環境ができました!

image.png

明日からはRustでAPIの実装を進めていきます👍

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