3

我正在阅读 Ubuntu 17.10 上的官方 Docker CE(版本 18.03.1-ce,构建 9ee9f40)教程,但在运行服务时遇到了问题,如此所述。 该服务不会启动,但 Docker 不会告诉我原因。

问题描述

当我启动服务时,会发生以下情况:

$ docker stack deploy -c ./docker-compose.yml compose-example 
Creating network compose-example_webnet
Creating service compose-example_web
$ 

没有返回错误,退出状态为 0,没有任何异常。但是,当获取有关堆栈的信息时...

$ docker stack ps compose-example 
ID                  NAME                        IMAGE                 NODE                DESIRED STATE       CURRENT STATE                 ERROR                              PORTS
cc01b85bh1si        compose-example_web.1       jessetg/example:2.1   jesse-ubuntu        Ready               Rejected 3 seconds ago        "container ingress-sbox is alr…"   
z0hleui2rnai         \_ compose-example_web.1   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected 18 seconds ago       "container ingress-sbox is alr…"   
w47ajzunn7it         \_ compose-example_web.1   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected 43 seconds ago       "container ingress-sbox is alr…"   
yca0lx87oasq         \_ compose-example_web.1   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected 53 seconds ago       "container ingress-sbox is alr…"   
uvpvaperhcek         \_ compose-example_web.1   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected about a minute ago   "container ingress-sbox is alr…"   
qgfvmdrysirb        compose-example_web.2       jessetg/example:2.1   jesse-ubuntu        Ready               Rejected 3 seconds ago        "container ingress-sbox is alr…"   
zgek1ga179a9         \_ compose-example_web.2   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected 18 seconds ago       "container ingress-sbox is alr…"   
ybuzkik6rqqn         \_ compose-example_web.2   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected 48 seconds ago       "container ingress-sbox is alr…"   
yx71seu5cxzx         \_ compose-example_web.2   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected about a minute ago   "container ingress-sbox is alr…"   
yrdlrc5jwdyu         \_ compose-example_web.2   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected about a minute ago   "container ingress-sbox is alr…"   
yuoegbfvo190        compose-example_web.3       jessetg/example:2.1   jesse-ubuntu        Ready               Rejected 3 seconds ago        "container ingress-sbox is alr…"   
zqft052r2len         \_ compose-example_web.3   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected 28 seconds ago       "container ingress-sbox is alr…"   
zbpjg0yqrhon         \_ compose-example_web.3   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected 53 seconds ago       "container ingress-sbox is alr…"   
y4cj9qlrlvkn         \_ compose-example_web.3   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected 58 seconds ago       "container ingress-sbox is alr…"   
x3yljiu07mkq         \_ compose-example_web.3   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected about a minute ago   "container ingress-sbox is alr…"   
nibomw8zsx6y        compose-example_web.4       jessetg/example:2.1   jesse-ubuntu        Ready               Rejected 3 seconds ago        "container ingress-sbox is alr…"   
we2uwo0gaka8         \_ compose-example_web.4   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected 13 seconds ago       "container ingress-sbox is alr…"   
y5gs1uooes2b         \_ compose-example_web.4   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected 38 seconds ago       "container ingress-sbox is alr…"   
w1dozapp04pv         \_ compose-example_web.4   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected 48 seconds ago       "container ingress-sbox is alr…"   
wc9weak6gzfi         \_ compose-example_web.4   jessetg/example:2.1   jesse-ubuntu        Shutdown            Rejected about a minute ago   "container ingress-sbox is alr…"   
$

CURRENT STATE列完全充满了拒绝。截断的错误都说"container ingress-sbox is already present in sandbox ingress_sbox"。列出各个容器如下所示:

$ docker container ls -a
CONTAINER ID        IMAGE                 COMMAND             CREATED              STATUS              PORTS               NAMES
3c42933ddabe        jessetg/example:2.1   "python3 app.py"    About a minute ago   Created                                 compose-example_web.4.b6r18eot53o23afx2csl0dnqi
59f6073c3ebd        jessetg/example:2.1   "python3 app.py"    12 minutes ago       Created                                 compose-example_web.2.hi2m5am7dsf6rumecr69kxv3t
$ 

然而,单独运行容器(即不在集群或堆栈中)完全按预期运行。

相关文件

Dockerfile

# Use an official Python runtime as a parent image
FROM python:3.6-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip3 install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python3", "app.py"]

docker-compose.yml

version: "3"
services:
  web:
    image: jessetg/example:2.1
    deploy:
      replicas: 4
      resources:
        limits:
          cpus: "0.1"
          memory: 64M
      restart_policy:
        condition: on-failure
    ports:
      - "80:4001"
    networks:
      - webnet
networks:
  webnet:

app.py

#!/usr/bin/env python3

from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)


@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)
4

2 回答 2

1

当您在创建网络时遇到错误时,“容器 ingress-sbox 已经存在于沙盒 ingress_sbox 中”,值得检查您的 Linux 内核。

内核方面需要注意的事项包括:

  • 安装的模块包和运行的内核不匹配(如果运行的内核无法加载所需的模块,那么你会卡住)。
  • 正在运行的内核禁用了某些必需的功能。

在不匹配的情况下,重新启动通常会让您运行新安装的内核包,这意味着您实际上可以加载必要的模块。

如果您正在运行的内核完全禁用了该功能,您可能需要完全切换到不同的内核。

于 2018-05-08T22:10:01.133 回答
-2

docker中没有办法捕获异常,而是将异常记录在一个文件中,然后你在容器内导航以检查,直到它还活着。你可以使用

docker exec -it 容器 id bash

进去或使用

docker diff 容器 id

检查添加的文件或修改的文件,这将是您的日志文件,因为每次日志都会附加到您的日志文件中。这就是我们跟踪异常的方式,或者如果您遇到,请分享任何更好的方式。

于 2018-05-06T19:19:21.787 回答