我不明白我错过了什么。

docker.compose.yml

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: svezday/friendlyhello:part-1
    deploy:
      replicas: 5
      restart_policy:
        condition: on-failure
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
    ports:
      - "80:80"
    networks:
      - webnet
  visualizer:
    image: dockersamples/visualizer:stable
    ports:
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      placement:
        constraints: [node.role == manager]
    networks:
      - webnet
networks:
  webnet:

文件
# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

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

# Install any needed packages specified in requirements.txt
RUN pip 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 ["python", "app.py"]

应用程序
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)

我在 ubuntu 18 上,使用 vitualbox。

这是虚拟机
NAME    ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER     ERRORS
myvm1   *        virtualbox   Running   tcp://192.168.99.100:2376           v18.09.0

要求.txt
Flask
Redis
myvm2   -        virtualbox   Running   tcp://192.168.99.101:2376           Unknown    Unable to query docker version: Get https://192.168.99.101:2376/v1.15/version: x509: certificate is valid for 192.168.99.103, not 192.168.99.101

docker-machine ssh myvm1
docker swarm init --advertise-addr 192.168.99.101:2377 getStartNow

docker-machine ssh myvm2
docker swarm join --token SWMTKN-1-29dkoqd6tskoqszzrdpcnw0nbmrgbrw9xr27yoxtvapodk6qmg-3tv01eh1ts0n97s5c5zq7q4ju 192.168.99.100:2377

docker-machine ssh myvm1
 docker stack deploy -c docker.compose.yml getStartNow

docker 堆栈ls
NAME                SERVICES            ORCHESTRATOR
getStartNow         2                   Swarm

码头服务 ls
ID                  NAME                     MODE                REPLICAS            IMAGE                             PORTS
w9l0khipey4v        getStartNow_visualizer   replicated          1/1                 dockersamples/visualizer:stable   *:8080->8080/tcp
3yoifm7inujf        getStartNow_web          replicated          5/5                 svezday/friendlyhello:part-1      *:80->80/tcp

这是我的问题

curl http://192.168.99.100:80
 curl: (7) Failed to connect to 192.168.99.100 port 80: Connection refused

curl http://192.168.99.100:8080
 curl: (7) Failed to connect to 192.168.99.100 port 8080: Connection refused

最佳答案

我有同样的问题。
我遵循了 Elavaud 解决方案 here,它对我有用。
所以:

  • 我从 here 下载了 boot2docker.iso
  • 检查事件虚拟机docker-machine ls
  • 销毁所有虚拟机(myvm1 和 myvm2)docker-machine rm $(docker-machine ls -q)
  • 再次创建指定下载的 boot2docker.iso 路径的虚拟机
  • docker-machine create --driver virtualbox --virtualbox-boot2docker-url path_to_your_boot2docker.iso virtual_machine_name在我的情况下,路径是 ~/Downloads/boot2docker.iso 所以我做了
    docker-machine create --driver virtualbox --virtualbox-boot2docker-url ~/Downloads/boot2docker.iso myvm1
    
    docker-machine create --driver virtualbox --virtualbox-boot2docker-url ~/Downloads/boot2docker.iso myvm2
    
  • 重新开始 第 4 部分

  • 最后想一想,我看到您的 docker-compose.yml 与在 get_started part3 中创建的 docker-compose.yml 不同。我不知道这是否可能是问题所在。
    在我的情况下,我在 get_started part3 中使用了相同的 docker-compose.yml,所以当我访问我的应用程序时,我使用端口 4000
    curl http://192.168.99.101:4000/
    

    关于docker - Docker 教程上的连接被拒绝第 4 部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53451285/

    10-16 02:26