docker compose 不会创建容器,但我没有收到任何错误消息

docker compose does not create containers, but I get no error messages

我正在学习关于 Ubuntu 17.10 的官方 Docker CE(版本 18.03.1-ce,内部版本 9ee9f40)教程,但我遇到了 运行 服务问题如上所述 here服务不会启动,但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)

在 docker 中没有办法捕获异常,只能在文件中记录异常,然后导航到容器内部进行检查,直到它存活。您可以使用

docker exec -it container id bash

进入其中或使用

docker diff container id

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

当您在创建与您遇到的网络类似的网络时遇到错误,"container ingress-sbox is already present in sandbox ingress_sbox",值得检查您的 Linux 内核。

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

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

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

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