0
0

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コンテナを複数起動したい on macOS

Last updated at Posted at 2025-11-12

このドキュメントについて

docker composeを利用していて「環境Aと環境Bを同時に起動したいのにポート番号が重なっていて起動できない」なんてことありますよね?(私はあります)
そこでポート番号が重なっていても問題なく環境を複数立ち上げられる方法を記載しておきます。

結論

ローカルホスト ループバックアドレスを複数有効化

スクリプトを作成

terminal
sudo vi /usr/local/bin/setup-loopback-aliases.sh
/usr/local/bin/setup-loopback-aliases.sh
#!/bin/bash
for i in {2..10}; do
    ifconfig lo0 alias 127.0.0.$i up
done
terminal
sudo chmod +x /usr/local/bin/setup-loopback-aliases.sh

sudo vi /Library/LaunchDaemons/com.user.loopback.plist

作成したスクリプトをLaunchDaemonを使って起動時に実行

/Library/LaunchDaemons/com.user.loopback.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.user.loopback</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/setup-loopback-aliases.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
terminal
sudo chown root:wheel /Library/LaunchDaemons/com.user.loopback.plist
sudo chmod 644 /Library/LaunchDaemons/com.user.loopback.plist
sudo launchctl load /Library/LaunchDaemons/com.user.loopback.plist

起動に利用するdocker-compose.ymlファイル

apache/compose.yml
services:
  apache:
    image: httpd
    ports:
      - "127.0.0.1:80:80"
nginx/compose.yml
services:
  nginx:
    image: nginx
    ports:
      - "127.0.0.2:80:80"

これで、 http://127.0.0.1:80/http://127.0.0.2:80/ でそれぞれの環境にアクセスが可能になります(^^)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?