Docker教程上的连接被拒绝入门第4部分

时间:2018-11-23 18:10:43

标签: docker virtualbox docker-machine

我不明白我错过了什么。

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:

Dockerfile

# 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"]

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   

requirements.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 stack ls

NAME                SERVICES            ORCHESTRATOR
getStartNow         2                   Swarm

docker service 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

这是我的问题

卷曲http://192.168.99.100:80

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

卷曲http://192.168.99.100:8080

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

2 个答案:

答案 0 :(得分:7)

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

所以:

  1. 我从here下载了boot2docker.iso
  2. 检查活动的虚拟机

    docker-machine ls

  3. 销毁所有虚拟机(myvm1和myvm2)

    docker-machine rm $(docker-machine ls -q)

  4. 再次创建指定下载的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
  1. 重新开始,开始第4部分

最后,我看到您的docker-compose.yml与在get_started part3中创建的docker-compose.yml不同。我不知道这可能是问题。 就我而言,我在get_started part3中使用了相同的docker-compose.yml,因此当我访问我的应用程序时,我使用端口4000

curl http://192.168.99.101:4000/

答案 1 :(得分:1)

我也有这个问题,但是找到了一个稍微简单的解决方案。无需下载boot2docker并在您的计算机上指向它,您只需将virtualbox-boot2docker-url标志设置为降级版本的网址即可;例如:

docker-machine create myvm1 --virtualbox-boot2docker-url "https://github.com/boot2docker/boot2docker/releases/download/v18.06.1-ce/boot2docker.iso"
docker-machine create myvm2 --virtualbox-boot2docker-url "https://github.com/boot2docker/boot2docker/releases/download/v18.06.1-ce/boot2docker.iso"