3
1

More than 3 years have passed since last update.

dockerでMySQLのサンプルデータベースのSakilaを使用した環境をすぐ使用できるように設定してみた

Last updated at Posted at 2020-08-02

私はdocker初心者です。間違いやもっと良い方法があれば教えていただけるとありがたいです。


SakilaとはMySQLが公式で用意しているサンプルデータベースです。

こちらからダウンロードできます

DVDレンタル情報が保存されているデータベースで、個人的にはMySQLのデータを使って試したいときに使うというのが便利だと思います。

それをdockerでちょちょっと環境が作成できるようにやってみました。


参考

Dockerfile作成

mysqlの公式イメージを使ってDockerfileを設定してみました

Dockerfile
FROM mysql:8

ENV MYSQL_ROOT_PASSWORD=root

RUN apt-get update \
    && apt-get install -y wget unzip \
    && wget http://downloads.mysql.com/docs/sakila-db.zip \
    && unzip sakila-db.zip \
    # docker-entrypoint-initdb.d/ の .sql ファイルが自動でアルファベット順に実行されるので、リネームしつつ移動
    && mv sakila-db/sakila-schema.sql /docker-entrypoint-initdb.d/01_sakila-schema.sql \
    && mv sakila-db/sakila-data.sql /docker-entrypoint-initdb.d/02_sakila-data.sql

Dockerfileをビルドしてコンテナ作成

$ docker build -t sample .
(省略)
$ docker run --name sakila -d sample
(省略)
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                 NAMES
a599ee328437        sample              "docker-entrypoint.s…"   About a minute ago   Up About a minute   3306/tcp, 33060/tcp   sakila

mysqlに接続しshow tables;を実行

$ docker container exec -it sakila mysql -uroot -proot -Dsakila
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show tables;
+----------------------------+
| Tables_in_sakila           |
+----------------------------+
| actor                      |
| actor_info                 |
| address                    |
| category                   |
| city                       |
| country                    |
| customer                   |
| customer_list              |
| film                       |
| film_actor                 |
| film_category              |
| film_list                  |
| film_text                  |
| inventory                  |
| language                   |
| nicer_but_slower_film_list |
| payment                    |
| rental                     |
| sales_by_film_category     |
| sales_by_store             |
| staff                      |
| staff_list                 |
| store                      |
+----------------------------+
23 rows in set (0.00 sec)

mysql> 

Sakilaのサンプルのdbが作成されていることを確認できました

phpmyadminでも見れるようにしてみた

先ほどのDockerfileと同じディレクトリにdocker-compose.ymlを作成

docker-compose.yml
version: "3"

services:
    db:
        build: .
        container_name: mysql

    phpmyadmin:
        container_name: phpmyadmin
        image: phpmyadmin/phpmyadmin
        environment:
            - PMA_ARBITRARY=1
            - PMA_HOST=mysql
            - PMA_USER=root
            - PMA_PASSWORD=root
        ports:
            - 8080:80
$ docker-compose up -d

Screen Shot 2020-08-03 at 0.59.40.png

以上です。

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