简体   繁体   English

Docker 容器随机关闭

[英]Docker containers randomly shutting down

So I've had this system where I had 3 docker containers running at the same time to host my APIs, one for traefik, and the other two for different APIs (written in python using FastAPI).所以我有这个系统,我有 3 个 docker 容器同时运行来托管我的 API,一个用于 traefik,另外两个用于不同的 API(使用 FastAPI 以 python 编写)。 It was working fine for a while, however now all the docker containers seem to be randomly shutting down after a couple hours at the same.它工作了一段时间,但现在所有docker 容器似乎在几个小时后随机关闭。

This is the error message all of them output:这是所有错误消息output:

Exception in thread Thread-5:
Traceback (most recent call last):
  File "urllib3/connectionpool.py", line 677, in urlopen
  File "urllib3/connectionpool.py", line 392, in _make_request
  File "http/client.py", line 1277, in request
  File "http/client.py", line 1323, in _send_request
  File "http/client.py", line 1272, in endheaders
  File "http/client.py", line 1032, in _send_output
  File "http/client.py", line 972, in send
  File "docker/transport/unixconn.py", line 43, in connect
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "requests/adapters.py", line 449, in send
  File "urllib3/connectionpool.py", line 727, in urlopen
  File "urllib3/util/retry.py", line 410, in increment
  File "urllib3/packages/six.py", line 734, in reraise
  File "urllib3/connectionpool.py", line 677, in urlopen
  File "urllib3/connectionpool.py", line 392, in _make_request
  File "http/client.py", line 1277, in request
  File "http/client.py", line 1323, in _send_request
  File "http/client.py", line 1272, in endheaders
  File "http/client.py", line 1032, in _send_output
  File "http/client.py", line 972, in send
  File "docker/transport/unixconn.py", line 43, in connect
urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "threading.py", line 926, in _bootstrap_inner
  File "threading.py", line 870, in run
  File "compose/cli/log_printer.py", line 168, in tail_container_logs
  File "compose/cli/log_printer.py", line 185, in wait_on_exit
  File "compose/container.py", line 268, in wait
  File "docker/utils/decorators.py", line 19, in wrapped
  File "docker/api/container.py", line 1305, in wait
  File "docker/utils/decorators.py", line 46, in inner
  File "docker/api/client.py", line 233, in _post
  File "requests/sessions.py", line 578, in post
  File "requests/sessions.py", line 530, in request
  File "requests/sessions.py", line 643, in send
  File "requests/adapters.py", line 498, in send
requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

Here is my docker-compose for my first API (call it "Hierarchy"):这是我的第一个 API(称为“层次结构”)的 docker-compose:

services:

  backend:
    build: ./
    restart: always
    labels:
      - traefik.enable=true

      - traefik.http.services.hierarchy_app.loadbalancer.server.port=80

      - traefik.http.routers.hierarchy-http.entrypoints=http
      - traefik.http.routers.hierarchy-http.rule=Host(`api.mydomain.me`)
      - traefik.docker.network=traefik-public

      - traefik.http.routers.hierarchy-https.entrypoints=https
      - traefik.http.routers.hierarchy-https.rule=Host(`api.mydomain.me`)
      - traefik.http.routers.hierarchy-https.tls=true

      - traefik.http.routers.hierarchy-https.tls.certresolver=le

      - traefik.http.middlewares.https-redirect.redirectscheme.scheme=https
      - traefik.http.middlewares.https-redirect.redirectscheme.permanent=true

      - traefik.http.routers.hierarchy-http.middlewares=https-redirect

    networks:
      - traefik-public

    volumes:
      - ${PWD}/hierarchy_app/commands.json:/hierarchy_app/commands.json:Z

networks:
  traefik-public:
    external: true

Here is my docker-compose for my second API (call it "Voicerooms"):这是我的第二个 API(称之为“语音室”)的 docker-compose:

services:

  backend:
    build: ./
    restart: always
    labels:
      - traefik.enable=true

      - traefik.http.services.voicerooms_app.loadbalancer.server.port=80

      - traefik.http.routers.voicerooms-http.entrypoints=http
      - traefik.http.routers.voicerooms-http.rule=Host(`api.mydomain2.app`)
      - traefik.docker.network=traefik-public

      - traefik.http.routers.voicerooms-https.entrypoints=https
      - traefik.http.routers.voicerooms-https.rule=Host(`api.mydomain2.app`)
      - traefik.http.routers.voicerooms-https.tls=true

      - traefik.http.routers.voicerooms-https.tls.certresolver=le

      - traefik.http.middlewares.https-redirect.redirectscheme.scheme=https
      - traefik.http.middlewares.https-redirect.redirectscheme.permanent=true

      - traefik.http.routers.voicerooms-https.middlewares=https-redirect
    networks:
      - traefik-public

    volumes:
      - ${PWD}/voicerooms_app/commands.json:/voicerooms_app/commands.json:Z

networks:
  traefik-public:
    external: true

Any help would be greatly appreciated, I've been trying to figure out this problem for a while:)任何帮助将不胜感激,我一直试图解决这个问题一段时间:)

Edit: After scrolling up a bit more, I found this in the error message:编辑:再向上滚动一点后,我在错误消息中发现了这一点:

backend_1  | [2022-01-10 02:04:51 +0000] [1] [INFO] Handling signal: term
backend_1  | [2022-01-10 02:04:51 +0000] [7] [INFO] Shutting down
backend_1  | [2022-01-10 02:04:51 +0000] [8] [INFO] Shutting down

I'm assuming my docker container is getting terminated for some reason, what's going on here?我假设我的 docker 容器由于某种原因被终止,这是怎么回事?

I also encountered the same error as you,My problem is that docker does not start.我也遇到了和你一样的错误,我的问题是docker不启动。

check docker status检查 docker 状态

 systemctl status docker

systemctl start docker

docker-compose up try again

systemctl enable docker

to start on boot.在启动时启动。

OR或者

If you're on a Mac, it may mean that Docker itself isn't running.如果您使用的是 Mac,则可能意味着 Docker 本身没有运行。 I had rebooted my Mac and Docker wasn't set to automatically launch at login.我已经重新启动了我的 Mac,并且 Docker 没有设置为在登录时自动启动。 You can set this as a Docker preference.您可以将其设置为 Docker 首选项。

OR you can solve this by running sudo systemctl start docker and then running sudo docker-compose up或者你可以通过运行 sudo systemctl start docker 然后运行 sudo docker-compose up 来解决这个问题

Seems your application is for some reason attempting to use a file that is not there, and it's not managing properly the Exception, making your application exit and your container stop.似乎您的应用程序出于某种原因试图使用不存在的文件,并且它没有正确管理异常,从而使您的应用程序退出并且您的容器停止。

Make sure that the send method you have in your application requests/adapters.py on line 498 is properly encapsulated into a Try...Except statement.确保您在应用程序requests/adapters.py中第 498 行的send方法被正确封装到Try...Except语句中。 Also, make sure to log the exception, this will avoid your application to crash, but will not fix the bug or underlying cause - you will have to investigate this separately.此外,请确保记录异常,这将避免您的应用程序崩溃,但不会修复错误或根本原因 - 您必须单独调查。

Your application dies for some reason and the containers finish.您的应用程序由于某种原因而死亡,并且容器完成。 How to debug this is a common question, I would start all containers in an interactive mode and check out what is happening:如何调试这是一个常见问题,我会以交互模式启动所有容器并检查发生了什么:

docker run --rm -p8080:80 -it <CONTAINER NAME>

Instead of -p8080:80 (exposing container's port 80 on host's 8080), add your networking setup... Also add any mount/binds/environment setting your containers need.而不是-p8080:80 (在主机的 8080 上公开容器的端口 80),添加您的网络设置...还添加您的容器需要的任何挂载/绑定/环境设置。

You can also start the container in bash and start your command manually:您也可以在bash中启动容器并手动启动您的命令:

docker run --rm -p8080:80 -it <CONTAINER NAME> /bin/bash

From now on, you have to find what is wrong with your app.从现在开始,您必须找出您的应用程序出了什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM