Creating and Storing a Docker Image in Azure Container Registry

Created By

Mario Mendieta

July 21, 2019

Getting Started

  • In this tutorial we will create a Docker image and privately store it in the cloud using Azure Container Registry. We will start by writing a shell script that prints "Hello World". We will package this script into a Docker image using a Dockerfile, and then push it to the cloud in Azure Container Registry.

    This tutorial requires Azure CLI. To install it visit the official documentation. You must also have Docker installed locally. To get the latest version of Docker, follow the official guide here.

What is Docker?

  • Docker is a tool designed to create, deploy, and run applications by using containers. A container is a package in which you place your application and all its parts, such as libraries and other dependencies, so that you can easily ship them all out as a single unit.

    Think of Docker like a virtual machine, but rather than creating a virtual OS, you use the same OS as the host system - only shipping your application with things not already running on the host.

Writing the Shell Script

  • Let's create a directory where we will place our script and required Docker file for containerization:

    mkdir ContainerEngineDemo
    cd ContainerEngineDemo
    Now create a new file, call it HelloWorld.sh:

    nano HelloWorld.sh
    Add the following code and save it:

    #!/bin/bash
    echo "Hello WOrld"

    Set the script executable permission:

    chmod +x HelloWorld.sh
    Test the creation by running the script:

    ./HelloWorld.sh

Assembling the Image with a Dockerfile

  • A Dockerfile defines the environment inside your container, what resources are required, what needs to be virtualized, and what ports need to be open.

    Let's start by creating a new file in the directory where your script was created, we will call it Dockerfile.

    nano Dockerfile
    Copy and paste the following content into the Dockerfile and save it:

    FROM bash:4.4
    COPY HelloWorld.sh /
    CMD ["bash", "/HelloWorld.sh"]

    We are now ready to build and run the image. The following command will create a Docker image, and name it using the --tag option:

    docker build --tag=containerenginedemo
    At this point, our Docker image has been created, we can run it to test it:

    docker run -it --rm --name container-engine-app containerenginedemo
    The response should display:

    Hello World
    You just ran the shell script that you created in the previous section within the new Docker image. You can run the image on any OS and any machine, and the result should be the same.

Storing the Image in Azure Container Registry

  • All resources in Azure must be deployed into a resource group. A resource group is a collection in which you organize all related resources, so that you can manage them as a single unit. For more information on resource groups, visit the official documentation.

    We will create a resource group in the East US region:

    az group create --name ContainerEngineDemoRG --location eastus
    We will now create an Azure Container Registry. This is a managed, private Docker registry service, where we can store our image in the cloud.

    To create the container registry, run the following command:

    az acr create --name containerenginedemoregistry --resource-group ContainerEngineDemoRG --sku Basic --admin-enabled true
    This will display a username along with two passwords. We will use these credentails to login to the container registry and upload the image:

    docker login containerenginedemoregistry.azurecr.io --username [registry-username]
    Where [registry-username] is the username you got when creating the container registry.

    We are now ready to push our image to the registry. However, in order to do that we must first tag it with the registry hostname.

    docker tag containerenginedemo containerenginedemoregistry.azurecr.io/containerenginedemo:v1.0.0
    Let's now push the image:

    docker push containerenginedemoregistry.azurecr.io/containerenginedemo:v1.0.0
    We will now verify that the push has been successful by opening a new terminal window, and listing all images stored in our registry:

    az acr repository list -n labenginedemocontainerregistry
    This will display something similar to:

    [
      "containerenginedemo"
    ]

    This confirms that our Docker image is now privately stored in our container registry in Azure.

Get in Touch

Tell us about your project. We will get back to you as soon as possible.