Docker 容器在一周后自动正常关闭

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

我遇到了与 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 比较陌生,发现很难找出这种正常关闭的原因。

node.js docker nginx docker-compose dockerfile
1个回答
2
投票

您是否有机会在您自己的用户运行的无根 docker 上下文下运行 docker 命令?

如果是这样的话,

systemd
可能就是罪魁祸首。
systemd
在用户会话结束一定时间后自动终止用户下启动的进程。

解决方案

启用延迟模式或配置

systemd
不终止用户进程,或两者兼而有之。

sudo loginctl enable-linger $USER

为了确定,请编辑

/etc/systemd/logind.conf
并添加以下内容 到最后一行。

UserStopDelaySec=infinity
KillUserProcesses=no

重新启动机器以使新配置生效。

sudo reboot

来自文档

KillUserProcesses=

采用布尔参数。配置用户注销时是否应终止用户的进程。如果为 true,则与会话对应的范围单元以及该范围内的所有进程都将终止。如果为 false,则范围被“放弃”,请参阅 systemd.scope(5),并且进程不会被终止。 默认为“no”,但请参阅下面的选项 KillOnlyUsers= 和 KillExcludeUsers=。

当然除非启用逗留模式,记录在此处

enable-linger [USER...], disable-linger [USER...]

为一个或多个用户启用/禁用用户延迟。如果为特定用户启用,则会在启动时为该用户生成用户管理器,并在注销后保留。这允许未登录的用户运行长时间运行的服务。采用一个或多个用户名或数字 UID 作为参数。如果未指定参数,则为调用者会话的用户启用/禁用延迟。

写了一篇关于它的博客,如果你想查看的话,还有更多细节。

© www.soinside.com 2019 - 2024. All rights reserved.