docker 如何配置Azure Web应用程序容器优雅关闭

djmepvbi  于 2023-08-03  发布在  Docker
关注(0)|答案(1)|浏览(124)

我使用Azure Web应用程序作为Docker容器主机,并且我想配置Docker容器或Web应用程序来响应SIGTERM消息
我只找到了这个:
启动关闭时,容器主机会向您的容器发送SIGTERM消息。在容器中实现的代码可以响应此操作系统级消息以处理终止。
电子邮件:https://learn.microsoft.com/en-us/azure/container-apps/application-lifecycle-management#shutdown
但是我到底需要如何配置Web应用程序或容器?我不明白我试着用这样的东西作为entrypoint.sh

#!/bin/bash

# Function to gracefully stop the processes and clean up
cleanup() {
    echo "Received termination signal. Stopping processes gracefully..."

    # Stop the app (Python)
    echo "Stopping app..."
    kill $python_pid

    # Stop the Bot (Java)
    echo "Stopping Bot"
    kill $java_pid

    # Wait for the processes to be terminated
    wait $python_pid
    wait $java_pid

    echo "All processes have been stopped. Exiting..."
    exit 0
}

# Trap termination signal and call the cleanup function
trap 'cleanup' SIGTERM

# Start ML app
cd /app
echo "Starting app..."
gunicorn --log-level debug --access-logfile - --error-logfile - -b :5000 wsgi:app &
python_pid=$!

# Start Slack Bot
cd /bot
echo "Starting Bot."
java -jar hrbs.jar &
java_pid=$!

# Wait for any of the processes to finish
wait -n $python_pid $java_pid

# Cleanup on error exit (should not reach this point, as we expect termination signal to be sent)
cleanup

字符串

xxe27gdn

xxe27gdn1#

Azure Web App会自动处理SIGTERM信号,以正常关闭容器。

  • 配置entrypoint.sh是一个很好的做法,您的想法是正确的。在主应用程序文件的目录中设置这个entrypoint.sh文件。
  • 在容器chmod +x entrypoint.sh内设置执行权限
    路径目录:


的数据

Dockerfile:

# Use an appropriate base image for your application
FROM python:3.8

# Set the working directory in the container
WORKDIR /app

# Copy your application files to the container
COPY app/ /app
COPY entrypoint.sh /app

# Install dependencies
RUN pip install -r /app/requirements.txt

# Grant execute permissions to entrypoint.sh
RUN chmod +x /app/entrypoint.sh

# Expose the port your application listens on
EXPOSE 5000

# Set the entrypoint to run your entrypoint.sh script
ENTRYPOINT ["/entrypoint.sh"]

字符串

  • 如果您根据需要向应用程序添加任何包,请配置**requirements.txt**。

一旦你的应用程序准备好转换成图像,并发送到容器如下。



然后,在portal中创建一个webapp,将publish选项配置为Docker容器,如下所示。



一旦部署完成。转到Web App的“设置”部分,选择“容器设置”,并确保启用“正常关机”。检查在docker desktop中创建的容器并运行它。

相关问题