1. 程式人生 > >第一次構建、執行、釋出、獲取docker映象

第一次構建、執行、釋出、獲取docker映象

1、前言

過去,如果您要開始編寫Python應用程式,第一步就是把Python的執行環境安裝到您的機器上,而且安裝的環境還要和線上的一致,比較麻煩。

使用Docker,您可以從docker的官方registry或者其他倉庫,獲取一個可移植的Python執行環境映象,無需安裝。然後,你可以基於這個映象開發你的應用程式,這樣可以確保您的應用程式,依賴項和執行時都一起執行。

2、構建一個python映象

2.1、為了構建您自己的映象,首先需要建立一個名稱為Dockerfile的檔案,用於定義建立映象並執行container所需的步驟。 Dockerfile中的每條指令都會在映象中建立一個層級。當您更改Dockerfile並重新build映象時,只重建那些更改的層級。與其他虛擬化技術相比,這是使映象輕量,小巧,快速的一個原因。

建立一個空目錄,建立一個名為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"]
2.2 在與Dockerfile檔案同一個目錄下,建立requirements.txt和app.py檔案。因為Dockerfile檔案的ADD命令,上面的兩個檔案會被加到最終的映象中;因為EXPOSE命令,訪問容器的80埠,才可以訪問到app.py的內容,注意:這裡的80埠指的是容器暴露的埠,並不是實際機器的埠。

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)

2.3 把我們的應用打包為映象,要在DockerFile目錄下執行。這會建立一個Docker映象,我們將使用-t標記它,以使映象有一個友好的名稱。

docker build -t friendlyhello

3 、執行映象

執行應用程式,使用-p將機器的埠4000對映到容器暴露的埠80:

docker run -p 4000:80 friendlyhello

您也可以在shell中使用curl命令來檢視相同的內容。

$ curl http://localhost:4000

<h3>Hello World!</h3><b>Hostname:</b> 8fc990912a14<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>

按crtl+c結束應用

現在讓我們在後臺執行應用程式:

docker run -d -p 4000:80 friendlyhello

檢視所有的container資訊

$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED
1fa4ab2cf395        friendlyhello       "python app.py"     28 seconds ago

現在使用docker container stop來結束程序,使用CONTAINER ID,如下所示:

docker container stop 1fa4ab2cf395

4、釋出映象

4.1、我使用的是阿里雲的docker registry,感覺應該會比較快。首先你要有一個阿里雲的賬號。然後登陸進去新建一個倉庫,設定名稱空間等資訊。


4.2 登陸阿里雲的docker registry,後續操作都需要登陸才可以執行。

sudo docker login --username=admin registry.cn-hangzhou.aliyuncs.com

4.3 為映象打標,tag為可選的,如果沒有,預設為latest

格式:

docker tag image_name registry_url/namespace/repository_name:[tag]

例如

docker tag friendlyhello registry.cn-hangzhou.aliyuncs.com/shuzhou/demo1:latest

檢視本地的映象列表

docker image ls

4.4 釋出映象

docker push registry.cn-hangzhou.aliyuncs.com/shuzhou/demo1:latest

4.5 現在你可以在任何一臺機器上執行下面的命令,執行映象

docker run -p 4000:80 registry.cn-hangzhou.aliyuncs.com/shuzhou/demo1:latest

4.6 拉取映象

docker pullregistry.cn-hangzhou.aliyuncs.com/shuzhou/demo1:latest