【问题标题】:Docker container fails to initalize from docker-composeDocker 容器无法从 docker-compose 初始化
【发布时间】:2021-12-27 04:58:32
【问题描述】:

我是 Docker 新手,正在学习 Docker Compose,使用 CentOS 8 作为主机系统。所有这些文件都在同一个目录中,并以 0755 模式标记为可执行文件。

我的主要应用在webapp.py:

import time

import redis
from flask import Flask

app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)

def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)

@app.route('/')
def hello():
    count = get_hit_count()
    return 'Hello World! I have been seen {} times.\n'.format(count)

有一个匹配的requirements.txt文件:

flask
redis

Dockerfile:

FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "webapp.py"]

还有一个docker-compose.yml 文件:

version: "3"
services:
  web:
    build: .
    ports:
      - "5000:5000"
    stdin_open: true
    tty: true

  redis:
    image: "redis:alpine"

但是,当我运行 docker-compose up 时,它会因退出网络应用而失败。编写输出:

[root@localhost webappl]# docker-compose up
Starting webappl_redis_1 ... done
Starting webappl_web_1   ... done
Attaching to webappl_web_1, webappl_redis_1
redis_1  | 1:C 27 Dec 2021 09:03:35.861 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 27 Dec 2021 09:03:35.861 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 27 Dec 2021 09:03:35.861 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1  | 1:M 27 Dec 2021 09:03:35.863 * monotonic clock: POSIX clock_gettime
redis_1  | 1:M 27 Dec 2021 09:03:35.864 * Running mode=standalone, port=6379.
redis_1  | 1:M 27 Dec 2021 09:03:35.864 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1  | 1:M 27 Dec 2021 09:03:35.864 # Server initialized
redis_1  | 1:M 27 Dec 2021 09:03:35.864 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1  | 1:M 27 Dec 2021 09:03:35.865 * Loading RDB produced by version 6.2.6
redis_1  | 1:M 27 Dec 2021 09:03:35.865 * RDB age 4147 seconds
redis_1  | 1:M 27 Dec 2021 09:03:35.865 * RDB memory usage when created 0.77 Mb
redis_1  | 1:M 27 Dec 2021 09:03:35.865 # Done loading RDB, keys loaded: 0, keys expired: 0.
redis_1  | 1:M 27 Dec 2021 09:03:35.865 * DB loaded from disk: 0.000 seconds
redis_1  | 1:M 27 Dec 2021 09:03:35.865 * Ready to accept connections
webappl_web_1 exited with code 0

为什么会退出?

【问题讨论】:

  • 只是想补充一下,每当我按照docs.docker.com/compose/gettingstarted的步骤进行操作时,它都能完美运行。
  • 你在哪里暴露你在 docker-compose 中使用的 5000 端口?你的 Webapp.py 不应该打开那个端口吗?
  • Docker 和 Python 通常都使用纯文本文件,而不是图像;我无法使用您在此处附加的 PNG 文件构建 Docker 图像。您可以编辑问题并将所有图像替换为应用程序源和输出的实际文本吗?
  • 如果您需要帮助,请停止发布图片
  • @silentsudo,看起来必须像 docker 本身一样学习使用 StackOverflow,哈哈。虽然删除了图像。

标签: docker docker-compose dockerfile


【解决方案1】:

您可以尝试在您的网络中添加以下内容:在端口之后:?如果 stdin_open 不起作用,请尝试 stdin

stdin_open: true
tty: true

【讨论】:

  • webappl_web_1 容器仍然存在。
  • 您需要使用“docker-compose down”停止现有的 docker-compose 容器
猜你喜欢
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 2020-01-28
  • 2019-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多