DEV Community

Cover image for Containerize your NodeJS App using Docker and deploy it to Azure Container Apps
Jp Lazaro
Jp Lazaro

Posted on

Containerize your NodeJS App using Docker and deploy it to Azure Container Apps

In this blog you will be able to learn the following

  • Containerize your node app using Dockerfile
  • Build and create your own image using docker
  • Setup your Azure Services like resource group, Azure Container Registry, Azure Container Env and Azure Container Apps
  • Setup your Azure Container Registry and Setup Access Keys
  • Deploy your image to Azure Container Registry
  • Create your Azure container app via Azure CLI

Create a folder and open it in VS code
Image description

Open terminal and initialize node project
Image description

It will generate package.json
Image description

Let’s install express.js
Image description

Image description

Let’s create express API

Image description

Configure package.json to run the app
Image description

Lets run the app

Image description

Image description

Create dockerfile

Image description

Image description

Create docker ignore

Image description

Image description

Next step is installing docker desktop in our local computer.
Once installed, let's open it, and explore

These are the containers available in your computer. As you can see there are no containers yet.
Image description

These are the images available in your computer. As you can see there are no images yet.
Image description

These are the image builds available in your computer. As you can see there are no image builds yet.
Image description

Now let's build our new image using docker command :

docker build -t azdockernode .
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Check the docker desktop > Builds : you will see it is now building your image

Image description

Once completed it will be available in Images list in your docker desktop
Image description

Let's install Azure CLI
Download the installer from the official website of microsoft
Image description

Login our azure account

az login
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Create resource group

 az group create  --name myrg1 --location eastus
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Create Azure Container Registry

az acr create  --resource-group myrg1  --name myrg1acr1 --sku Basic  --location eastus 
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

You can now see your ACR in your resource group
Image description

Now, let's setup the access key so we can publish our local image later on.

Image description

Click the admin button
Image description

it will generate username and password, later we will use this credentials to push our image to this azure container registry

Image description

Let's login our ACR in terminal
Image description

We need to tag our local image to Azure container registry

docker tag azdockernode myrg1acr1.azurecr.io/azdockernode
Enter fullscreen mode Exit fullscreen mode

Image description

We need to tag our local image to Azure container registry

docker tag azdockernode myrg1acr1.azurecr.io/azdockernode
Enter fullscreen mode Exit fullscreen mode

Now let's push our image to the Azure Container Registry

docker push  myrg1acr1.azurecr.io/azdockernode
Enter fullscreen mode Exit fullscreen mode

Image description

To verify the completion of push. You can go to azure portal > myrg1acr1 (your Azure container registry)> repository

Image description

Next step is to create Azure Container Apps Environment

az containerapp env create   
--name mycontainerenv1 
--resource-group myrg1
--location eastus 
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Finally, lets create the container app

az containerapp create 
--name mycontainerapp1 
--resource-group myrg1 
--image myrg1acr1.azurecr.io/azdockernode 
--environment mycontainerenv1 
--cpu 1 
--memory 2Gi 
--target-port 3000 
--ingress external 
--registry-server myrg1acr1.azurecr.io
--registry-username myrg1acr1 
--registry-password <<PASSWORD FROM Azure Container Registry Admin>>
Enter fullscreen mode Exit fullscreen mode

Image description

once successful you will see some json like this
Image description

We can verify again in the Azure Portal if it's created

Image description

Image description

Image description

Finally, you made it!
Thank you for reading!

Top comments (0)