1. 程式人生 > >Docker嚐鮮記-Mac Docker安裝流程

Docker嚐鮮記-Mac Docker安裝流程

久仰Docker大名已久,於是今天趁著有空,嘗試了一下Docker
先是從docker的官網上下載下來mac版本的docker安裝包,安裝很簡易,就直接拖圖示就好了。

按照官網教程,輸入了:

$ docker run hello-world

輸入了好多次總是出錯。

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b04784fba78d: Pulling fs layer 
docker: error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/registry-v2/docker/registry/v2/blobs/sha256/18/1815c82652c03bfd8644afda26fb184f2ed891d921b20a0703b46768f9755c57/data?Expires=1504854289&Signature=Sq~Ruv4S51ePsuXxIXTDZ8PFESR5Eu6bqmPyYjcQPeplwqOVZxsaF8W3tsIqMMebNz0hZ1jJWN7LoSWF9pPbhIlwOfuKkT92JvbDN30DvftzisY5dYuw3BKmrqiv67QmWRYIsFdGiQxb2rWE0quUiSvKAWs5pfF6dhbXRtVrAxA_&Key-Pair-Id=APKAJECH5M7VWIS5YZ6Q:
dial tcp: lookup dseasb33srnrn.cloudfront.net on 192.168.65.1:53: read udp 192.168.65.2:54463->192.168.65.1:53: i/o timeout. See 'docker run --help'.

於是查看了stackoverlow,發現是proxy的問題,哦,就是我開了vpn唄,於是就把藍燈的vpn關了,再輸入一次,依舊不行。這時候再看,發現要重啟docker。於是重啟之後,果然噹噹噹安裝好了。

 docker-image-files docker run hello-world
Unable to
find image 'hello-world:latest' locally latest: Pulling from library/hello-world b04784fba78d: Pull complete Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to
be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://cloud.docker.com/ For more examples and ideas, visit: https://docs.docker.com/engine/userguide/

建立APP

接著需要裝docker上的環境了,於是新建一個資料夾Docker-Image, 然後再在這個資料夾裡面vim 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 -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"]

接著同一目錄下,建立兩個檔案requirements.txt 以及 app.py

requirements.txt

Flask
Redis

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)

接著確保自己在當前目錄Docker-Image下:

$ ls
Dockerfile      app.py          requirements.txt

開始建立docker的app,不要忘記最後的那個點

$ docker build -t friendlyhello .

如果建立成功的話,會在你當前的目錄下

$ docker images

REPOSITORY            TAG                 IMAGE ID
friendlyhello         latest              326387cea398

然額我肯定又失敗了:

Sending build context to Docker daemon  4.608kB
Step 1/7 : FROM python:2.7-slim
2.7-slim: Pulling from library/python
ad74af05f5a2: Pulling fs layer 
8812637047e3: Pulling fs layer 
be169522399f: Pulling fs layer 
286703095347: Waiting 
error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/registry-v2/docker/registry/v2/blobs/sha256/45/451c85955bc2512838b77c3d26db434f32b9e1f532472d0869d4a719fc179ac6/data?Expires=1504856139&Signature=KzkIc3MDDMJ0wGZctxz~OJu2XoBsJNe1y0v5cLd31g6cCmeLIbgtvaJaAoO7hBDZpELpl~lzzBB2Gz7AV4xAr41v-5Qf7ylZwreaHQ65Fyoelok4vrtGBvoGktW2Rzz-gQPcKaQ5KJwbOvRO6qOYKbnQT8HmUuzECld4YDbAiss_&Key-Pair-Id=APKAJECH5M7VWIS5YZ6Q: dial tcp: lookup dseasb33srnrn.cloudfront.net on 192.168.65.1:53: read udp 192.168.65.2:47936->192.168.65.1:53: i/o timeout

又是i/o timeout的問題。
怎能放棄治療?!於是又使用了stackoverflow大法:
改DNS地址為8.8.8.8
這時候再run一遍命令,神奇地開始下載了……
如果你看到這個長長長長的安裝內容以及最後一句successfully,說明你已經成功build了docker的app環境。

➜  docker-image-files docker build -t friendlyhello .
Sending build context to Docker daemon  4.608kB
Step 1/7 : FROM python:2.7-slim
2.7-slim: Pulling from library/python
ad74af05f5a2: Pull complete 
8812637047e3: Pull complete 
be169522399f: Pull complete 
286703095347: Pull complete 
Digest: sha256:5d668aa50cac534b08df02d942a6d1e9e71b38da9386ee54b3312be0af138469
Status: Downloaded newer image for python:2.7-slim
 ---> 451c85955bc2
Step 2/7 : WORKDIR /app
 ---> 08ccfba76a50
Removing intermediate container 3c5879c8130f
Step 3/7 : ADD . /app
 ---> 3fe444ae9b98
Removing intermediate container 8a2d221f93ee
Step 4/7 : RUN pip install -r requirements.txt
 ---> Running in e37e45584db6
Collecting Flask (from -r requirements.txt (line 1))
  Downloading Flask-0.12.2-py2.py3-none-any.whl (83kB)
Collecting Redis (from -r requirements.txt (line 2))
  Downloading redis-2.10.6-py2.py3-none-any.whl (64kB)
Collecting itsdangerous>=0.21 (from Flask->-r requirements.txt (line 1))
  Downloading itsdangerous-0.24.tar.gz (46kB)
Collecting Jinja2>=2.4 (from Flask->-r requirements.txt (line 1))
  Downloading Jinja2-2.9.6-py2.py3-none-any.whl (340kB)
Collecting Werkzeug>=0.7 (from Flask->-r requirements.txt (line 1))
  Downloading Werkzeug-0.12.2-py2.py3-none-any.whl (312kB)
Collecting click>=2.0 (from Flask->-r requirements.txt (line 1))
  Downloading click-6.7-py2.py3-none-any.whl (71kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->Flask->-r requirements.txt (line 1))
  Downloading MarkupSafe-1.0.tar.gz
Building wheels for collected packages: itsdangerous, MarkupSafe
  Running setup.py bdist_wheel for itsdangerous: started
  Running setup.py bdist_wheel for itsdangerous: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/fc/a8/66/24d655233c757e178d45dea2de22a04c6d92766abfb741129a
  Running setup.py bdist_wheel for MarkupSafe: started
  Running setup.py bdist_wheel for MarkupSafe: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/88/a7/30/e39a54a87bcbe25308fa3ca64e8ddc75d9b3e5afa21ee32d57
Successfully built itsdangerous MarkupSafe
Installing collected packages: itsdangerous, MarkupSafe, Jinja2, Werkzeug, click, Flask, Redis
Successfully installed Flask-0.12.2 Jinja2-2.9.6 MarkupSafe-1.0 Redis-2.10.6 Werkzeug-0.12.2 click-6.7 itsdangerous-0.24
 ---> a81e2bc9de84
Removing intermediate container e37e45584db6
Step 5/7 : EXPOSE 80
 ---> Running in 1a42c917a0d5
 ---> 0dc7297ab9cd
Removing intermediate container 1a42c917a0d5
Step 6/7 : ENV NAME World
 ---> Running in df9e70eb4d1b
 ---> 2fa8053a651b
Removing intermediate container df9e70eb4d1b
Step 7/7 : CMD python app.py
 ---> Running in e183136d2fd4
 ---> 46f195936dee
Removing intermediate container e183136d2fd4
Successfully built 46f195936dee
Successfully tagged friendlyhello:latest

命令列裡輸入 docker images,顯然安裝成功了。

$ docker-image-files docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
friendlyhello       latest              46f195936dee        10 minutes ago      194MB
python              2.7-slim            451c85955bc2        6 weeks ago         182MB

接著愉快滴看到docker跑起來啦!

➜  docker-image-files docker run -p 4000:80 friendlyhello
 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
172.17.0.1 - - [08/Sep/2017 08:05:38] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [08/Sep/2017 08:05:38] "GET /favicon.ico HTTP/1.1" 404 -

這裡寫圖片描述
我直接conmand+c關掉了docker的app。