{"id":3940,"date":"2023-11-04T23:13:57","date_gmt":"2023-11-04T23:13:57","guid":{"rendered":"http:\/\/localhost:10003\/building-and-deploying-a-docker-container-to-azure-kubernetes-service\/"},"modified":"2023-11-05T05:48:26","modified_gmt":"2023-11-05T05:48:26","slug":"building-and-deploying-a-docker-container-to-azure-kubernetes-service","status":"publish","type":"post","link":"http:\/\/localhost:10003\/building-and-deploying-a-docker-container-to-azure-kubernetes-service\/","title":{"rendered":"Building and deploying a Docker container to Azure Kubernetes Service"},"content":{"rendered":"
Docker containers have become the standard for packaging and deploying applications in modern software development. These containers provide a consistent runtime environment that can be easily moved between development, testing, and production environments. Kubernetes, on the other hand, is the most popular container orchestration platform that provides robust management, scaling, and deployment of containerized applications.<\/p>\n
In this tutorial, we will walk you through the process of building and deploying a Docker container to Azure Kubernetes Service (AKS). AKS is a fully managed Kubernetes service provided by Microsoft Azure that simplifies the deployment and management of containerized applications.<\/p>\n
Before we get started, make sure you have the following prerequisites in place:<\/p>\n
The first step is to create a Docker image that contains the application code, dependencies, and other necessary files. To create a Docker image, we need a Dockerfile that defines the steps needed to build the image.<\/p>\n
In this example, we will be using an example Node.js application to demonstrate building a Docker image. Create a new directory on your computer, navigate to it, and create a new file called Here is what the Dockerfile does:<\/p>\n This Dockerfile defines a simple Node.js application and installs its dependencies in the container. You can modify it to suit your application needs. Save the Dockerfile and navigate to the application’s root directory in your terminal.<\/p>\n To build the Docker image, run the following command:<\/p>\n This command builds a Docker image with the tag Once the image is built successfully, you can test it by running it locally:<\/p>\n This command runs the Docker image and maps port 3000 in the container to port 3000 on your local machine. The The next step is to push the Docker image to a container registry. The container registry is where Kubernetes pulls the Docker images from when it deploys the application.<\/p>\n Azure offers a fully managed container registry called Azure Container Registry (ACR) that provides secure storage and easy integration with other Azure services such as AKS. To push the Docker image to ACR, you first need to create a new registry in your Azure account.<\/p>\n Run the following Azure CLI command to create a new container registry:<\/p>\n Replace Once the container registry is created successfully, you need to log in to the registry using Docker:<\/p>\n Replace The first command tags your Docker image with the registry address and application image name. The second command pushes the image to the container registry. This command may take a few minutes to complete depending on the size of your image.<\/p>\n You can verify that the image is pushed successfully by visiting the container registry in the Azure portal and checking the repository’s name and tag.<\/p>\n The final step is to deploy the Docker container to AKS. To create a new Kubernetes deployment, you need to create a YAML configuration file that describes the application deployment.<\/p>\n Create a new file called This YAML file describes a Kubernetes deployment with the following properties:<\/p>\n The template specifies the configuration for the individual pods that make up the deployment. In this case, the template includes a single container that runs the Save the This command creates a new Kubernetes deployment based on the configuration specified in the YAML file. You can check the status of your deployment by running the following command:<\/p>\n This command should show your newly created pod with the name Finally, to expose the application to the outside world, you need to create a Kubernetes service. Create a new file called This YAML file describes a Kubernetes service with the following properties:<\/p>\n The Save the This command creates a new Kubernetes service based on the configuration specified in the YAML file. You can check the status of your service by running the following command:<\/p>\n This command should show your newly created service with the name In this tutorial, we walked you through the process of building and deploying a Docker container to Azure Kubernetes Service. We started by creating a Dockerfile for the application and building a Docker image. Then, we pushed the Docker image to Azure Container Registry and created a Kubernetes deployment and service using YAML configuration files. Finally, we exposed the service through an external IP address to access the application externally.<\/p>\n This tutorial covers the basic steps required to deploy a containerized application on AKS. However, there are many other Kubernetes concepts and features that you should explore to become proficient in deploying and managing containerized applications in production environments.<\/p>\n","protected":false},"excerpt":{"rendered":" Docker containers have become the standard for packaging and deploying applications in modern software development. These containers provide a consistent runtime environment that can be easily moved between development, testing, and production environments. Kubernetes, on the other hand, is the most popular container orchestration platform that provides robust management, scaling, Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[463,465,30,464,462],"yoast_head":"\nDockerfile<\/code> with the following content:<\/p>\n
# Define the base image to use\nFROM node:14-alpine\n\n# Set working directory for subsequent commands\nWORKDIR \/app\n\n# Copy package.json and package-lock.json\nCOPY package*.json .\/\n\n# Install dependencies\nRUN npm install\n\n# Copy application files to the container image\nCOPY . .\n\n# Expose the port on which the application listens\nEXPOSE 3000\n\n# Start the application\nCMD [\"npm\", \"start\"]\n<\/code><\/pre>\n
\n
WORKDIR<\/code> instruction sets the working directory for subsequent instructions.<\/li>\n
package.json<\/code> and
package-lock.json<\/code> to the container.<\/li>\n
npm install<\/code> command.<\/li>\n
docker build -t myapp:v1 .\n<\/code><\/pre>\n
myapp:v1<\/code> using the Dockerfile located in the current directory. The
.<\/code> at the end of the command specifies the build context. The build context is the location where Docker looks for files when it builds the image. In this case, it is the current directory. This command may take a few minutes to complete depending on the size of your application and the number of dependencies it has.<\/p>\n
docker run -p 3000:3000 myapp:v1\n<\/code><\/pre>\n
myapp:v1<\/code> tag refers to the image you built in the previous step. You should now be able to access the application by navigating to `http:\/\/localhost:3000` in your browser.<\/p>\n
Pushing the Docker Image to Azure Container Registry<\/h2>\n
az acr create --resource-group <my-resource-group> --name <my-acr-name> --sku Basic\n<\/code><\/pre>\n
<my-resource-group><\/code> with the name of your existing Azure resource group, and
<my-acr-name><\/code> with a unique name for your container registry. You can also choose a different Azure SKU other than
Basic<\/code> depending on your requirements and budget.<\/p>\n
docker login <my-acr-name>.azurecr.io\n<\/code><\/pre>\n
<my-acr-name><\/code> with the name of your container registry. Docker prompts you for your Azure login credentials. Once authenticated, you can push the Docker image to ACR:<\/p>\n
docker tag myapp:v1 <my-acr-name>.azurecr.io\/myapp:v1\n\ndocker push <my-acr-name>.azurecr.io\/myapp:v1\n<\/code><\/pre>\n
Deploying the Application to Azure Kubernetes Service<\/h2>\n
deployment.yml<\/code> in your application’s root directory with the following content:<\/p>\n
apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n name: myapp\nspec:\n replicas: 1\n selector:\n matchLabels:\n app: myapp\n template:\n metadata:\n labels:\n app: myapp\n spec:\n containers:\n - name: myapp\n image: <my-acr-name>.azurecr.io\/myapp:v1\n ports:\n - containerPort: 3000\n imagePullPolicy: Always\n<\/code><\/pre>\n
\n
apiVersion<\/code>: The Kubernetes API version to use for this deployment.<\/li>\n
kind<\/code>: The Kubernetes resource type, which is “Deployment” in this case.<\/li>\n
metadata<\/code>: The metadata for the deployment, such as the name of the deployment.<\/li>\n
spec<\/code>: The specification of the deployment, which includes the desired number of replicas, the selector used to match the pods, and the template for the pods.<\/li>\n<\/ul>\n
myapp:v1<\/code> Docker image from the ACR container registry.<\/p>\n
deployment.yml<\/code> file and run the following command to deploy the application to AKS:<\/p>\n
kubectl apply -f deployment.yml\n<\/code><\/pre>\n
kubectl get pods\n<\/code><\/pre>\n
myapp<\/code> and status as
Running<\/code>.<\/p>\n
service.yml<\/code> in your application’s root directory with the following content:<\/p>\n
apiVersion: v1\nkind: Service\nmetadata:\n name: myapp\nspec:\n selector:\n app: myapp\n ports:\n - protocol: TCP\n port: 80\n targetPort: 3000\n type: LoadBalancer\n<\/code><\/pre>\n
\n
apiVersion<\/code>: The Kubernetes API version to use for this service.<\/li>\n
kind<\/code>: The Kubernetes resource type, which is “Service” in this case.<\/li>\n
metadata<\/code>: The metadata for the service, such as the name of the service.<\/li>\n
spec<\/code>: The specification of the service, which includes the selector used to match the pods and the port configuration.<\/li>\n<\/ul>\n
targetPort<\/code> is set to
3000<\/code> to match the port exposed by the application in the Docker container. The
type<\/code> is set to
LoadBalancer<\/code> to expose the service through an external IP address.<\/p>\n
service.yml<\/code> file and run the following command to create the service:<\/p>\n
kubectl apply -f service.yml\n<\/code><\/pre>\n
kubectl get service\n<\/code><\/pre>\n
myapp<\/code> and an external IP address that you can use to access the application externally.<\/p>\n
Conclusion<\/h2>\n