1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Windows上でDockerコンテナを動かす準備

1
Last updated at Posted at 2022-04-15

業務でDockerを使っているので、復習もかねて自宅環境からDockerの基本をおさらいする。

5月28日より配信が開始されたWindows 10の大型アップデート、Windows 10 May 2020 Updateが正式リリースされました。それと同時に配信を開始したWSL 2(Windows Subsystem for Linux 2)について・・・(中略)

とのことなので、WSL2を使用してLinux環境をWindows上に簡単に構築してみる。

自分の環境

Windows 10 Home
バージョン 20H2(OSビルド 19042.1348)

※コマンドプロンプトから winverで確認できる。

1. WSL2のインストール

まずはWSL2をインストールして、Linux環境を立ち上げる。
難しいことは何一つない。
powershellを管理者権限で起動し、以下のコマンドを実施するだけ。

wsl --install

これで、デフォルトのLinuxとしてUbuntsもインストール完了。
(簡単すぎる。。。)
※インストールを完了するには、再起動が必要。

2. Ubuntsuの起動とLinuxの初期設定

再起動が完了すると、自動的にUbuntsuが起動し、インストールを開始する。
数分待つとLinuxの初期設定を要求される。

ユーザー名とパスワードを設定
user / pass とかで。
※パスワードは入力しても見えないので注意。異常ではない。

3. Dockerのインストール

Linuxの環境が立ち上がったところで、Dockerが動く環境を構築する。

Docker Engineには、無償のコミュニティ版と有償のエンタープライズ版がありますが、本稿ではコミュニティ版のみを扱います。

Dockerの公式docsからインストールをする。

以下のコマンドでUbuntsuのバージョンを確認できる。

cat /etc/issue

Docker Engineの実行に必要なパッケージ一式をインストールします。
apt-getコマンドは、Debian系のディストリビューション(DebianやUbuntu)のパッケージ管理システムであるAPT(Advanced Package Tool)ライブラリを利用してパッケージを操作・管理するコマンド。

$ sudo apt-get update
$ sudo apt-get -y install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

GPGキーを追加する
 Dockerオフィシャルサイトのキーをダウンロードして登録します。

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Dockerダウンロードサイトを登録する
 Dockerダウンロードサイトをaptリポジトリに追加します。

sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

Docker Engineをインストールする
 次のようにして、Docker Engineをインストールします。

sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io

ubuntuユーザーでdockerを利用できるようにする
 既定ではrootユーザーでしかdocker操作できないため、ubuntuユーザーでもdocker操作できるようにしておきます。次のようにして権限を与えます。

sudo gpasswd -a user docker

ログインし直す
前の設定は、次回ログインより有効であるため、一度ログオフしてログインし直します。

以上でDocker Engineのインストールは完了です。インストールが完了すると、dockerコマンドが使えるようになります。次のように実行してバージョン番号を確認することで、正しく動作することを確認しておきましょう

4. Dockerコマンドを実行する。

docker version

でインストールされていることを確認。

すると、以下のエラーが。

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

どうやら、Dockerデーモンが起動していないようだ。

sudo cgroupfs-mount
sudo service docker start

これでDockerを起動。すると以下のエラー。

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/version": dial unix /var/run/docker.sock: connect: permission denied

dockerコマンドはデフォルトではroot権限なしには叩けないようになっています。 そこでdockerグループにユーザーを追加し、sudoなしでも叩けるようにしましょう。

まず以下を実行し、現在ログインしているユーザーをdockerグループへ追加。

sudo gpasswd -a $(whoami) docker

次にdocker.sock にグループでの書き込み権限を付与。一度ログインしなおす。

sudo chgrp docker /var/run/docker.sock

最後にdocker daemonを再起動します。

sudo service docker restart

これでDockerコマンドが使えるようになった。

Dockerのデーモン自動起動設定は、公式ドキュメントにもあるように方法はあるようだが、
Ubuntsu20.04ではできなそうだった。
shスクリプトを作るなどすればできるが。そこまではやらない。

次回↓、はDokcerで実際にコンテナを動かす。

この記事の参考↓

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?