nginx Docker容器在一周后自动关闭

tzxcd3kk  于 2023-06-28  发布在  Nginx
关注(0)|答案(1)|浏览(117)

我遇到了一个与Docker容器相关的问题,该容器运行一个由nginx和一堆html文件组成的多级Docker镜像。问题是,它在运行一周或两周后优雅地关闭了。它接收到的信号是:signal 3 (SIGQUIT) received, shutting down
正在运行的容器列表:

1a68ecd3b68f   angular-nginx-web    "nginx -g 'daemon of…"   14 hours ago   Up 14 hours   80/tcp, 0.0.0.0:49162->8002/tcp, :::49162->8002/tcp   web
600c7583e1cc   nginx:alpine                                                                 "/docker-entrypoint.…"   14 hours ago   Up 14 hours   0.0.0.0:8000->80/tcp, :::8000->80/tcp                 nginx
fd9c765aedc1   express-node-backend   "docker-entrypoint.s…"   14 hours ago   Up 14 hours   0.0.0.0:49161->8001/tcp, :::49161->8001/tcp           rest

正在正常关闭的容器名称为web
Docker-compose文件:

services:
  nginx:
    container_name: nginx
    image: nginx:alpine
    restart: always
    ports:
      - "8000:80"
    command: nginx -g "daemon off;"
    volumes:
      - ./nginx/production/conf.d/:/etc/nginx/conf.d/
    networks:
      - prod
  web:
    container_name: web
    image: angular-nginx-web
    restart: always
    volumes:
      - ./nginx/web:/etc/nginx/conf.d
      - type: bind 
        source: ./env/production/web_base_settings/settings.json
        target: /usr/share/nginx/html/assets/settings/settings.json
    ports:
      - "8002"
    networks:
      - prod

  rest:
    container_name: rest
    image: 
    restart: always
    env_file:
      - env/production/rest.env
    ports:
      - "8001"
    networks:
      - prod

angular-nginx-web镜像的Dockerfile:

# Stage 1

# Build target base #
#####################
FROM node:6-alpine AS base
WORKDIR /angular-nginx-web
COPY package*.json  ./
COPY .git/ ./.git/
RUN apk add --no-cache git && \
    npm install
COPY . /angular-nginx-web
RUN npm run build && \
    rm -rf /node_modules

# Stage 2

FROM nginx:alpine as server
WORKDIR /usr/share/nginx/html
RUN rm -rf ./*
COPY --from=base /angular-nginx-web/dist .
ENTRYPOINT ["nginx", "-g", "daemon off;"]

收到的日志:

<ip> - - [10/Mar/2022:17:23:57 +0000] "HEAD /assets/misc/connectcheck.txt HTTP/1.0" 200 0 "http://<ip>:8000/angular-web/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36" "10.80.240.47"
2022/03/11 02:12:38 [notice] 1#1: signal 3 (SIGQUIT) received, shutting down
2022/03/11 02:12:38 [notice] 9#9: exiting
2022/03/11 02:12:38 [notice] 7#7: gracefully shutting down
2022/03/11 02:12:38 [notice] 7#7: exiting
2022/03/11 02:12:38 [notice] 7#7: exit
2022/03/11 02:12:38 [notice] 8#8: gracefully shutting down
2022/03/11 02:12:38 [notice] 8#8: exiting
2022/03/11 02:12:38 [notice] 8#8: exit
2022/03/11 02:12:38 [notice] 1#1: signal 17 (SIGCHLD) received from 9
2022/03/11 02:12:38 [notice] 1#1: cache manager process 9 exited with code 0
2022/03/11 02:12:38 [notice] 1#1: signal 29 (SIGIO) received
2022/03/11 02:12:38 [notice] 1#1: signal 17 (SIGCHLD) received from 7
2022/03/11 02:12:38 [notice] 1#1: worker process 7 exited with code 0
2022/03/11 02:12:38 [notice] 1#1: signal 29 (SIGIO) received
2022/03/11 02:12:38 [notice] 1#1: signal 17 (SIGCHLD) received from 8
2022/03/11 02:12:38 [notice] 1#1: worker process 8 exited with code 0
2022/03/11 02:12:38 [notice] 1#1: exit

有人能帮帮忙吗。我对Docker相对较新,很难弄清楚这种优雅关闭的原因。

hi3rlvi2

hi3rlvi21#

你是否碰巧在你自己的用户运行的无根docker上下文下运行docker命令?
如果是这样的话,systemd可能是罪魁祸首。systemd在用户会话结束一定时间后自动终止用户启动的进程。

解决方案

启用延迟模式或将systemd配置为不终止用户进程,或同时启用这两种模式。

sudo loginctl enable-linger $USER

只是为了确保,编辑/etc/systemd/logind.conf并将以下行添加到末尾。

UserStopDelaySec=infinity
KillUserProcesses=no

重新启动您的计算机,使新配置生效。

sudo reboot

关于docs
KillUserProcesses=
接受布尔参数。配置用户注销时是否应终止用户的进程。如果为true,则与会话对应的作用域单元和该作用域内的所有进程将被终止。如果为false,则作用域被“放弃”,参见systemd.scope(5),并且进程不会被杀死。默认为“no”,但请参见下面的选项KillOnlyUsers=和KillExcludeUsers=。
当然,除非启用了延迟模式,请参见此处
enable-linger [USER...], disable-linger [USER...]
为一个或多个用户启用/禁用用户延迟。如果为特定用户启用,则会在 Boot 时为该用户生成一个用户管理器,并在注销后保留该管理器。这允许未登录的用户运行长时间运行的服务。将一个或多个用户名或数字UID作为参数。如果未指定参数,则为调用方会话的用户启用/禁用延迟。
wrote a blog about it与多一点的细节,如果你想检查出来。

相关问题