我在掌握Docker教程(第一个计时器)时遇到了什么问题。我理解这些概念,但我对于解决问题毫无头绪。
MacOSX:High Sierra v10.13.4 Chrom:版本65.0.3325.181(官方版本)(64位) iTerm2:Build 3.1.5
这是我完成的事情:
第1部分:方向 - 没有代码,安装了docker
docker --version
Docker version 18.03.0-ce, build 0520e24
确定.... 第2部分:https://docs.docker.com/get-started/part2/#dockerfile 创建了Dockerfile
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
ADD . /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
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)
创建的requirements.txt
requirements.txt:
Flask
Redis
很酷,所以在我的目录中......
$ls
Dockerfile app.py requirements.txt
接下来我构建图像......是吗?
$docker build -t image1 .
得分。那我健全检查......
$ docker image ls
REPOSITORY TAG IMAGE ID
image1 latest c40d667d22f1
python 2.7-slim ce007f0fff83
看起来很棒!啊? 然后我运行它
$docker run -p 4000:80 image1
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
我按CTRL + C ......然后!
$ curl http://localhost:4000
<h3>Hello World!</h3>
<b>Hostname:</b> a934716c2975<br/>
<b>Visits:</b><i>cannot connect to Redis, counter disabled</i>
预计......它说不要担心没有redis工作。我希望它不是,fml。
让我们分享吧!
$ docker login
Login Succeeded
$docker tag image1 joha0033/image1repo:image1tag
REPOSITORY TAG IMAGE ID
joha0033/image1repo image1tag c40d667d22f1
image1 latest c40d667d22f1
python 2.7-slim ce007f0fff83
$docker push joha0033/image1repo:image1tag
The push refers to repository [docker.io/joha0033/image1repo]
41acc25d8003: Pushed
4165b88689b8: Pushed
5bcdad5f3718: Pushed
386a80c8d5fc: Mounted from joha0033/get-started
332873801f89: Mounted from joha0033/get-started
2ec65408eff0: Mounted from joha0033/get-started
43efe85a991c: Mounted from joha0033/get-started
image1tag: digest: sha256:3ac0db69c366afa2bc099ec7762190d5f89c7270953ea8ff093a25b2a18b87c4 size: 1788
$docker run -p 4000:80 joha0033/image1repo:image1tag
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
(几行,从joha0033 / get-started安装......我尝试了一次之前有相同的结果,我删除了修剪过的所有东西-a,我想我开始新鲜了......)
END PART 2 // ENTER .... PART 3!
在先决条件中:https://docs.docker.com/get-started/part3/ 最后一点,它说确保它适用于80:80和http://localhost/ 原因:确保您的图像作为已部署的容器运行。
怎么做?
$docker run -p 80:80 joha0033/image1repo:image1tag
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
所以,现在我试试......
Chrome和Firefox:http://localhost/(无法连接)
AND
$curl http://localhost/
$curl: (7) Failed to connect to localhost port 80: Connection refused
以下在localhost上工作正常:4000 / AND $ curl http://localhost:4000/
$ docker run -p 4000:80 joha0033/image1repo:image1tag
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
但是,我不能让以下工作,虽然它看起来像它的工作!?
$ docker run -p 80:80 joha0033/image1repo:image1tag
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
这只是权限吗?没有网络搜索非常有帮助......你们都已经超越了我的脑海!救命!谢谢!为此感到兴奋!!!
请不要告诉我,我忘记引号或愚蠢的东西......但最有可能......
我还尝试过https,localhost:8080 /,在云端检查,公开。
先谢谢!
增加:
$sudo lsof -i :80
Google 329 webleddevelopment 216u (ESTABLISHED)
Google 329 webleddevelopment 249u (ESTABLISHED)
Google 329 webleddevelopment 252u (CLOSE_WAIT)
到底是什么意思?!