This lab is part of a series of labs for Mastering Container Offers Workshop. You must finish this lab before moving on to the next sections.
In this lab you will go from start to finish in getting your artifacts ready for publication of your container offer. You'll start by getting containers running locally and move to updating deployment files in preparation for publishing the entire solution to your ACR.
- Getting started
- Prepare Solution Images
- Run Containers Locally
- Push Images to ACR
- Helm Chart
- Update Deployments File
- Update the UI Definition File
- Update the ARM Template
- Update Package Manifest File
⚠️ You must have completed all prerequisites before continuing with this lab.
If Admin user is not enabled on your ACR, select Settings > Access keys in the left menu and enable it in the Azure portal as shown in the following image.
Capture the following values as you will need them throughout the remainder of the labs.
- Login server
- Username
- Password
This section is extremely important for working easily through the labs.
You will be working with one solution throughout all the labs. To make it easier to work with the solution, make a copy of the begin folder and move it to a location of your choice outside the Git repository folder that holds these labs.
- Copy the folder
<path to Git repo>/docs/container/lab1-prepare-container-deployment/begin
. - Paste this folder to a location of your choice.
c:\projects\begin
for example. - Rename the
begin
folder tocontainer-labs
. From now on, the labs will refer tocontainer-labs
when referring to your working folder.
In this section, you will create a DockerFile
for solution Azure ToDo
and publish the image to your ACR created in the prerequisites exercise. The sample solution is a simple to-do application that is already completed for you. The intent of this lab is not to have you write code, but to prepare your container offer artifacts.
-
Using your text editor, open the following Dockerfile.
container-labs/code/Dockerfile
-
Add the following code to the Dockerfile.
FROM node:18.12-alpine3.17 # Create app directory WORKDIR /usr/src/app RUN apk update # Copy solution package file COPY package*.json ./ # Install app dependencies RUN npm install RUN npm audit fix --force # Copy all files COPY . . # Add command to expose web app port EXPOSE 3000 # Command to start the application CMD [ "npm", "start" ]
-
Ensure Docker Desktop is running.
-
Run the following commands to build the image.
cd container-labs\code docker build -t <ACR Login Server Name>/todojs:v1 .
-
The solution uses MongoDB for storage. You're going to run MongoDB in another container.
Run the following commands to pull mongodb image locally and tag it.
docker pull bitnami/mongodb:latest docker tag bitnami/mongodb:latest <ACR Server name>/mongo:latest
-
If you enter the following command, you should see the two local images prefixed with your ACR name.
docker images
In this section we will run the solution locally with Docker.
-
Create a user-defined bridge network.
docker network create todo-net
-
Start by running MongoDB locally. Specify an
admin username
andpassword
of your choice for the mongoDB instance.docker run -d --net todo-net -e MONGO_INITDB_ROOT_USERNAME:<admin username> -e MONGO_INITDB_ROOT_PASSWORD:<password> -p 27017:27017 --name mongotodo bitnami/mongodb:latest
-
Start the main AzureTodo web application container.
docker run -d --net todo-net -p 3000:3000 -e ENVIRONMENT:development -e DATABASE_NAME:azure-todo-app -e DATABASE_URL=mongodb://mongotodo:27017 <ACR Login Server Name>/todojs:v1
-
Verify the containers are running correctly. Run the following command to see the two containers.
docker ps
-
Browse the web application by going to http://localhost:3000
-
Try to create and delete tasks to make sure site is running properly
In this section we will publish or push the solution images to the ACR you created in prerequisites.
-
Open a terminal window (WSL2 on Windows) and run the following commands to login into ACR server.
docker login <ACR Login Server Name> -u <ACR admin> -p <ACR password>
-
Push the
todo.js
image to your ACR.docker push <ACR Server name>/todojs:v1
-
Push the mongo image to your ACR.
docker push <ACR Server name>/mongo:latest
You can now see the images in your ACR.
-
In the Azure portal browse to your ACR.
-
In the left menu, select Services > Repositories.
-
You should see an a view like the following image.
In this section will explore the Helm Chart directory AzureTodo
.
About Helm
Helm is the package manager for Kubernetes. In other words, it is used to help you manage Kubernetes applications. Helm is the Kubernetes equivalent of
yum
orapt
. Helm deploys charts, which you can think of as a packaged application.
-
Open
container-labs\container-package\AzureTodo\values.yaml
in your text editor. -
Update the two registry lines with your ACR server name. For example:
registry: myacr.azureacr.io
-
Locate the value of the
todojs:v1
digest in the Azure portal and copy it.Image Digest
The image digest is the hash of the image index or image manifest JSON document.
Inspect the images you pushed to the ACR earlier. Open each repository in the ACR and see that individual versions of an image will have their own Digest value. Copy the digest value from the Azure portal.
-
Paste the
todojs:v1
digest value onto line 7. -
Update line 11 with
mongo:latest
image digest. -
Under section
MongoDB Admin
Add the following key/value pairs.mongoDBAdmin: <enter admin name> mongoDBPassword: <enter password>
-
Update the last line with namespace from your choice i.e contoso
About deployments.yaml
This file defines the configuration for a Kubernetes deployment. Settings may be defined that specify details of creating pods, minimum pods in a cluster, scalability, and other settings.
-
Open the following file in your editor.
container-labs\container-package\AzureTodo\templates\deployments.yaml
-
Uncomment line 25 and add the following.
value: {{ .Values.mongoDBPassword }}
-
Uncomment line 27 and add the following.
value: {{ .Values.mongoDBAdmin }}
About
createUIDefinition.json
The createUIDefinition.json file is used to customize the customer's interface during the installation process. It allows for various controls to be shown on the screen to collect values needed to install the application.
-
Open the following file in your editor.
container-labs\container-package\createUIDefinition.json
-
Add the following JSON to the
elements
section.This JSON introduces two controls that will collect the admin username and password for the MongoDB container that will be created upon installation.
{ "name": "mongoDBAdmin", "type": "Microsoft.Common.TextBox", "label": "mongoDBAdmin", "toolTip": "Admin name for MongoDB", "defaultValue": "", "constraints": { "required": true, "regex": "^[a-z0-9A-Z]{4,20}$", "validationMessage": "Between 4 and 20 alphanumeric characters" } }, { "name": "mongoDBPassword", "type": "Microsoft.Common.PasswordBox", "label": { "password": "No special characters, between 12 and 30 characters long", "confirmPassword": "Passwords must match" }, "constraints": { "required": true, "regex": "^[a-z0-9A-Z]{12,30}$", "validationMessage": "Must be between 12 and 30 alphanumeric characters long." }, "visible": true, "options": { "hideConfirmation": false }, "toolTip": "Password for MongoDB admin" },
-
Go to the
outputs
section at the bottom of the file and and add the following JSON."app-mongoDBAdmin" : "[steps('ExtensionConfiguration').mongoDBAdmin]", "app-mongoDBPassword" : "[steps('ExtensionConfiguration').mongoDBPassword]",
This JSON adds the values collected by the new UI controls to the output of the
createUIDefinition.json
file. These values are passed along to the ARM template that creates the resources being deployed.
About the main deployment file
The
mainTemplate.json
is an ARM template, used for deploying resources into Azure. This file receives the output ofcreateUIDefinition.json
as input parameters.
-
Open the following file in your editor.
container-labs\container-package\mainTemplate.json
-
add the following parameters to the parameters section.
"app-mongoDBAdmin": { "type": "string", "metadata": { "description": "Enter Mongo DB admin username" } }, "app-mongoDBPassword": { "type": "secureString", "metadata": { "description": "Enter Mongo DB password" } }
Find the resource type Microsoft.KubernetesConfiguration/extensions
. This is a Kubernetes cluster extension that builds on top of Helm to produce an ARM-driven deployment experience.
About manifest.yaml
The manifest file brings together all elements of the deployment package by pointing at the other files you've been updating.
In this section you will update the manifest.yaml file.
-
Open the following file in your editor.
container-labs\container-package\manifest.yaml
-
Go to
registryServer:
and add your ACR server name. -
Go to
namespace
and the same namespace you entered under Helmvalues.yaml
file.
Congratulations! You have now finished this lab and your deployment files are ready for next steps.