Here are the files for a basic express app I am trying to containerize and deploy on the Azure container registry.
index.js
const express = require('express')
const morgan = require('morgan')
const app = express()
const port = process.env.PORT || 80;
app.use(morgan('combined'))
app.get('/', (req, res) => {
res.json({
message: 'Welcome to Cloud Cast backend'
})
})
app.listen(port, () => {
console.log(`Server is running on port ${port}`)
})
package.json
{
"name": "cloud-cast-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.19.2",
"morgan": "^1.10.0"
}
}
Dockerfile
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 80
CMD ["node", "index.js"]
Commands used
- Create a container registry and push express-image to it
az acr create --name expressreg --resource-group cloud-cast-rg --sku Basic
az acr login --name expressreg
docker build -t express-image:v1 .
docker tag express-image:v1
docker push expressreg.azurecr.io/express-image:v1
az acr update --name expressreg --admin-enabled true
- Create a container instance from the repository pushed to the container registry.
az container create \
--resource-group cloud-cast-rg \
--name express-app \
--image expressreg.azurecr.io/express-image:v1 \
--cpu 1 \
--memory 1.5 \
--registry-login-server expressreg.azurecr.io \
--registry-username expressreg \
--registry-password <your-registry-password> \
--ports 80
(I entered the respective password, concealing it for the sake of privacy)
Problem:
Seeing the message:
One or more of the containers in 'express-app' are in a 'Waiting' state and may not be running. Click here to view container statuses.
When I click it I am on the container tab and can observe that the container instance is terminating and restarting perpetually. The container is mostly in the waiting state.
Tried to access logs
az container logs --resource-group cloud-cast-rg --name express-app
Output: None
Tried to test the image locally:
docker run -p 80:80 expressreg.azurecr.io/express-image:v1
On 127.0.0.1:80 I get
{"message":"Welcome to Cloud Cast backend"}
Further debugging:
az container attach --name express-app --resource-group cloud-cast-rg
Output:
Container 'express-app' is in state 'Waiting'...
(count: 1) (last timestamp: 2024-07-15 22:58:08+00:00) pulling image "expressreg.azurecr.io/express-image:v1"
(count: 1) (last timestamp: 2024-07-15 22:58:28+00:00) Successfully pulled image "expressreg.azurecr.io/express-image:v1"
(count: 8) (last timestamp: 2024-07-15 23:07:20+00:00) Started container
(count: 8) (last timestamp: 2024-07-15 23:07:27+00:00) Killing container with id <container-id>
Container crash logs
az container show --name express-app --resource-group cloud-cast-rg
Output:
{
"confidentialComputeProperties": null,
"containers": [
{
"command": null,
"environmentVariables": [],
"image": "expressreg.azurecr.io/express-image:v1",
"instanceView": {
"currentState": {
"detailStatus": "CrashLoopBackOff: Back-off restarting failed",
"exitCode": null,
"finishTime": null,
"startTime": null,
"state": "Waiting"
},
"events": [
{
"count": 1,
"firstTimestamp": "2024-07-15T22:58:08+00:00",
"lastTimestamp": "2024-07-15T22:58:08+00:00",
"message": "pulling image \"expressreg.azurecr.io/express-image:v1\"",
"name": "Pulling",
"type": "Normal"
},
{
"count": 1,
"firstTimestamp": "2024-07-15T22:58:28+00:00",
"lastTimestamp": "2024-07-15T22:58:28+00:00",
"message": "Successfully pulled image \"expressreg.azurecr.io/express-image:v1\"",
"name": "Pulled",
"type": "Normal"
},
{
"count": 8,
"firstTimestamp": "2024-07-15T22:58:35+00:00",
"lastTimestamp": "2024-07-15T23:07:20+00:00",
"message": "Started container",
"name": "Started",
"type": "Normal"
},
{
"count": 8,
"firstTimestamp": "2024-07-15T22:58:41+00:00",
"lastTimestamp": "2024-07-15T23:07:27+00:00",
"message": "Killing container with id 910295e546c119c46ef36b1861bd0d9e4e608567d45b2f8092d317a7c15e5d6f.",
"name": "Killing",
"type": "Normal"
}
],
"previousState": {
"detailStatus": "Error",
"exitCode": 1,
"finishTime": "2024-07-15T23:07:27.938000+00:00",
"startTime": "2024-07-15T23:07:20.953000+00:00",
"state": "Terminated"
},
"restartCount": 7
},
"livenessProbe": null,
"name": "express-app",
"ports": [],
"readinessProbe": null,
"resources": {
"limits": null,
"requests": {
"cpu": 1.0,
"gpu": null,
"memoryInGb": 1.5
}
},
"securityContext": null,
"volumeMounts": null
}
],
"diagnostics": null,
"dnsConfig": null,
"encryptionProperties": null,
"extensions": null,
"id": "/subscriptions/cdab2bc2-370f-4d93-ba21-c793d78f9ecf/resourceGroups/cloud-cast-rg/providers/Microsoft.ContainerInstance/containerGroups/express-app",
"identity": null,
"imageRegistryCredentials": [
{
"identity": null,
"identityUrl": null,
"isDelegatedIdentity": false,
"password": null,
"server": "expressreg.azurecr.io",
"username": "expressreg"
}
],
"initContainers": [],
"instanceView": {
"events": [],
"state": "Running"
},
"ipAddress": null,
"location": "eastus",
"name": "express-app",
"osType": "Linux",
"priority": null,
"provisioningState": "Succeeded",
"resourceGroup": "cloud-cast-rg",
"restartPolicy": "Always",
"sku": "Standard",
"subnetIds": null,
"tags": {},
"type": "Microsoft.ContainerInstance/containerGroups",
"volumes": null,
"zones": null
}
Pardon the big gibberish o/p. The bottom line is the 'CrashLoopBackOff: Back-off restarting failed' message.
I have spent countless hours trying to get to the bottom of this. Someone please help.