我正在阅读 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)