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?

WSL2 + Testcontainers + Eclipse で快適な開発環境を構築する

Last updated at Posted at 2025-02-21

はじめに

Docker Desktop のライセンス問題があるため、私は WSL2 に Docker をインストールして運用していました。
VSCode であれば WSL2 にリモート接続して開発作業ができるので問題なかったのですが、Eclipse の場合はそういった機能がないため困ってしまいました。

環境

Testcontainers について

Testcontainers はユニットテストを実行する際に、DB等の外部サービスをDockerコンテナで自動作成してくれるライブラリです。

例えば、GitHub の CI 環境のインスタンスには Docker がインストールされているので、Testcontainers を使って実装すれば特別な準備をすることなく CI 環境でユニットテストを実行できてとても便利です。

やったこと

Windows11 から Docker にアクセスできるように設定を修正する

WSL2:/etc/docker/daemon.json
{
  "hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
}
WSL2:/lib/systemd/system/docker.service
-ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
+ExecStart=/usr/bin/dockerd --containerd=/run/containerd/containerd.sock

WSL2 を再起動させるためシャットダウン

Windows11
> wsl --shutdown

Windows11 の ターミナル を起動して Ubuntu 22.04 タブを起動

疎通確認

Windows11
> curl http://localhost:2375/containers/json
[]

WSL2 の IP アドレスを取得(localhost だと Testcontainers からの接続が失敗するので)

WSL2
$ ip a | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1280 qdisc mq state UP group default qlen 1000
    inet 172.31.241.180/20 brd 172.31.255.255 scope global eth0
Windows11:~/.testcontainers.properties
docker.host=tcp\://172.31.241.180\:2375

まとめ

Docker Desktop のような統合ツールを導入することなく、Windows11 から Docker を活用することができました。課題は WSL2 側の IP アドレスが変更されてしまった場合の考慮ですが、今回はここまでとさせていただきます。

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?