LoginSignup
1
0

MacでDocker/docker-compose

Last updated at Posted at 2022-06-14

MacでLinuxを起動し、Docker環境を構築したので、備忘録として残しておきます。

Linux起動

WindowsではWSL2を使ってLinuxを起動しますが、MacでLinux起動となるとVirtualBoxなどが脳裏をよぎりますが、今回は「Multipass」というのを使ってUbuntuを起動しました。

トップページで環境を選ぶようになっているので、ここで「macOS」を選択し、「Download Multipass for MacOS」をクリックするとダウンロードされます。
multipass-バージョン番号+mac-Darwin.pkg」というファイルがダウンロードされているはずなので、これを起動します。
インストーラーが起動するので、インストールを完了させます。

インストールが完了すると、ターミナルで「multipass」コマンドが使えるようになっていると思います。

$ multipass --version
multipass   1.9.1+mac
multipassd  1.9.1+mac

次にLinuxコンテナを作成します。

$ multipass launch --name ubuntu --cpus 2 --memory 4G --disk 100G

--nameでインスタンス名、--cpusでCPUのコア数、--memoryでメモリ、--diskでストレージ容量を指定します。
OSはデフォルトでubuntuになります。

作成したubuntuコンテナにシェルで接続します。

$ multipass shell ubuntu

接続すると以下のような表示になります。

Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.4.0-117-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

 System information disabled due to load higher than 1.0

 * Super-optimized for small spaces - read how we shrank the memory
   footprint of MicroK8s to make it the smallest full K8s around.

   https://ubuntu.com/blog/microk8s-memory-optimisation

1 update can be applied immediately.
To see these additional updates run: apt list --upgradable


Last login: Tue Jun 14 01:53:54 2022 from 192.168.64.1

ラストログインは、macOSに生成されたブリッジネットワークからのものになっていると思います。
Ubuntuが起動してしまえばあとは、UbuntuのDocker起動手順がそのまま使えます。

Dockerインストール

参考サイト(オフィシャル):https://docs.docker.com/engine/install/ubuntu/

まずは、古いバージョンが入っているかもしれないので、削除します。

$ sudo apt-get remove docker docker-engine docker.io containerd runc

次に、必要なものをインストールします。
私の環境だと、下記はすでにインストールはされていました。

$ sudo apt-get update
$ sudo apt-get install ca-certificates curl gnupg lsb-release

次に、Dockerリポジトリを追加するための鍵を生成します。

$ sudo mkdir -p /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Dockerリポジトリを追加します。

$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

これでDocker engineをインストールする環境が整いました。
インストール可能なバージョン一覧を表示します。
インストールする時にバージョンを指定出来ます。

$ apt-cache madison docker-ce
docker-ce | 5:20.10.17~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.16~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.15~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.14~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.13~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 ・
 ・
 ・

インストールします。

$ sudo apt-get update
$ sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io docker-compose-plugin

「VERSION_STRING」には、例えば上記の例で言うと「5:20.10.17~3-0~ubuntu-focal」などを指定します。
バージョンを省略すると、おそらくstableバージョンがインストールされます(多分)。

$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

次に、一般ユーザーでdockerコマンドが使えるようにします。
まず、dockerグループを作成します。

$ sudo groupadd docker

作成したグループに、現在の作業ユーザーを追加します。

$ sudo usermod -aG docker $USER

グループ設定を反映します。

$ newgrp docker 

これで、一般ユーザー(現在作業している/usermodで設定したユーザー)で、dockerコマンドが使えるようになりました。

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:80f31da1ac7b312ba29d65080fddf797dd76acfb870e677f390d5acba9741b17
Status: Downloaded newer image for hello-world:latest

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 ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED              STATUS                          PORTS                                                           NAMES
05071d641959   hello-world         "/hello"                 About a minute ago   Exited (0) About a minute ago                                                                   intelligent_kare

docker-composeインストール

下記コマンドでインストールする。

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
・
・(インストール経過)
・
$ sudo chmod +x /usr/local/bin/docker-compose

これで、docker-composeが使えるようになります。

Dockerコンテナ内にアクセス

これで、Dockerコンテナが使えるようになったわけですが、コンテナ内のサービスにWindowsとかであれば、localhostのポートフォワードしたところへ繋ぐとアクセス出来ますが、Macではlocalhostでは接続出来ません。
てっとり早い手段としては、Multipassで起動したUbuntuに割り当てられた仮想アドレス宛てにアクセスすると繋がります。
私は今のところはこれで使っています。

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