LoginSignup
7
4

More than 1 year has passed since last update.

【2022年】MacOS で Podman のバインドマウントを使おう

Last updated at Posted at 2022-03-27

はじめに

Podman は、Red Hat 社が開発したオープンソースのコンテナ管理ツールで、Docker の代替として、以下のような部分に魅力があります。

  • オープンソース(つまり無料)
  • podman build|run|ps のように、Docker CLI と同じコマンドがサポートされている
  • containerd のようなデーモンプロセスに依存しない(Docker より本質的に安全)

そしてついに、Podman v4.0.0 のリリースで、MacOS ユーザーはホストシステム上のファイルやディレクトリをコンテナー内に バインドマウント できるようになりました。「バインドマウントができないから」を理由に Docker に頼ってきた方、これを機にツール変更を検討してはいかがでしょうか。

MacOS で podman をインストールする

Mac クライアントは Homebrew からダウンロードできます。

brew update
brew install podman
podman -v
# podman version 4.0.2

-v / --volume オプションで仮想マシンを初期化する

バインドマウントを利用するために、コンテナが実行される Linux 仮想マシンを初期化する時に「マウント可能なパス」を指定する必要があります。Podman のデフォルト Fedora CoreOS では、ルートファイルシステム(/)がリードオンリーでマウントされるため /mnt ディレクトリに対してマウントする必要があります(以下の例のように)

$ podman machine init --volume /Users:/mnt/Users
Downloading VM image: fedora-coreos-35.20220305.dev.0-qemu.aarch64.qcow2.xz: done  
Extracting compressed file
Image resized.
Machine init complete
To start your machine run:

	podman machine start

$ podman machine start
Starting machine "podman-machine-default"
INFO[0000] waiting for clients...                       
INFO[0000] new connection from  to /var/folders/1p/ngsbv_gj3px52qmhqchv10j00000gn/T/podman/qemu_podman-machine-default.sock 
Waiting for VM ...
INFO[0029] Socket forward established: /Users/hankehly/.local/share/containers/podman/machine/podman-machine-default/podman.sock to /run/user/501/podman/podman.sock 
Mounting volume... /Users:/mnt/Users

This machine is currently configured in rootless mode. If your containers
require root permissions (e.g. ports < 1024), or if you run into compatibility
issues with non-podman clients, you can switch using the following command: 

	podman machine set --rootful

API forwarding listening on: /Users/hankehly/.local/share/containers/podman/machine/podman-machine-default/podman.sock

The system helper service is not installed; the default Docker API socket
address cant be used by podman. If you would like to install it run the
following command:

	sudo /opt/homebrew/Cellar/podman/4.0.2/bin/podman-mac-helper install

You can still connect Docker API clients by setting DOCKER_HOST using the
following command in your terminal session:

	export DOCKER_HOST='unix:///Users/hankehly/.local/share/containers/podman/machine/podman-machine-default/podman.sock'

Machine "podman-machine-default" started successfully

ちなみに、Docker Desktop の Resources > File Sharing 画面から、マウント可能なパスが書かれているので、Docker から Podman に移行しやすいように同じパスを指定するといいかもしれません。

podman machine init \
    --volume /Users:/mnt/Users \
    --volume /Volumes:/mnt/Volumes \
    --volume /private:/mnt/private \
    --volume /tmp:/mnt/tmp

docker-preferences.png

バインドマウントを用いたコンテナーの起動

実際にバインドマウントを使ってみましょう。まず、マウントするテストディレクトリを作り、適当な文字列を記載したファイルを中に入れます。

mkdir ~/Desktop/test
echo "hello world" > ~/Desktop/test/test.txt

次に、--volume オプションでバインドマウントを指定したコンテナを起動します。コマンドの書き方は、Docker とほとんど一緒ですが、違いが2つあります。

  • dockerpodman に入れ替える
  • --volume のセミコロンの左側のパスは、MacOS のホストシステムのパス(/Users)ではなく、Linux 仮想マシンの中のパスを書く(/mnt/Users
$ podman run -it --rm --volume /mnt/Users/hankehly/Desktop/test:/test docker.io/bash:4.4
Trying to pull docker.io/library/bash:4.4...
Getting image source signatures
Copying blob sha256:46ea3ec908d84768af0912f2e0ab1bbc0908c14508218e9f59684ea499b32fc9
Copying blob sha256:a5e44472bb1f0d721d23f82fa10a4c3d25994790238a173c1de950a649eb9a90
Copying blob sha256:41fa6fdeabaaf497983e684b493ca96c9dbc0cca434e7f0700979a0946b31e84
Copying blob sha256:a5e44472bb1f0d721d23f82fa10a4c3d25994790238a173c1de950a649eb9a90
Copying blob sha256:46ea3ec908d84768af0912f2e0ab1bbc0908c14508218e9f59684ea499b32fc9
Copying blob sha256:41fa6fdeabaaf497983e684b493ca96c9dbc0cca434e7f0700979a0946b31e84
Copying config sha256:de41e00e741bef0e6c139e28383b358b20634a5644b875084c5991b5dcc9534a
Writing manifest to image destination
Storing signatures
bash-4.4#

バインドマウントしたディレクトリにちゃんとアクセスできることを確かめると…

bash-4.4# ls -la /test
total 4
drwxr-xr-x    3 root     nobody          96 Mar 26 03:56 .
dr-xr-xr-x    1 root     root            40 Mar 26 03:56 ..
-rw-r--r--    1 root     nobody           5 Mar 26 03:56 test.txt
bash-4.4# cat /test/test.txt 
hello world

できました!

終わりに

MacOS でのバインドマウントサポートの追加で、Docker より Podman を選ぶ理由がもう一つ増えました。Podman への乗り換えで、Docker Desktop の利用料金のコスト削減を検討してみてはいかがでしょうか。詳しくは podman.io でご確認ください。

7
4
1

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