Installing Docker
Download Docker Desktop:
- Visit the Docker Desktop download page and download the appropriate version for your operating system (Windows or macOS).
Start Docker Desktop:
- Launch Docker Desktop and ensure it is running. You should see the Docker icon in your system tray (Windows) or menu bar (macOS).
Build Docker image using dockerfile
If you want to create a custom Docker image, you can use a Dockerfile
. A Dockerfile
is a text file that contains instructions for building an image.
Create a Dockerfile:
Create a new directory and inside it, create a file named
Dockerfile
which should be stored in Frontend root directory with the following content:# Use an official Ubuntu as a parent image FROM node:16.15.0 # Set the working directory in the container WORKDIR /frontend # Copy the any file starting with package in json extension from current directory contents into the container at /frontend COPY package*.json ./ # Install any needed packages specified in package.json RUN npm install # Copy the all current directory contents into the container at /frontend COPY . ./ # Make port 3000 available to the world outside this container EXPOSE 3000 # Define environment variable ENV NAME World # Run the build RUN npm run build # Launch the container CMD [ "npm", "start" ]
This is a simple example that sets up an Nodejs container with 16.15.0 version, installs NPM and runs a React script.
Similarly, For Backend - We write the below
Dockerfile
stored in Backend root directory and Run the Backend as well:# Use an official Ubuntu as a parent image FROM node:16.15.0 # Set the working directory in the container WORKDIR /backend # Copy the any file starting with package in json extension from current directory contents into the container at /backend COPY package*.json ./ # Install any needed packages specified in package.json RUN npm install # Copy the all current directory contents into the container at /backend COPY . ./ # Make port 5001 available to the world outside this container EXPOSE 5001 # Run and launch the application CMD [ "node", "Server.js" ] # Define environment variable ENV NAME World
Build the Docker Image:
Navigate to the directory containing the
Dockerfile
and run the following command:docker build -t backend .
The
-t
flag tags the image with a name (backend or frontend
).
Run the Custom Image:
Now you can run a container from your custom image:
# Run the Frontend application docker run -d -p 3000:3000 frontend # Run the Backend application docker run -d -p 5001:5001 backend
Creating ACR and Pushing container images from Local to ACR
Open a terminal or command prompt where you have the Azure CLI installed.
Log in to your Azure account if you haven't already:
az login
Set the desired Azure subscription if you have multiple subscriptions:
az account set --subscription <your-subscription-id>
Create a resource group if you don't have one already. For example:
az group create --name myResourceGroup --location eastus
Create the Azure Container Registry:
az acr create --resource-group myResourceGroup --name myContainerRegistry --sku Basic #eg: az acr create --resource-group aks_poc1 --name backendpoc --sku Basic
Here’s a detailed explanation of the command:
az acr create
: This command is used to create a new Azure Container Registry.--resource-group myResourceGroup
: Specifies the resource group in which to create the registry. ReplacemyResourceGroup
with your resource group name.--name myContainerRegistry
: The name of the container registry. ReplacemyContainerRegistry
with your desired registry name. Note that the name must be globally unique within Azure.--sku Basic
: Specifies the SKU (pricing tier) for the registry. Options includeBasic
,Standard
, andPremium
.
Log in to Azure Container Registry
Log in to your Azure Container Registry (ACR):
az acr login --name backendpoc
Tag the Docker Image
Assuming you have already built a Docker image locally, you need to tag it with the fully qualified path to your ACR. Replace
your-image-name
with the name of your Docker image andtag
with the appropriate tag (e.g.,latest
):docker tag your-image-name backendpoc.azurecr.io/your-image-name:tag
For example, if your image name is
backend or frontend
and you are tagging it asv1
:docker tag backend backendpoc.azurecr.io/backend:v1 docker tag frontend backendpoc.azurecr.io/frontend:v1
Push the Docker Image
Push the tagged image to your ACR:
docker push backendpoc.azurecr.io/your-image-name:tag
Using the previous example:
docker push backendpoc.azurecr.io/backend:v1 docker push backendpoc.azurecr.io/frontend:v1
Verify the Image in ACR
You can verify that your image has been pushed to ACR by using the Azure CLI:
az acr repository list --name backendpoc --output table
This command lists all the repositories in your ACR. You can also view specific tags of an image:
az acr repository show-tags --name backendpoc --repository your-image-name --output table