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 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.
Here are some key concepts in Azure Functions:
- 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.
- 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.
- 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.
- Billing: Azure Functions follows the pay-as-you-go pricing model, You have to pay according to your usage.
- 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 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.
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:-
- 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:-
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__)
return 'Hello, World !'
if __name__ == '__main__':
Now you can run this code locally by using the below commands:
This command gives you an output with the URL. you can hit that URL in any browser and see the output.
Hello, World !
Now you need to add the below content in the Dockerfile:
# Use the official Python base image
# Set the working directory in the container
# 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
# 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:
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
- Now update the few values of docker:
- 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.
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.