Docker を始めて使う際のプロセスをメモしておきます。
Macbook Pro に Docker Desktop をインストールし、Docker Hub から MySQL のコンテナイメージをダウンロードし起動する手順となります。
- Docker Desktop のインストール
- Docker Hub から Docker イメージをダウンロード
- コンテナの作成 & 起動
- コンテナに入ってみる
- コンテナから出る
- その他のコマンド
- コンテナに入らずにコンテナ内の MySQL にアクセスする
- データベースを作成する
- DBeaver で接続する
1. Docker Desktop のインストール
下記のリンクから Mac 用の Docker Desktop をダウンロードします。
https://docs.docker.com/desktop/install/mac-install/
「Docker.dmg」というファイルがダウンロードされるのでダブルクリックで実行し、画面表示に従ってインストールを進めます。
「docker --version」を実行してインストールが完了したか確認します。
% docker --version Docker version 23.0.5, build bc4487a
無事インストールが出来た様です。
2. Docker Hub から Docker イメージをダウンロード
次に、「docker pull mysql」を実行して MySQL のコンテナイメージをダウンロードします。
ちなみに「実行場所にディレクトリが作られる」等はないので、ローカル環境のどこで実行しても大丈夫です。
% docker pull mysql Using default tag: latest latest: Pulling from library/mysql 328ba678bf27: Pull complete f3f5ff008d73: Pull complete dd7054d6d0c7: Pull complete 70b5d4e8750e: Pull complete cdc4a7b43bdd: Pull complete a0608f8959e0: Pull complete 5823e721608f: Pull complete a564ada930a9: Pull complete 539565d00e89: Pull complete a11a06843fd5: Pull complete 92f6d4aa041d: Pull complete Digest: sha256:a43f6e7e7f3a5e5b90f857fbed4e3103ece771b19f0f75880f767cf66bbb6577 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest
ダウンロードが完了しました。「docker images」コマンドを実行するとダウンロードされたコンテナイメージを確認することができます。
% docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 8189e588b0e8 4 weeks ago 564MB
注意点
ちなみに、この時ローカルの Docker アプリケーションが起動していないと下記のエラーが出てしまいます。
% docker pull mysql Using default tag: latest Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
この場合は Docker アプリケーションを起動して再度実行してください。
3. コンテナの作成 & 起動
Docker イメージのダウンロードの完了後、「docker run」コマンドでイメージからコンテナを作成、起動します。
Docker Hub のページに記載がある下記のフォーマットを元に適切な「docker run」コマンドを作成、実行します。
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
主なオプション
上記のフォーマットに登場するオプションは下記の内容になります。
表記 | 設定内容 | 備考 |
---|---|---|
--name | コンテナの識別子 | |
-e | コンテナ内の環境変数 | 上記では MySQL のログインパスワードを設定 |
-d | デタッチドモード | バックグラウンドでコンテナを起動 |
mysql:tag | 使用する Docker イメージとそのバージョンの指定 | 「イメージ:タグ」の形式で記述。タグのデフォルトは直近のもの。 |
加えて今回は -p オプションを設定し、ローカル PC からコンテナへのポート転送も設定しておきます。
表記 | 設定内容 | 備考 |
---|---|---|
-p | ポートフォワーディング | 「ホストのポート:コンテナのポート」の形式で設定 |
今回使うコマンドは下記とします。
$ docker run --name sample-mysql -p 13306:3306 -e MYSQL_ROOT_PASSWORD=mysqlpassword -d mysql
「-p 13306:3306」の部分で、ホストの 13306 番ポートとコンテナの 3306 番ポートを紐づける設定にしています。
% docker run --name sample-mysql -p 13306:3306 -e MYSQL_ROOT_PASSWORD=mysqlpassword -d mysql 2b3b0efc8a7c7085e8e405dae4026a929a8d3cc76860f99dc9dde0b3f24474f1
「docker ps」コマンドを実行してコンテナが起動されているかを確認します。
% docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2b3b0efc8a7c mysql "docker-entrypoint.s…" 15 seconds ago Up 14 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp sample-mysql
無事にコンテナが作成、起動されています。
ちなみに、先述した「docker pull」を実行しなくても、「docker run」実行時にローカル環境に対象のイメージがない場合、自動的に Docker Hub 上で探してくれます。
4. コンテナに入ってみる
「docker exec」コマンドを実行し、作成したコンテナの中に入ってみます。
% docker exec -it sample-mysql bash bash-4.4#
表記 | 設定内容 |
---|---|
-it | i は STDIN(標準入力)の開放、t は擬似 TTY(標準入出力先デバイス)の割り当て。 |
sample-mysql | 対象のコンテナの名前 |
bash | コンテナ内で使用するシェル |
bash-4.4# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.33 MySQL Community Server - GPL Copyright (c) 2000, 2023, Oracle and/or its affiliates. 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>
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
とりあえずコンテナ内の MySQL を参照することはできました。
mysql> exit Bye bash-4.4#
5. コンテナから出る
「exit」と入力すればコンテナから出られます。
bash-4.4# exit exit %
6. その他のコマンド
取り急ぎいくつかのコマンドを記載しておきます。
コマンド表記 | 用途 |
---|---|
docker stop コンテナ名 | コンテナの停止 |
docker start コンテナ名 | コンテナの起動 |
docker ps | 起動中のコンテナの確認 |
docker ps -a | 作成されたコンテナの確認(起動中、停止中両方) |
7. コンテナに入らずにコンテナ内の MySQL にアクセスする
「docker exec」を使わず、ターミナルからコンテナ内の MySQL にアクセスしてみます。
「mysql -h 127.0.0.1 -P 13306 -u root -p」のコマンドでポート 13306 番ポートの MySQL を指定してログインします。
% mysql -h 127.0.0.1 -P 13306 -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.33 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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 databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql>
こんな感じでアクセスできました。
8. データベースを作成する
「db_in_container」というデータベースを作ってみます。
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) mysql> create database db_in_container; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | db_in_container | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.01 sec)
無事作れました。
9. DBeaver で接続してみる


