Reputation: 81
In the startup phase of a Vue3 spa app container (production stage) to an Azure app service for containers - pulled from an Azure container registry - I'm experiencing the following error:
docker container could not be started
The pull(download), extract and build phase of the image are run correctly. The Docker custom image as below:
# Dockerfile
FROM node:16.17-bullseye-slim as base
LABEL name=client-base
WORKDIR /home/site/wwwroot
COPY package*.json ./
RUN npm install -g [email protected] \
&& npm config set fetch-retries 5 \
&& npm config set fetch-retry-factor 2 \
&& npm config set fetch-retry-mintimeout 10000 \
&& npm config set fetch-retry-maxtimeout 60000 \
&& npm config set fund false \
&& npm ci --production \
&& npm cache clean --force
COPY . .
EXPOSE 8080
# Development stage
FROM base as development
LABEL name=client-development
RUN npm i && npm cache clean --force
CMD ["npm", "run", "dev"]
# Production stage
FROM base as production
LABEL name=client-production
COPY --from=base /home/site/wwwroot/server.sh /usr/local/bin/server.sh
RUN chmod +x /usr/local/bin/server.sh
The web app has the following Startup file or command value:
server.sh
And the file is the following:
# Script used by Azure to start the application a SPA (Single Page Application) using PM2.
# It first stops all running PM2 processes, kills PM2, removes the .pm2 directory, and then starts the application.
# The script is run as root by Azure
set -e
# Define the function to start the application with pm2
start_application() {
echo "Removing .pm2 directory..."
rm -rf ~/.pm2
if [ $? -eq 0 ]; then
echo "Successfully removed .pm2 directory."
else
echo "Failed to remove .pm2 directory."
exit 1
fi
echo "Starting pm2, the process manager..."
pm2 serve public --no-daemon -i max --spa --name client
pm2 save
if [ $? -eq 0 ]; then
echo "Application started successfully."
exit 0
else
echo "Failed to start application."
exit 1
fi
}
# Check if pm2 is available
if command -v pm2 >/dev/null 2>&1; then
echo "pm2 is available"
start_application
else
echo "pm2 is not available"
# Save the location where pm2 will be installed in a variable
PM2_INSTALL_LOCATION=$(npm config get prefix)/bin
echo "Installing pm2 ..."
npm install -g pm2 --legacy-peer-depsq
# Check if the location where pm2 is installed is in the PATH
if [[ ":$PATH:" != *":$PM2_INSTALL_LOCATION:"* ]]; then
echo "Adding pm2 to the PATH ..."
export PATH=$PATH:$PM2_INSTALL_LOCATION
fi
# Check if pm2 is available after installing it and adding its location to the PATH
if command -v pm2 >/dev/null 2>&1; then
echo "pm2 is now available"
start_application
else
echo "pm2 is still not available"
fi
fi
exec "$@"
The app service container log:
Status: Image is up to date for stovsp.azurecr.io/client:1
2023-07-26T11:50:19.303Z INFO - Pull Image successful, Time taken: 0 Minutes and 0 Seconds
2023-07-26T11:50:19.423Z INFO - Starting container for site
2023-07-26T11:50:19.424Z INFO - docker run -d --expose=8080 --name stovsp_1_19575aa0 -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_USE_DIAGNOSTIC_SERVER=False -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=true -e WEBSITES_PORT=8080 -e WEBSITE_SITE_NAME=stovsp -e PORT=8080 -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=stovsp.azurewebsites.net -e WEBSITE_INSTANCE_ID=ec75ffac14f5a3cf139396184c4a565cf164999cdee10579f254283ef7d9d529 -e HTTP_LOGGING_ENABLED=1 -e NODE_OPTIONS=--require /agents/nodejs/build/src/Loader.js -e JAVA_TOOL_OPTIONS=-javaagent:/agents/java/applicationinsights-agent-codeless.jar -e ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=Microsoft.ApplicationInsights.StartupBootstrapper -e DOTNET_STARTUP_HOOKS=/agents/core/StartupHook/Microsoft.ApplicationInsights.StartupHook.dll stovsp.azurecr.io/client:1 server.sh
2023-07-26T11:50:23.568Z INFO - Initiating warmup request to container stovsp_1_19575aa0_msiProxy for site stovsp
2023-07-26T11:50:23.616Z INFO - Container stovsp_1_19575aa0_msiProxy for site stovsp initialized successfully and is ready to serve requests.
2023-07-26T11:50:23.617Z INFO - Initiating warmup request to container stovsp_1_19575aa0 for site stovsp
2023-07-26T11:50:41.286Z INFO - Waiting for response to warmup request for container stovsp_1_19575aa0. Elapsed time = 17.7182047 sec
2023-07-26T11:50:57.233Z INFO - Waiting for response to warmup request for container stovsp_1_19575aa0. Elapsed time = 33.6649324 sec
2023-07-26T11:51:17.365Z INFO - Waiting for response to warmup request for container stovsp_1_19575aa0. Elapsed time = 53.7965557 sec
2023-07-26T11:51:37.642Z INFO - Waiting for response to warmup request for container stovsp_1_19575aa0. Elapsed time = 74.0737583 sec
2023-07-26T11:50:22.016898311Z pm2 is not available
2023-07-26T11:50:39.105492065Z Installing pm2 ...
2023-07-26T11:51:24.016930170Z npm WARN deprecated [email protected]: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
2023-07-26T11:51:30.898502153Z
2023-07-26T11:51:30.898571953Z added 158 packages in 49s
2023-07-26T11:51:31.145521704Z pm2 is now available
2023-07-26T11:51:34.774649478Z
2023-07-26T11:51:34.774684378Z -------------
2023-07-26T11:51:34.774698878Z
2023-07-26T11:51:34.774703378Z __/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
2023-07-26T11:51:34.774707678Z _\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
2023-07-26T11:51:34.774711878Z _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
2023-07-26T11:51:34.774716078Z _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
2023-07-26T11:51:34.774732578Z _\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
2023-07-26T11:51:34.774737078Z _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
2023-07-26T11:51:34.774740978Z _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
2023-07-26T11:51:34.774744878Z _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
2023-07-26T11:51:34.774748778Z _\///______________\///______________\///__\///////////////__
2023-07-26T11:51:34.774752578Z
2023-07-26T11:51:34.774766178Z
2023-07-26T11:51:34.774769678Z Runtime Edition
2023-07-26T11:51:34.774773079Z
2023-07-26T11:51:34.774787779Z PM2 is a Production Process Manager for Node.js applications
2023-07-26T11:51:34.774791579Z with a built-in Load Balancer.
2023-07-26T11:51:34.774795079Z
2023-07-26T11:51:34.774798279Z Start and Daemonize any application:
2023-07-26T11:51:34.774801779Z $ pm2 start app.js
2023-07-26T11:51:34.774805179Z
2023-07-26T11:51:34.774808479Z Load Balance 4 instances of api.js:
2023-07-26T11:51:34.774811879Z $ pm2 start api.js -i 4
2023-07-26T11:51:34.774815379Z
2023-07-26T11:51:34.774818579Z Monitor in production:
2023-07-26T11:51:34.774821979Z $ pm2 monitor
2023-07-26T11:51:34.774825379Z
2023-07-26T11:51:34.774828579Z Make pm2 auto-boot at server restart:
2023-07-26T11:51:34.774831979Z $ pm2 startup
2023-07-26T11:51:34.774850879Z
2023-07-26T11:51:34.774855079Z To go further checkout:
2023-07-26T11:51:34.774858479Z http://pm2.io/
2023-07-26T11:51:34.774861879Z
2023-07-26T11:51:
34.774865079Z
2023-07-26T11:51:34.774868479Z -------------
2023-07-26T11:51:34.774871879Z
2023-07-26T11:51:35.015998123Z [PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
2023-07-26T11:51:37.294359774Z [PM2] PM2 Successfully daemonized
2023-07-26T11:51:37.515960690Z [PM2][WARN] No process found
2023-07-26T11:51:37.516012090Z [PM2] [v] All Applications Stopped
2023-07-26T11:51:37.721347383Z [PM2] [v] PM2 Daemon Stopped
2023-07-26T11:51:37.757874335Z Successfully killed pm2 daemon.
2023-07-26T11:51:37.768581250Z Removing .pm2 directory...
2023-07-26T11:51:38.991522270Z Successfully removed .pm2 directory.
2023-07-26T11:51:38.991550570Z Starting pm2, the process manager...
2023-07-26T11:51:41.226231767Z
2023-07-26T11:51:41.226292568Z -------------
2023-07-26T11:51:41.226301268Z
2023-07-26T11:51:41.226304768Z __/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
2023-07-26T11:51:41.226308668Z _\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
2023-07-26T11:51:41.226312468Z _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
2023-07-26T11:51:41.226316268Z _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
2023-07-26T11:51:41.226320168Z _\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
2023-07-26T11:51:41.226323968Z _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
2023-07-26T11:51:41.226327368Z _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
2023-07-26T11:51:41.226330768Z _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
2023-07-26T11:51:41.226334268Z _\///______________\///______________\///__\///////////////__
2023-07-26T11:51:41.226337668Z
2023-07-26T11:51:41.226340668Z
2023-07-26T11:51:41.226343568Z Runtime Edition
2023-07-26T11:51:41.226346768Z
2023-07-26T11:51:41.226357768Z PM2 is a Production Process Manager for Node.js applications
2023-07-26T11:51:41.226361268Z with a built-in Load Balancer.
2023-07-26T11:51:41.226364368Z
2023-07-26T11:51:41.226367368Z Start and Daemonize any application:
2023-07-26T11:51:41.226370768Z $ pm2 start app.js
2023-07-26T11:51:41.226383168Z
2023-07-26T11:51:41.226386468Z Load Balance 4 instances of api.js:
2023-07-26T11:51:41.226389568Z $ pm2 start api.js -i 4
2023-07-26T11:51:41.226392768Z
2023-07-26T11:51:41.226395768Z Monitor in production:
2023-07-26T11:51:41.226398868Z $ pm2 monitor
2023-07-26T11:51:41.226401868Z
2023-07-26T11:51:41.226404968Z Make pm2 auto-boot at server restart:
2023-07-26T11:51:41.226408068Z $ pm2 startup
2023-07-26T11:51:41.226411168Z
2023-07-26T11:51:41.226414068Z To go further checkout:
2023-07-26T11:51:41.226417268Z http://pm2.io/
2023-07-26T11:51:41.226420368Z
2023-07-26T11:51:41.226423368Z
2023-07-26T11:51:41.226426368Z -------------
2023-07-26T11:51:41.226441468Z
2023-07-26T11:51:41.505946830Z [PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
2023-07-26T11:51:44.190448013Z [PM2] PM2 Successfully daemonized
2023-07-26T11:51:44.407480695Z [PM2] Starting /usr/local/lib/node_modules/pm2/lib/API/Serve.js in fork_mode (1 instance)
2023-07-26T11:51:44.505083122Z [PM2] Done.
2023-07-26T11:51:44.506903124Z [PM2] Serving /home/site/wwwroot/public on port 8080
2023-07-26T11:51:44.565083300Z ┌────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
2023-07-26T11:51:44.571926808Z │ id │ name │ namespace │ version │ mode │ pid
│ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
2023-07-26T11:51:44.571933908Z ├────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
2023-07-26T11:51:44.571939108Z │ 0 │ client │ default │ 5.3.0 │ fork │ N/A │ 0s │ 0 │ online │ 0% │ 0b │ root │ disabled │
2023-07-26T11:51:44.571943609Z └────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
2023-07-26T11:51:47.098045090Z [PM2] Saving current process list...
2023-07-26T11:51:47.135423334Z [PM2] Successfully saved in /root/.pm2/dump.pm2
2023-07-26T11:51:47.164087669Z Application started successfully.
2023-07-26T11:51:48.352Z ERROR - Container stovsp_1_19575aa0 for site stovsp has exited, failing site start
2023-07-26T11:51:48.533Z ERROR - Container stovsp_1_19575aa0 didn't respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.
2023-07-26T11:51:48.589Z INFO - Stopping site stovsp because it failed during startup.
Upvotes: 0
Views: 251
Reputation: 8195
I created one sample express app with pm2 module, Dockerized it and sent it to Azure Container registry like below:-
As you are using pm2 module, The pm2 module will monitor the app in the log terminal, The Pm2 is started and logged correctly. The Express app is unavailable because, The pm2 is initiated in the start up command. You need to add the start up command in your Dockerfile or in your Azure Web app Configuration General settings to run the Web app on Port 8080. Refer my SO thread answer on Pm2 the last image similar to the logs you shared.
Add the below command in your start up command to serve your vue js files in the browser correctly:-
pm2 serve public --no-daemon -i max --spa --name client
or
npm start
In your local environment, Check if the above command starts and loads your web app correctly and then add that command in
Before this make sure to build the app correctly with npm run build
command.
Cross check your docker build command, I have used below command to build my image and push it to Azure ACR:-
docker build -t expressapp .
sudo az login
sudo az account set --subscription "xxxxx"
sudo az acr login --name valleyacr54 --username valleyacr54 --password xxxxxxxJ+ACRDcsgO/
Also, Try to replace your Dockerfile with the code below:-
FROM node:16.17-bullseye-slim as base
LABEL name=client-base
WORKDIR /home/site/wwwroot
# Install PM2
RUN npm install -g pm2
COPY package*.json ./
RUN npm install -g [email protected] \
&& npm config set fetch-retries 5 \
&& npm config set fetch-retry-factor 2 \
&& npm config set fetch-retry-mintimeout 10000 \
&& npm config set fetch-retry-maxtimeout 60000 \
&& npm config set fund false \
&& npm ci --production \
&& npm cache clean --force
COPY . .
EXPOSE 8080
Refer this Exercise and all the next steps to run your express app correctly as docker container from ACR in Azure Web app.
Upvotes: 0