DEV Community

Cover image for Creating and Deploying a Custom Docker Image with Azure Container Registry and Azure Container Instances
Theodora
Theodora

Posted on

Creating and Deploying a Custom Docker Image with Azure Container Registry and Azure Container Instances

Table of Contents

Prerequisites

Make sure you have the following installed:

Step 1: Create a Simple Dockerfile and Shell Script

Create a new directory, then inside it:

This is a minimal Dockerfile using the nginx base image and a custom shell script.

FROM nginx:latest

This tells Docker to use the latest version of the official nginx image from Docker Hub as the base image.

  • The base image already includes Nginx installed and configured.
  • It's lightweight and typically used for serving web content.

ARG VERSION

This declares a build-time argument named VERSION.

  • ARG is only available during the image build process.
  • You can pass it using -build-arg like this:

docker build --build-arg VERSION=1.2.3
### ENV VERSION=$VERSION

This sets an environment variable inside the container using the value passed during build time.

  • ENV is available at runtime — your script or Nginx can access it.
  • You can verify it with: echo $VERSION

COPY hostname.sh .

This copies the hostname.sh script from your local directory into the image's working directory (which is / by default unless changed).

  • If you want it in a specific directory like /usr/local/bin, you should write: COPY hostname.sh /usr/local/bin/

CMD ["/hostname.sh"]

This sets the default command that runs when the container starts.

  • It overrides Nginx’s default command.
  • So instead of starting Nginx, the container will run the hostname.sh script.

Note If the script isn't executable or doesn't start with a shebang (#!/bin/bash or similar), the container will fail.

This Dockerfile creates a container that:

  1. Uses the latest Nginx image.
  2. Accepts a version number at build time.
  3. Sets that version as an environment variable.
  4. Copies a script called hostname.sh into the container.
  5. Runs that script instead of Nginx when the container starts

Script Breakdown: hostname.sh

#!/bin/sh

  • This line tells the system to use the sh shell to run the script.

HOSTNAME=\hostname``

  • Runs the hostname command, capturing the machine/container name.
  • Stores it in the variable HOSTNAME.

VERSION=${VERSION:-v1}

  • Reads the VERSION environment variable if it’s set.
  • If not, it defaults to v1.

Creates a landing HTML page for the root of the Nginx server:

cat > /usr/share/nginx/html/index.html <<EOF
...
EOF

  • This file will be served when someone visits / in the browser.
  • It includes:
    • Writes an HTML file directly to the default Nginx directory.
    • The hostname (of the container or VM)
    • The version number

mkdir /usr/share/nginx/html/healthz /usr/share/nginx/html/hostname /usr/share/nginx/html/version

  • Creates 3 subdirectories inside Nginx's web root:
    • /healthz – for liveness/readiness checks
    • /hostname – for showing the container’s hostname and version
    • /version – for showing the version only

Writes mini HTML/text responses to each endpoint:
cat > /usr/share/nginx/html/hostname/index.html <<EOF
$HOSTNAME -- $VERSION
EOF

  • Shows: hostname -- version
  • Endpoint: /hostname

cat > /usr/share/nginx/html/version/index.html <<EOF
$VERSION
EOF

  • Endpoint: /version
  • Shows: version only

chmod 777 /usr/share/nginx/html/healthz
cat > /usr/share/nginx/html/healthz/index.html <<EOF
healthy
EOF

  • Grants all permissions to /healthz directory (not ideal for production, but okay for dev/demo).
  • Writes a file that says healthy to /healthz/index.html
  • This is useful for Kubernetes or Docker health checks.

nginx -g "daemon off;"

  • Starts Nginx in foreground mode (so the container doesn't exit).
  • This is required in Docker — if the main process exits, the container stops.

When this script runs inside a Docker container, it:

If you access the container in a browser (or curl):

  • / → HTML page showing the hostname and version
  • /healthz → returns healthy
  • /hostname → returns hostname -- version
  • /version → returns the version string

Step 2: Build the Docker Image

Open PowerShell in the folder and run:

Output

Step 3: Set Up Azure Container Registry

In the Azure Portal:

  1. Search for Container Registry
  2. Click Create
  3. Choose a name (mine was hostcr)
  4. Set the resource group, region, SKU, etc.
  5. Enable Admin User under Access Keys after creation

Step 4: Push Docker Image to ACR

1.Authenticate with Azure:

  1. Tag Your Docker Image

  1. Log in to ACR

Output

  1. Push the Image to ACR

Output


boops-I lost my screenshot of the Azure intercae for this

Step 5: Deploy to Azure Container Instances

  1. In the Azure portal, search for Container Instances
  2. Click Create
  3. Fill in:
    • Name
    • Resource Group
    • Image source: Azure Container Registry
    • Image: hostcr.azurecr.io/hostname:v1
  4. Review and create

Common Errors and Fixes

docker push returns "authentication required"

Fix: Make sure you're logged into ACR:

az login fails due to tenant mismatch

Fix: Use the right tenant ID explicitly:

✅ Final Checks

  1. Go to your deployed Container Instance
  2. Copy the public IP
  3. Visit in browser:

You should see HTML responses with hostname, version, and health check status.

Top comments (0)