Docker教程连接被拒绝开始第4部分

问题描述 投票:4回答:2

我不明白我错过了什么。

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.朋友

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上,使用virtualbox。

这是vm

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

码头工人堆栈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

这是我的问题

卷曲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
docker virtualbox docker-machine
2个回答
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
投票

我也有这个问题,但发现了一个稍微简单的解决方案;您可以将virtualbox-boot2docker-url标志设置为降级版本的URL,而不是下载boot2docker并在您的计算机上指向它。例如。:

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"
© www.soinside.com 2019 - 2024. All rights reserved.