容器启动成功后退出

问题描述 投票:0回答:0

在 Vue3 spa 应用程序容器(生产阶段)到容器的 Azure 应用程序服务的启动阶段(从 Azure 容器注册表中提取),我遇到以下错误:

docker container could not be started

镜像的拉取(下载)、提取和构建阶段均正确运行。 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 [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

网络应用程序具有以下启动文件或命令值:

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

应用服务容器日志:

2023-07-25T19:48:02.427233654Z pm2 is not available
2023-07-25T19:48:07.103796363Z Installing pm2 ...
2023-07-25T19:48:34.342910932Z 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-25T19:48:39.626198756Z 
2023-07-25T19:48:39.626236956Z added 158 packages in 30s
2023-07-25T19:48:39.683626138Z pm2 is now available
2023-07-25T19:48:39.684479036Z Removing .pm2 directory...
2023-07-25T19:48:39.695750313Z Successfully removed .pm2 directory.
2023-07-25T19:48:39.702133699Z Starting pm2, the process manager...
2023-07-25T19:48:41.545997595Z 
2023-07-25T19:48:41.546046095Z                         -------------
2023-07-25T19:48:41.546051595Z 
2023-07-25T19:48:41.546054695Z __/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
2023-07-25T19:48:41.546058495Z  _\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
2023-07-25T19:48:41.546079795Z   _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
2023-07-25T19:48:41.546083695Z    _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
2023-07-25T19:48:41.546087595Z     _\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
2023-07-25T19:48:41.546091295Z      _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
2023-07-25T19:48:41.546094795Z       _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
2023-07-25T19:48:41.546098095Z        _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
2023-07-25T19:48:41.546101395Z         _\///______________\///______________\///__\///////////////__
2023-07-25T19:48:41.546104795Z 
2023-07-25T19:48:41.546107695Z 
2023-07-25T19:48:41.546110595Z                           Runtime Edition
2023-07-25T19:48:41.546130295Z 
2023-07-25T19:48:41.546133195Z         PM2 is a Production Process Manager for Node.js applications
2023-07-25T19:48:41.546136195Z                      with a built-in Load Balancer.
2023-07-25T19:48:41.546139195Z 
2023-07-25T19:48:41.546141995Z                 Start and Daemonize any application:
2023-07-25T19:48:41.546144995Z                 $ pm2 start app.js
2023-07-25T19:48:41.546147795Z 
2023-07-25T19:48:41.546151095Z                 Load Balance 4 instances of api.js:
2023-07-25T19:48:41.546153995Z                 $ pm2 start api.js -i 4
2023-07-25T19:48:41.546156995Z 
2023-07-25T19:48:41.546202094Z                 Monitor in production:
2023-07-25T19:48:41.546206094Z                 $ pm2 monitor
2023-07-25T19:48:41.546219994Z 
2023-07-25T19:48:41.546223594Z                 Make pm2 auto-boot at server restart:
2023-07-25T19:48:41.546226594Z                 $ pm2 startup
2023-07-25T19:48:41.546229494Z 
2023-07-25T19:48:41.546232194Z                 To go further checkout:
2023-07-25T19:48:41.546235194Z                 http://pm2.io/
2023-07-25T19:48:41.546238094Z 
2023-07-25T19:48:41.546240894Z 
2023-07-25T19:48:41.546266794Z                         -------------
2023-07-25T19:48:41.546269994Z 
2023-07-25T19:48:41.752979868Z [PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
2023-07-25T19:48:43.519657569Z [PM2] PM2 Successfully daemonized
2023-07-25T19:48:43.653856233Z [PM2] Starting /usr/local/lib/node_modules/pm2/lib/API/Serve.js in fork_mode (1 instance)
2023-07-25T19:48:43.764975355Z [PM2] Done.
2023-07-25T19:48:43.766992850Z [PM2] Serving /home/site/wwwroot/public on port 8080
2023-07-25T19:48:43.802713560Z ┌────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
2023-07-25T19:48:43.802736260Z │ id │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
2023-07-25T19:48:43.802741960Z ├────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
2023-07-25T19:48:43.802746560Z │ 0  │ client    │ default     │ 5.3.0   │ fork    │ N/A      │ 0s     │ 0    │ online    │ 0%       │ 0b       │ root     │ disabled │
2023-07-25T19:48:43.802750460Z └────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
2023-07-25T19:48:45.941561003Z [PM2] Saving current process list...
2023-07-25T19:48:45.960728955Z [PM2] Successfully saved in /root/.pm2/dump.pm2
2023-07-25T19:48:45.981740602Z Application started successfully.
node.js azure pm2 azure-web-app-for-containers
© www.soinside.com 2019 - 2024. All rights reserved.