ITの隊長のブログ

ITの隊長のブログです。Rubyを使って仕事しています。最近も色々やっているお(^ω^ = ^ω^)

m1 macでschemaspyをdocker composeで動かす

スポンサードリンク

概要

  • 参考にしてた記事で動かんくなってたので辛い
  • 改めて整理して色々試して動くようになったのでそれだけ貼り付ける

ディレクト

$ tree
.
├── Dockerfile
├── docker-compose.yml
├── initdb.d
│   └── 01-init.sql
├── mysql
│   └── custom
│       └── my.cnf
├── output
└── schemaspy
    └── schemaspy.properties

ファイルの内容

Dockerfile

FROM openjdk:8u212-jdk-alpine

ENV DRIVER_URL https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.30/mysql-connector-java-8.0.30.jar
ENV APP_URL https://github.com/schemaspy/schemaspy/releases/download/v6.1.0/schemaspy-6.1.0.jar

WORKDIR /app
COPY schemaspy/schemaspy.properties .

RUN apk --update add graphviz ttf-dejavu && \
    apk --update add --virtual .builddep tzdata wget libressl && \
    cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && \
    mkdir /drivers && \
    wget -O /drivers/mysql-connector-java.jar ${DRIVER_URL} && \
    wget -O schemaspy.jar ${APP_URL} && \
    apk del .builddep && \
    rm -rf /var/cache/apk/*

docker-compose.yml

version: "3.9"
 
volumes:
  db-store:
 
services:
  db:
    image: mysql:8.0-oracle
    volumes:
      - db-store:/var/lib/mysql
      - ./initdb.d:/docker-entrypoint-initdb.d
      - ./mysql/custom:/etc/mysql/conf.d
    environment:
      - MYSQL_DATABASE=my_database
      - MYSQL_ROOT_PASSWORD=root
 
  schemaspy:
    build:
      context: .
    image: schemaspy
    volumes:
      - ./output:/output
    tty: true

initdb.d/01-init.sql

CREATE DATABASE IF NOT EXISTS my_database;
USE my_database;

CREATE TABLE `pokemon` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL
);

CREATE TABLE `type` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL
);

CREATE TABLE `waza` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL,
    `type_id` INT NOT NULL,
    FOREIGN KEY (`type_id`) REFERENCES `type`(`id`)
);

CREATE TABLE `pokemon_type` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `pokemon_id` INT NOT NULL,
    `type_id` INT NOT NULL,
    FOREIGN KEY (`pokemon_id`) REFERENCES `pokemon`(`id`),
    FOREIGN KEY (`type_id`) REFERENCES `type`(`id`)
);

CREATE TABLE `pokemon_waza` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `pokemon_id` INT NOT NULL,
    `waza_id` INT NOT NULL,
    FOREIGN KEY (`pokemon_id`) REFERENCES `pokemon`(`id`),
    FOREIGN KEY (`waza_id`) REFERENCES `waza`(`id`)
);

schemaspy/schemaspy.properties

schemaspy.t=mysql
schemaspy.dp=/drivers/mysql-connector-java.jar
schemaspy.host=db
schemaspy.port=3306
schemaspy.db=my_database
schemaspy.s=my_database
schemaspy.u=root
schemaspy.p=root
schemaspy.implied=true
schemaspy.norows=true
schemaspy.nopages=true

mysql/custom/my.cnf

[mysqld]
character-set-server=utf8

[mysql]
default-character-set=utf8

実行

$ docker compose up -d
$ docker compose exec schemaspy java -jar schemaspy.jar -o /output/

参考記事

gmor-sys.com

qiita.com

zenn.dev