Running Docker Compose

We often like to run our tests and things using docker-compose, so that we can spin up any required services as needed - databases and so on. Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.

  1. Running Docker Compose
  2. Getting Started With Compose

Step 2: Customize and Push to Docker Hub

The last step used a Docker image which Microsoft publishes and maintains. Next step, create your own custom image. You should have a Docker ID, you probably created it to download Docker Desktop.

In your favorite text editor create a file called Dockerfile in the same C:temp directory. No extension, just Dockerfile. Paste in this code and save the file:

This tells Docker to use the same IIS base image, and create a layer that adds in the HTML you created in the last step. Instead of manually copying a file into the container, you will create an image with your HTML already inside it. To build the image, in your terminal, type:

Two things, first replace <YourDockerID> with your Docker ID. Also notice the “.” at the end of the line. That tells Docker to build in the context of this directory. So when it looks to COPY the file to /inetpub/wwwroot it will use the file from this directory.

You can run it:

And go to http://localhost:8081 to see the page - note your original container is still running and you can see it at http://localhost:8080.

Finally push to Docker Hub:

You may be asked to login if you haven’t already. Then you can go to hub.docker.com, login and check your repositories

Finally, stop and remove the running containers:

-->

In this tutorial, you use Docker Compose to define and run a multi-container application locally and then deploy it as a container group in Azure Container Instances.

Running

Run containers in Azure Container Instances on-demand when you develop cloud-native apps with Docker and you want to switch seamlessly from local development to cloud deployment. This capability is enabled by integration between Docker and Azure. You can use native Docker commands to run either a single container instance or multi-container group in Azure.

Important

Not all features of Azure Container Instances are supported. Provide feedback about the Docker-Azure integration by creating an issue in the Docker ACI Integration GitHub repository.

Tip

You can use the Docker extension for Visual Studio Code for an integrated experience to develop, run, and manage containers, images, and contexts.

In this article, you:

  • Create an Azure container registry
  • Clone application source code from GitHub
  • Use Docker Compose to build an image and run a multi-container application locally
  • Push the application image to your container registry
  • Create an Azure context for Docker
  • Bring up the application in Azure Container Instances

Prerequisites

  • Azure CLI - You must have the Azure CLI installed on your local computer. Version 2.10.1 or later is recommended. Run az --version to find the version. If you need to install or upgrade, see Install the Azure CLI.

  • Docker Desktop - You must use Docker Desktop version 2.3.0.5 or later, available for Windows or macOS. Or install the Docker ACI Integration CLI for Linux.

Create Azure container registry

Before you create your container registry, you need a resource group to deploy it to. A resource group is a logical collection into which all Azure resources are deployed and managed.

Create a resource group with the az group create command. In the following example, a resource group named myResourceGroup is created in the eastus region:

Once you've created the resource group, create an Azure container registry with the az acr create command. The container registry name must be unique within Azure, and contain 5-50 alphanumeric characters. Replace <acrName> with a unique name for your registry:

Here's partial output for a new Azure container registry named mycontainerregistry082:

The rest of the tutorial refers to <acrName> as a placeholder for the container registry name that you chose in this step.

Running Docker Compose

Log in to container registry

You must log in to your Azure Container Registry instance before pushing images to it. Use the az acr login command to complete the operation. You must provide the unique name you chose for the container registry when you created it.

For example:

Getting Started With Compose

The command returns Login Succeeded once completed:

Get application code

The sample application used in this tutorial is a basic voting app. The application consists of a front-end web component and a back-end Redis instance. The web component is packaged into a custom container image. The Redis instance uses an unmodified image from Docker Hub.

Use git to clone the sample application to your development environment:

Change into the cloned directory.

Inside the directory is the application source code and a pre-created Docker compose file, docker-compose.yaml.

Modify Docker compose file

Open docker-compose.yaml in a text editor. The file configures the azure-vote-back and azure-vote-front services.

In the azure-vote-front configuration, make the following two changes:

  1. Update the image property in the azure-vote-front service. Prefix the image name with the login server name of your Azure container registry, <acrName>.azurecr.io. For example, if your registry is named myregistry, the login server name is myregistry.azurecr.io (all lowercase), and the image property is then myregistry.azurecr.io/azure-vote-front.
  2. Change the ports mapping to 80:80. Save the file.

The updated file should look similar to the following:

By making these substitutions, the azure-vote-front image you build in the next step is tagged for your Azure container registry, and the image can be pulled to run in Azure Container Instances.

Tip

You don't have to use an Azure container registry for this scenario. For example, you could choose a private repository in Docker Hub to host your application image. If you choose a different registry, update the image property appropriately.

Run multi-container application locally

Run docker-compose up, which uses the sample docker-compose.yaml file to build the container image, download the Redis image, and start the application:

When completed, use the docker images command to see the created images. Three images have been downloaded or created. The azure-vote-front image contains the front-end application, which uses the uwsgi-nginx-flask image as a base. The redis image is used to start a Redis instance.

Run the docker ps command to see the running containers:

To see the running application, enter http://localhost:80 in a local web browser. The sample application loads, as shown in the following example:

After trying the local application, run docker-compose down to stop the application and remove the containers.

Push image to container registry

To deploy the application to Azure Container Instances, you need to push the azure-vote-front image to your container registry. Run docker-compose push to push the image:

It can take a few minutes to push to the registry.

To verify the image is stored in your registry, run the az acr repository show command:

Create Azure context

To use Docker commands to run containers in Azure Container Instances, first log into Azure:

When prompted, enter or select your Azure credentials.

Create an ACI context by running docker context create aci. This context associates Docker with an Azure subscription and resource group so you can create and manage container instances. For example, to create a context called myacicontext:

When prompted, select your Azure subscription ID, then select an existing resource group or create a new resource group. If you choose a new resource group, it's created with a system-generated name. Azure container instances, like all Azure resources, must be deployed into a resource group. Resource groups allow you to organize and manage related Azure resources.

Run docker context ls to confirm that you added the ACI context to your Docker contexts:

Deploy application to Azure Container Instances

Next, change to the ACI context. Subsequent Docker commands run in this context.

Run docker compose up to start the application in Azure Container Instances. The azure-vote-front image is pulled from your container registry and the container group is created in Azure Container Instances.

Note

Docker Compose commands currently available in an ACI context are docker compose up and docker compose down. There is no hyphen between docker and compose in these commands.

In a short time, the container group is deployed. Sample output:

Run docker ps to see the running containers and the IP address assigned to the container group.

Sample output:

To see the running application in the cloud, enter the displayed IP address in a local web browser. In this example, enter 52.179.23.131. The sample application loads, as shown in the following example:

To see the logs of the front-end container, run the docker logs command. For example:

You can also use the Azure portal or other Azure tools to see the properties and status of the container group you deployed.

Compose

When you finish trying the application, stop the application and containers with docker compose down:

This command deletes the container group in Azure Container Instances.

Next steps

In this tutorial, you used Docker Compose to switch from running a multi-container application locally to running in Azure Container Instances. You learned how to:

  • Create an Azure container registry
  • Clone application source code from GitHub
  • Use Docker Compose to build an image and run a multi-container application locally
  • Push the application image to your container registry
  • Create an Azure context for Docker
  • Bring up the application in Azure Container Instances

You can also use the Docker extension for Visual Studio Code for an integrated experience to develop, run, and manage containers, images, and contexts.

If you want to take advantage of more features in Azure Container Instances, use Azure tools to specify a multi-container group. For example, see the tutorials to deploy a container group using the Azure CLI with a YAML file, or deploy using an Azure Resource Manager template.