Docker尝鲜记-Mac Docker安装流程

本文记录了作者首次安装并使用Docker的过程,从安装Docker到创建并运行第一个Docker应用,包括解决过程中遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

久仰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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值