ラベル Docker の投稿を表示しています。 すべての投稿を表示
ラベル Docker の投稿を表示しています。 すべての投稿を表示

2019年6月16日日曜日

WSL1 上で Docker をカジュアルに利用する

2019-06-18 ここから追記。

元々書いていた方法だと、 build image 時に以下の問題が有ると怒られたので、おとなしく DOCKER_HOST 環境変数を使うようにしました…。

SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have ‘-rwxr-xr-x’ permissions. It is recommended to double check and reset permissions for sensitive files and directories.

以下、その手順。

1. タスクトレイアイコンの Docker を右クリック → Settings から設定を開く

2. □ Expose daemon on tcp://localhost:2375 without TLS にチェックを入れる

3. DOCKER_HOST 環境変数を Docker for Windows に向ける

export DOCKER_HOST=tcp://localhost:2375

~/.bashrc は dotfiles リポジトリで一括管理しているので、
Mac でも Windows でも同じ ~/.bashrc を使いたい。
ので、以下のようにいい感じに追記した。

# If exist docker.exe in $PATH, set env:DOCKER_HOST.
command -v docker.exe && export DOCKER_HOST=tcp://localhost:2375

4. docker run hello-world

$ curl -LRsS https://get.docker.com/ | bash
   :
   :
$ 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/

負けた。


2019-06-18 ここまで追記。

以下、元の記事を残しますが、非推奨です。


「Ubuntu いくつと Docker いくつ、特定のバージョン同士だと動作する!」とか「DOCKER_HOST=tcp://localhost:2375」みたいなのに疲れた人用。

1. (インストール済みの場合) WSL 上でインストールした Docker を削除する

最終的に以下な状態になれば OK 。

$ docker
docker: command not found

お手元の環境に合わせて適切に削除してください。

apt purge docker.io
apt purge docker-ce

など。

2. Docker for Windows をインストールする

こちら

コマンドプロンプト上から docker.exe が実行できることを確認して下さい。

C:\Users\djeeno>docker.exe

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

...

実行できない場合は PATH が通ってないです。
Docker for Windows インストール後に一度も再起動していない場合は、再起動して OS 環境変数の PATH を読み直し。

3. WSL の PATH の通ったディレクトリ配下に以下のファイルを設置する。

こちら

手っ取り早く /usr/local/bin/docker にインストールしたい場合は以下を実行してください。

sudo curl -LRsS https://raw.githubusercontent.com/djeeno/windows/master/docker -o /usr/local/bin/docker
sudo chmod +x /usr/local/bin/docker

以下、ファイルの中身を転記。

#!/bin/sh

# This script executes a command with the same file name as the executable file itself,
# or a file name with ".exe" added after that.
# It's just like BusyBox.

command_name=$(basename "$0")

if command -v "${command_name}.exe" >/dev/null; then
  exec "${command_name}.exe" "$@"
else
  exec "${command_name}" "$@"
fi

ざっくり言うと、このファイルを docker という名前で実行すると、 PATH 配下にある docker.exe を実行してくれます。

おわり

$ type docker
docker is /usr/local/bin/docker

$ 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/

シンプル。

2019年3月21日木曜日

Docker コンテナが [!!!!!!] Failed to early mount API filesystems, freezing. と言って死ぬときの回避策

TL;DR

コンテナを init で起動すると死ぬ。

$ docker run -it ubuntu:16.04 init
[!!!!!!] Failed to early mount API filesystems, freezing.

--cap-add=SYS_ADMIN と -v /sys/fs/cgroup:/sys/fs/cgroup:ro を付けたら行けた。

$ docker run -it --cap-add=SYS_ADMIN -v /sys/fs/cgroup:/sys/fs/cgroup:ro ubuntu:16.04 init
systemd 229 running in system mode. (+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN)
Detected virtualization vm-other.
Detected architecture x86-64.

Welcome to Ubuntu 16.04.5 LTS!

 ...

何か起きたか

ansible で systemd 辺りをセットアップする playbook を書いたため検証しようと思ったが、わざわざ仮想マシンを起こすのもめんどくさかった。
ので、コンテナの中で systemd を、ひいては init を起こした。

docker run -it ubuntu:16.04 init

すると、 [!!!!!!] Failed to early mount API filesystems, freezing. と出力して死んだ。

何が問題だったか

どうやらコンテナ起動コマンドを init にすると、諸々の権限が足りず起動に失敗するらしい。
( 詳細までは元気が足りず調べられていない )

解決策 or 回避策

docker run するときに capability SYS_ADMIN を追加した上で、/sys/fs/cgroup を読み込み専用でマウントしてやると起動できた。

docker run -it --cap-add=SYS_ADMIN -v /sys/fs/cgroup:/sys/fs/cgroup:ro ubuntu:16.04 init

備考

最初に Google 先生に聞いてみたときには、 docker run するときに --privileged オプションを付けて特権モードで起動しろなどという恐ろしい助言が出てきたが、試してみると確かに init は起こせた。

しかし、複数のコンテナを --privileged で起動すると謎の CPU の高騰に悩まされた。
( 確か。結構昔のことなので正確に覚えていない。 )

できることなら特権を与えるなんて危険なことはしたくなかったので、もうちょっと権限を絞りたいと思って色々試したところ、↑な感じで行けた。

SYS_ADMIN よりも細かく capabilities を絞ってできるかどうかは試せていない。


参考