tabimoba.net

とあるエンジニアの雑記帳

LXD上でDockerを動かす方法

2018/11/27 追記

Dockerサービスの起動に失敗する場合は、以下を参照ください。 https://qiita.com/tabimoba/items/4f7af774145c0dbd70d9

1.コンテナの作成

dockerプロファイルはLXD 2.14より削除されました。

Google等で検索すると、dockerプロファイルを指定した手順が検索結果に引っかかりますが、上記理由によりdockerプロファイルを指定した手順はエラーになります。このため、今後は以下の手順で進めていきます。

LXDコンテナでDockerを動作させる際は、以下の通りオプションを付加して、コンテナを作成します。 ※ -c security.privileged=true (特権コンテナとしてLXDコンテナを動作させる)は作成されるDockerコンテナによっては正常に動作しない場合があるため、ここでは付加しています。

Ubuntu 18.04

# lxc launch ubuntu:18.04 <LXDコンテナ名> -c security.nesting=true -c security.privileged=true

Ubuntu 16.04

ディストリビューションの標準パッケージで提供されるLXDを使用する場合は、従来通りdockerプロファイルを指定した手順で対応します。(下記参照)

LXD 2.14およびそれ以降を利用している場合は、次のコマンドを実行します。

# lxc launch ubuntu:16.04 <LXDコンテナ名> -c security.nesting=true -c security.privileged=true

2.Docker CEのインストール

https://docs.docker.com/install/linux/docker-ce/ubuntu/ を参考にインストールを進めます。(LXDコンテナに入っている状態で以下のコマンドを実行します)

$ sudo apt-get remove docker docker-engine docker.io
$ sudo apt-get update
$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce

3.DockerコンテナをLXDコンテナ上で作成する

LXDコンテナへ入り、Dockerでhello-worldを実行してみます。(hello-worldというDockerイメージからDockerコンテナを作成して、実行してみます)

$ lxc exec <LXDコンテナ名> /bin/bash
# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
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/