Install docker after deploy virtual machine

Nasimjon Tohirov 261 Reputation points
2023-08-18T13:20:55.9166667+00:00

Hello everyone,

Just one question about VM. I would like to install some dependencies after successfully finished deploy the VM.

This approach command below, won't work, the processing is pretty slow:

az vm run-command invoke
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,013 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Prrudram-MSFT 28,201 Reputation points Moderator
    2023-08-23T18:23:50.9066667+00:00

    Hello @Nasimjon Tohirov

    If you find that the az vm run-command invoke command is too slow for your needs, you can consider using a custom script extension to install Docker on the VM after it has been deployed. Here's an example of how you can use a custom script extension to install Docker on a Linux VM:

    Create a shell script that installs Docker. For example, you can create a file named install-docker.sh with some similar contents as below

    #!/bin/bash
    
    # Install Docker
    sudo apt-get update
    sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent 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 -y docker-ce docker-ce-cli containerd.io
    
    # Add the current user to the docker group
    sudo usermod -aG docker $USER
    
    
    

    Upload the script to a public URL. For example, you can upload the script to a GitHub repository and use the raw URL.Create a custom script extension for the VM.

    Azure CLI command to create the extension:

    az vm extension set \ --resource-group <resource-group-name> \ --vm-name <vm-name> \ --name customScript \ --publisher Microsoft.Azure.Extensions \ --version 2.1 \ --settings '{"fileUris":["<script-url>"],"commandToExecute":"./install-docker.sh"}'

    Replace <resource-group-name> and <vm-name> with the name of your resource group and VM, respectively. Replace <script-url> with the URL of the script you uploaded in step 2.

    Wait for the extension to complete. You can monitor the status of the extension using the following Azure CLI command:

    az vm extension list \ --resource-group <resource-group-name> \ --vm-name <vm-name> \ --query "[].{Name:name, ProvisioningState:provisioningState, Status:status.message}" \ --output table

    Reference

    https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-linux

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.