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
$ 

ただし、コンテナーを単独で (つまり、swarm やスタックではなく) 実行すると、期待どおりに動作します。

関連ファイル

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

-2

ドッカーで例外をキャッチする方法はありませんが、例外をファイルに記録してから、コンテナ内を移動して、生きているまで確認します。使用できます

docker exec -it コンテナー ID bash

その中に入るか使用する

docker diff コンテナ ID

ログがログファイルに追加されるたびに、ログファイルになる追加ファイルまたは変更ファイルを確認します。これが方法です。例外を追跡するか、遭遇した場合は共有してください。

于 2018-05-06T19:19:21.787 に答える