NashTech Insights

How to run the docker image in the Azure function.

Mohd Muzakkir Saifi
Mohd Muzakkir Saifi
Table of Contents
close up photo of programming of codes

Hello learner, In this blog we will learn how to run the docker image in the Azure function with the ACR. I have explained in steps, you can follow this blog to set up the Azure function.

Azure function:-

Azure Functions is a serverless computing service that is provided by Azure Cloud. It allows us to run your code in an environment where you do not need to take tension to maintain that environment, no need to provision, no need to maintain, no need to scale up or down. Basically, it is designed to execute the pieces of the code with the HTTP trigger, time trigger, and many others. You can check this link to know more about it.

Key Concepts:-

Here are some key concepts in Azure Functions:

  1. Serverless: Azure Functions is based on the serverless computing model. You do not need to worry or care about the server management. Azure will handle all the operations like scaling, patching, and availability of your infrastructure. You just need to focus on your code and the function will run your code.
  2. Event-driven execution: Azure Functions is designed to respond to various events or triggers, such as an HTTP request, a message arriving in a queue, or a timer. These events can trigger the Azure function automatically.
  3. Multiple programming languages: Azure Functions supports multiple programming languages, including C#, JavaScript, PowerShell, Python, and TypeScript. It is up to you, you can choose a language and start writing code.
  4. Scalability: Azure Functions automatically scales your functions based on traffic. If your function is experiencing high traffic or low traffic then the function will scale up and scale down according to that.
  5. Billing: Azure Functions follows the pay-as-you-go pricing model, You have to pay according to your usage.
  6. Development and deployment: You can develop and test your functions locally using the Azure Functions Core Tools. Once your function is working as per exception then you can deploy on the Azure portal.

Azure Functions is widely used for building serverless applications, event-driven architectures, real-time data processing, and integration scenarios. It provides flexibility, scalability, and ease of development, allowing developers to focus on writing code and delivering business value.

Docker:


Docker is an open-source platform that enables you to package, distribute, and run applications using containers. Containers are lightweight and isolated environments that contain everything needed to run an application, including the code, dependencies, and system tools. Docker simplifies application deployment, improves scalability, and enhances the consistency of software across different environments.

Docker Images:


A Docker image is a lightweight, standalone, and executable package that contains everything needed to run a piece of software. It includes the code, runtime, system tools, libraries, and dependencies required for the application to run. Docker images are built from a set of instructions called a Dockerfile and are stored in a registry. They can be easily shared, distributed, and used to create containers on different machines or environments.

Now Its time to do the hands-on, how can we run the docker image in the Azure function:-

Prerequisite:

  • You should install Python on your system.
  • You should install Flask on your system.
  • You should install Docker on your system.

Follow the below steps to run the images:-

Here I am creating a simple hello-world application using Flask. Just install the flask in your system using pip.

pip3 install flask

After installation, You just need to create two files:-

  • app.py
  • Dockerfile
  • requirements.txt

You need to add the flask in the requirements.txt file. Just open the file and add the flask in it and using this file you can run this file and install the flask. You can also add the other package in it and install all packages using a single command:

pip3 install -r requirements.txt

In the app.py, You just need to paste the below code to see the hello-world:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
return 'Hello, World !'

if __name__ == '__main__':
app.run(host='0.0.0.0')


Now you can run this code locally by using the below commands:

flask run
or
python3 app.py

This command gives you an output with the URL. you can hit that URL in any browser and see the output.

Hello, World !

Dockerfile:-

Now you need to add the below content in the Dockerfile:

# Use the official Python base image
FROM python:3.9-slim-buster

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file to the working directory
COPY requirements.txt .

# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the Flask application code to the working directory
COPY app.py .

# Expose the port on which the Flask app will run
EXPOSE 5000

# Set the entry point command to run the Flask application
CMD ["python", "app.py"]


Now you can run your docker file using the below command:-

docker build -t flask-app:1.0 .

Your image has been built with this name and tag flask-app:1.0 . You can list the image using the below command:

docker images 

Run the docker image using this command:

docker run flask-app:1.0

The bold part is your image name you can replace this as per your understanding. You will see a URL in the output and hit that URL you will see the Hello, World!

Now, You must have the ACR and Azure Function created or you can create them using Portal or azure cli.

Now it’s time to push your image to the ACR. First of all, you need to login into the ACR, So you can use the below command to log in:

docker login <acr url> -u <username> -p <passwd>

Replace the values as per your ACR values. After this, you should tag the image with the below command:

docker tag flask-app:1.0 <acr-url>/flask-app:1.0

Now push your image to the ACR:

docker push <acr-url>/flask-app:1.0

Azure Function with ACR:-

Now we just need to configure the Azure function. Follow the below steps to configure the Azure function with ACR:-

  • Step 1:- Create an Azure function with the docker container option.
  • Step 2:- In the left navigation, select the configuration in the setting.
    • Now update the few values of docker:
      • Update this parameter with your DOCKER_REGISTRY_SERVER_PASSWORD
      • Update this parameter with your value DOCKER_REGISTRY_SERVER_URL
      • Update this parameter with your value DOCKER_REGISTRY_SERVER_USERNAME
  • Step3:- Go to the Deployment section and select the Deployment center
    • Select the source for the image.
    • configure your ACR with Azure function.

Now your ACR is configured with the Azure function. Now you can check the logs in the same deployment center section. It will show you that image is pulling from the ACR and now you can copy the URL for the Azure function and hit that URL in the browser and then you will see the output of your Docker image.

Conclusion:

In this blog, we have seen what is Azure function and docker. We have run the docker image in the Azure function after pushing that image to the ACR. You can run the docker image in the Azure function through these steps.

If you like this blogs, you can subscribe to the Nashtech blogs and follow me. you can also read my other blogs.

Mohd Muzakkir Saifi

Mohd Muzakkir Saifi

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: