LoginSignup
5
4

More than 1 year has passed since last update.

Docker Desktop ナシではじめるWindowsコンテナアプリ開発①

Last updated at Posted at 2023-03-11

コンテナ開発を行う際によく用いられる Docker Desktop。
この Docker Desktop が2022年2月から有料化( https://www.docker.com/pricing/ )されて随分と経つのだけど、改めて Docker Desktop に頼らずコンテナ開発環境を構築し、その環境を利用して Windows コンテナアプリを作っていきたいと思う。

環境構築編

今回は本シリーズ1回目ということで以下のような手順で環境構築を行う。
・Docker Desktop に代わるコンテナ開発環境について
・コンテナアプリ開発環境構築手順
・コンテナ管理ツールをどうするか?

対象環境

  • ホストOS : macOS Monterey (MacBook Pro) v12.6

1. Docker Desktop に代わるコンテナ開発環境について

冒頭申し上げたように Docker Desktop は2022年2月から有償ツールとなった。
でも、「DockerってOSSで無償利用可能でしょ?」とお考えの方。実際、ライセンスは Apache2.0 ( https://github.com/docker/engine/blob/master/LICENSE )。無償で利用することは可能であり、DockerのGitプロジェクトに含まれる多くのツールも同様にApache2.0であるため、無料で利用することは可能。
( 一部のツールは別のライセンス形態 )
brewによるインストールでも、ライセンスApach2.0と示されている。
09.png
https://formulae.brew.sh/formula/docker

ところで、Apache2.0は商用利用も可としている。なので商用化されたDocker DesktopにDocker Engine等のApache2.0ライセンスソフトウェアを組み込んで有償とすることができる。
つまり、Docker Desktopの秀逸なインストーラにより組み込まれるDocker Engineとその他ツール、便利なコンテナ管理機能、Linux VM、セキュリティー等は有償ということになる。そのメリットは、環境構築を一瞬にして行えて、その上安全安心のお墨付きをいただけることと考える。言い換えると、Docker Desktopによりインストールされたツール群(Docker Engine, CLI等)を使うとライセンス違反になってしまうので注意が必要。ここでは Docker Desktop に頼らないでにコンテナ開発環境を構築しようと思う。
ここでは Lima(Linux virtual machines、https://github.com/lima-vm/lima/blob/master/README.ja.md )を使う。超簡単に言うと、LimaのH.P.から「自動的にファイル共有とポートフォワード機能を持つLinux仮想マシンと containerd を起動するものである。」
次から実際にLimaをインストールし、コンテナ環境を構築していく。

2.コンテナアプリ開発環境構築手順

インストール手順

Docker CLI インストール

先程申し上げたとおり、BrewによりインストールされるDocker(Client)はApache2.0なので、これを使っても問題なし。

brew install docker

Lima インストール

次いで、Lima。こちらはGithub ( https://github.com/lima-vm/lima/blob/master/README.ja.md ) に記載しているよう以下のコマンドを実行。ちなみにLimaのライセンスもApache2.0
10.png

$ brew install lima  

確認

$ limactl -v
limactl version 0.15.0

Lima をインストールするとデフォルトで以下のプロファイルが準備されている。

  • colima : macOSで小さく始めるDocker&Kubernetes
  • default : nerdctl ( Lima デフォルトのコンテナ環境 )
  • docker : Lima 上で動作する Linux 上の Docker
  • minikube : Minikube

Docker 開発環境を立ち上げたいので「docker」プロファイルを用いることにする。

コンテナ環境起動

$ limactl start docker
INFO[0000] Using the existing instance "docker"
    ・・・
    ・・・
    ・・・
INFO[0045] Message from the instance "docker":          
To run `docker` on the host (assumes docker-cli is installed), run the following commands:
------
docker context create lima --docker "host=unix:///Users/aa416024/.lima/docker/sock/docker.sock"
docker context use lima
docker run hello-world
------

コンテナ起動の際、最後に上記のようなコマンドを実行すべしというメッセージが出力される。
#1. docker context create lima --docker "host=unix:///Users/aa416024/.lima/docker/sock/docker.sock"
Docker用に新たなcontextを生成する。これにより別のクラスターや単一ノードへの接続を行い、かつCLI設定をすばやく切り替えられる。

$ docker context create lima --docker "host=unix:///Users/<ユーザ名>/.lima/docker/sock/docker.sock"

context "lima" already exists

既に”lima”コンテキストは存在していた。

#2. docker context use lima
dockerCLI が「lima」コンテキストを利用できるように切り替えられる。

$ docker context use lima
lima
Current context is now "lima"

現在のコンテキストは"lima"とのこと。

#3. docker run hello-world
いわずもがなhello-worldコンテナ実行確認

$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

すごく簡単に Docker コンテナ開発環境を立ち上げることができた。
では、コンテナ管理方法について今までは Docker Desktop のよくできた GUI で管理していたのだけど、この Docker on Lima ではどうやって同じ様にコンテナを管理していけばよいのか?それを以下で記載する

今回は有償となったDocker Desktopではなく、無償のDocker+Limaでコンテナ実行環境を構築してみた。
次回は、Docker Desktopのコンテナ管理面に関して、MITライセンス(OSSであり無償利用可)のVisual Studio Code( https://github.com/Microsoft/vscode )を使って同等のコンテナ管理を紹介する。

参照:
Limaを使ってDocker環境を構築する: https://note.com/shift_tech/n/n265a2f0904e9
Docker on Limaで脱Docker Desktop for Mac: https://zenn.dev/matsukaz/articles/31bc31ff1c54b4

5
4
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
5
4