0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Docker composeでMySQLのコンテナを複数同時に起動する場合のポート設定

Last updated at Posted at 2023-06-25

はじめに

Docker ComposeでMySQLのコンテナを複数同時に起動して作業したいことがあったのですが、その際にうまくいかなかったので備忘録として残しておきます。

MySQLのコンテナ

docker-compose.yml
version: "3.9"
volumes:
  db-store:
services:
  db:
    build:
      context: .
      dockerfile: ./infra/docker/mysql/Dockerfile
    ports:
      - 3306:3306
    volumes:
      - type: volume
        source: db-store
        target: /var/lib/mysql
        volume:
          nocopy: true
    environment:
      - MYSQL_DATABASE=${DB_DATABASE}
      - MYSQL_USER=${DB_USERNAME}
      - MYSQL_PASSWORD=${DB_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}

事象

2つ目のコンテナを起動する際、MySQLのポート番号がそれぞれ通常の3306のままだった場合、以下のようなエラーが表示されます。

$ docker compose up -d
 ⠿ Container container-db-1   Starting
Error response from daemon: driver failed programming external connectivity on endpoint container-db-1 (...): Bind for 0.0.0.0:3306 failed: port is already allocated
make: *** [up] Error 1

ポート番号が被っているのでそれは確かに接続できないですよね。

対処:ホスト側のポート番号を変更

そこでホスト側のポート番号を3307へ変更します。

docker-compose.yml
    ports:
      - 3307:3306

コンテナが起動できました。

$ docker compose up -d
 ⠿ Container container-db-1   Started

Sequel Aceでも接続してみる

データベース管理アプリのSequel Aceで接続します。
Sequel Aceの設定値は以下の通りです。

項目名 設定値
Host 127.0.0.1
Username MYSQL_USERの値を入力
Password MYSQL_PASSWORDの値を入力
Database MYSQL_DATABASEの値を入力
Port 3307

Sequel Aceでも接続できました。
これで複数同時に起動して利用することができました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?