Azure VM で Docker イメージを作成して Azure コンテナー レジストリ に登録する手順を解説します。
👇これより先は下記記事の内容を前提とします
Docker をAzure VM(Ubuntu)にインストールする
# Add Docker's official GPG key:
sudo apt-get -y update
sudo apt-get -y install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get -y update
# Install the Docker packages
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
ログインユーザへの権限付与
sudo usermod -G docker admin01
sudo su - admin01
動作確認
sudo 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/
カスタムイメージの作成
👇nginxのイメージを起動
docker run -it --rm -d --network host --name web nginx
👇http(80)ポートの解放
az vm open-port^
--resource-group sampleResourceGroup^
--name ubuntu-vm^
--port 80
👇ブラウザからVMのIPへのhttpアクセスを確認します。
http://***.***.***.***/
👇nginxのイメージを停止
docker stop web
👇動作確認用のhtmlファイルを作成します。
vi index.html
👇index.html
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Docker Nginx</title>
</head>
<body>
<h2>テストイメージ確認(2024年01月21日)</h2>
</body>
</html>
👇コンテナイメージ作成用のDockerfileファイルを作成します。
vi Dockerfile
FROM nginx:latest
COPY ./index.html /usr/share/nginx/html/index.html
👇コンテナイメージ作成
docker build -t webserver .
👇コンテナを起動
docker run -it --rm -d --network host webserver
👇ブラウザからVMのIPへのhttpアクセスを確認します。
http://***.***.***.***/
コンテナー レジストリの作成
👇コンテナー レジストリの作成
コンテナレジストリの名称は一意である必要があります。
下記containerregistory01
は私が登録してしまったので、別の名称を設定して下さい。
az acr create^
--name containerregistory01^
--resource-group sampleResourceGroup^
--sku Basic^
--admin-enabled true
👇コンテナー レジストリログイン情報の取得
az acr credential show^
--resource-group main_resource_group_stg^
--name containerregistory01
下記のような出力が得られるのでメモしておきます。
{
"passwords": [
{
"name": "password",
"value": "**********"
},
{
"name": "password2",
"value": "**********"
}
],
"username": "containerregistory01"
}
👇コンテナー レジストリへのログイン
上記手順で取得したusername
を使ってログインします。
docker login containerregistory01.azurecr.io --username containerregistory01
下記のように、Password:
を聞かれるので、上記手順で取得したpassword
を使ってログインします。
admin01@ubuntu-vm:~$ docker login containerregistory01.azurecr.io --username containerregistory01
Password:
👇プッシュ用のタグを作成します。
docker tag webserver containerregistory01.azurecr.io/webserver:latest
👇プッシュします。
docker push containerregistory01.azurecr.io/webserver:latest
👇関連記事
👇参考URL
- Install Docker Engine on Ubuntu
- NGINX ドッカーの公式イメージの使用方法
- Networking using the host network
- az vm open-port
- コンテナー レジストリの作成
[keywords]
Azure VM Docker コンテナーレジストリ
Azure VM で Docker イメージを作成して Azure コンテナー レジストリ に登録する
更新日:2024年01月23日