LoginSignup
8
7

More than 3 years have passed since last update.

Azure VMの作成からDockerの実行環境構築まで

Last updated at Posted at 2019-08-01

はじめに

※超初心者向け

社内教育用に作成したドキュメントです。(以前投稿した記事のAzure版)
パブリッククラウドもDockerも触ったことがない人を対象にしています。

準備するもの

  • 作業マシン(Windowsを使用しました)
  • Azureアカウント

Azure VMの作成

AzureポータルからVMインスタンスを作成します。

image.png

Addをクリック。
作成画面が表示されます。

image.png

入力、変更項目は以下の通りです。

  • Resource group:VMを作成するリソースグループを選択。新規作成もできます
  • VM name:VMの名前
  • Region:VMを作成するリージョン。何でもいいですが、今回はJapan Eastを選択
  • Image:VMのOSイメージ。今回はUbuntuベースのコマンドを使用するので、デフォルトのUbuntu Server 18.04 LTSを選択
  • Size:VMのスペック。デフォルトのは高いのでグレードを落としています

image.png

  • Authentication type:ユーザー名、SSHのパブリックキーを設定します。SSHキーがない場合は、こちらを参考に作成してください
  • INBOUND PORT RULES:INBOUNDポートを選択。SSHのみで大丈夫ですが、サービスを公開する等の場合はHTTPHTTPSも選択する必要があります

image.png

  • Monitoring:モニタリングはOffにしました。VMの監視を行いたい場合はOnにしてください
  • Azure Active Directory:AADでのログインに対応します。SSHキーは一つしか設定できないので、別マシンからVMにアクセスしたい場合はOnにすると良いです
  • Auto-shutdown:VMを自動で停止する設定。VMの停止忘れで課金が発生するのを防げます

image.png

あとはバリデーションに成功することを確認し、作成を実行して数分待ちます。

image.png

VMが作成されました。

image.png

VMのOverviewから、Connectをクリック。

image.png

右側に表示される画面から、ローカルアカウントで接続するコマンドをコピーします。
シェルを起動し、コピーしたコマンドを貼り付けて実行すればVMに接続できます。

コマンドプロンプトはデフォルトではSSH接続できないので、PowershellGit Bashを使用すると良いです。

※AADで接続する場合は、こちらの手順2以降を行ってください

Dockerの環境構築

インストール

以下の2つの方法で、Dockerをインストールできます。

パッケージ管理ツールを使用

Ubuntuのパッケージ管理ツールaptを使用してインストールできます。

$ sudo apt update
$ sudo apt install docker.io

公式サイトからダウンロード

Ubuntu用の公式サイトはこちら
いくつかの方法が記載されていますが、スクリプトで実行するのが楽です。
※インターネットからダウンロードしたスクリプトは、実行前に必ず確認を行いましょう

$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh

インストールの確認

バージョンが確認できればOKです。

$ sudo docker version
Client: Docker Engine - Community
 Version:           19.03.1
 API version:       1.40
 Go version:        go1.12.5
 Git commit:        74b1e89
 Built:             Thu Jul 25 21:21:05 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.1
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.5
  Git commit:       74b1e89
  Built:            Thu Jul 25 21:19:41 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.6
  GitCommit:        894b81a4b802e4eb2a91d1ce216b8817763c29fb
 runc:
  Version:          1.0.0-rc8
  GitCommit:        425e105d5a03fabd737a126ad93d62a9eeede87f
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Dockerコンテナを起動してみます。

$ sudo docker container run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

(以下略)

Dockerの実行にはroot権限が必要です。
以下のようにsudoをつけずに実行することもできますが、コンテナにホスト側のroot権限を与えてしまうため注意が必要です。

$ sudo usermod -aG docker $USER

(再ログイン)

$ docker container run hello-world

Warning:
Adding a user to the “docker” group grants them the ability to run containers which can be used to obtain root privileges on the Docker host. Refer to Docker Daemon Attack Surface for more information.

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