NodeJS 成功启动后退出容器

30byixjq  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(127)

在Vue 3 spa应用容器(生产阶段)到容器的Azure应用服务的启动阶段-从Azure容器注册表中提取-我遇到以下错误:
第一个月
映像的拉取(下载)、提取和构建阶段运行正常。Docker自定义镜像如下:

# Dockerfile
FROM node:16.17-bullseye-slim as base
LABEL name=client-base
WORKDIR /home/site/wwwroot
COPY package*.json ./
RUN npm install -g npm@9.8.1 \
    && 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

字符串
Web应用具有以下启动文件或命令值:
server.sh
文件如下:

# 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 "$@"


应用服务容器日志:

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 uuid@3.4.0: 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.

3zwjbxry

3zwjbxry1#

我创建了一个带有pm2模块的示例Express应用程序,将其Dockerized并将其发送到Azure容器注册表,如下所示:


的数据



当您使用pm2模块时,pm2模块将在日志终端中监视应用程序,Pm2已正确启动并记录。Express应用程序不可用,因为pm2在启动命令中启动。您需要在Dockerfile或Azure Web应用配置常规设置中添加启动命令,以便在端口8080上运行Web应用。请参考我在Pm2上的SO线程答案,最后一个图像与您共享的日志类似。
在启动命令中添加以下命令,以在浏览器中正确地提供vue js文件:

pm2 serve public --no-daemon -i max --spa --name client

字符串
或者是

npm start


在您的本地环境中,检查上述命令是否启动并正确加载您的Web应用程序,然后将该命令添加到在此之前,请确保使用npm run build命令正确构建应用程序。
交叉检查你的docker build命令,我已经使用下面的命令来构建我的镜像并将其推送到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/


另外,尝试用下面的代码替换你的Dockerfile:

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 npm@9.8.1 \
    && 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


请参阅此Exercise和所有后续步骤,以在Azure Web应用中从ACR正确地将Express应用作为Docker容器运行。

相关问题