docker----容器

先閱讀上一章 docker-基礎知識與安裝

 

一、介紹

現在是時候開始使用Docker方式構建應用程序了。我們從這樣一個應用程序層次結構的底部開始,這是一個容器,我們將在此頁面上介紹它。高於此級別的是一項服務,它定義了容器在生產中的行爲方式堆

 

 

二、使用定義容器 Dockerfile

Dockerfile定義容器內環境中發生的事情。對網絡接口和磁盤驅動器等資源的訪問在此環境中進行虛擬化,該環境與系統的其他部分隔離,因此您需要將端口映射到外部世界,並具體說明要複製到哪些文件那個環境。但是,在執行此操作之後,您可以預期Dockerfile在此處定義的應用程序的構建 在其運行的任何位置都會完全相同。

 

Dockerfile

創建一個test目錄。進入目錄,創建一個名爲的文件Dockerfile,將以下內容複製並粘貼到該文件中,然後保存。記下解釋新Dockerfile中每個語句的註釋。

# 使用官方python鏡像

FROM python:2.7-slim

 

# 設置工作目錄/app

WORKDIR /app

 

# 將當前目錄內容複製到/app應用程序容器中

COPY . /app

 

# 安裝文件中指定包

RUN pip install --trusted-host pypi.python.org -r requirements.txt

 

# 暴露容器端口給外部使用

EXPOSE 80

 

# 定義環境變量

ENV NAME World

 

# 當容器啓動時運行app.py

CMD ["python", "app.py"]

 

 

三、創建其餘文件

再創建兩個文件,requirements.txt然後app.py將它們放在同一個文件夾中Dockerfile。這完成了我們的應用程序,您可以看到它非常簡單。當上述Dockerfile被內置到的圖像,app.py並且requirements.txt是因爲存在DockerfileCOPY命令,並從輸出app.py是通過HTTP得益於訪問EXPOSE 命令。

image.png

[root@zabbix-agnet test]# cat requirements.txt

Flask

Redis

 

[root@zabbix-agnet test]# cat app.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

 

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)

現在我們看到pip install -r requirements.txtPython安裝FlaskRedis庫,應用程序打印環境變量NAME,以及調用的輸出socket.gethostname()。最後,因爲Redis沒有運行(因爲我們只安裝了Python庫,而不是Redis本身),我們應該期望在這裏使用它的嘗試失敗併產生錯誤消息。

注意:在容器內部訪問容器ID時,訪問主機名稱,這類似於正在運行的可執行文件的進程ID

而已!您不需要Pythonrequirements.txt系統中的任何內容,也不需要構建或運行此映像將它們安裝在您的系統上。看起來你並沒有真正建立一個PythonFlask的環境,但你有。

 

四、構建應用程序

現在運行build命令。這會創建一個Docker鏡像,我們將使用-t它來標記,因此它具有友好的名稱。

[root@zabbix-agnet test]# docker build -t friendlyhello .

image.png

此處有可能有報錯

image.png

解決方法:

[root@zabbix-agnet test]# service docker restart

image.png

 

五、運行該應用程序

運行應用程序,使用以下方法將計算機的端口4000映射到容器的已發佈端口80 -p

image.png 

本機檢查和web界面端均可檢查

您應該看到Python正在爲您的應用程序提供服務的消息http://0.0.0.0:80。但是該消息來自容器內部,它不知道您將該容器的端口80映射到4000,從而生成正確的URL http://localhost:4000

Web瀏覽器中轉到該URL,以查看在網頁上提供的顯示內容。

image.png

注意:如果您在Windows 7上使用Docker Toolbox,請使用Docker Machine IP而不是localhost。例如,http//192.168.99.1004000 /。要查找IP地址,請使用該命令docker-machine ip


六、啓動容器停止容器

 image.png

  

七、分享你的鏡像

Docker hub 官網註冊不了,下面這位大神解決了

https://blog.csdn.net/m0_37985112/article/details/83013700

 

使用您的Docker ID登錄

image.png

 

八、標記圖像

將本地映像與註冊表上的存儲庫相關聯的表示法是 username/repository:tag。標籤是可選的,但建議使用,因爲它是註冊管理機構用來爲Docker鏡像提供版本的機制。爲上下文提供存儲庫和標記有意義的名稱,例如 get-started:part1。這會將圖像放入get-started存儲庫並將其標記爲part1

現在,把它們放在一起來標記圖像。docker tag image使用您的用戶名,存儲庫和標記名稱運行,以便將圖像上載到所需的目標位置。該命令的語法是:

image.png

完成後,此上傳的結果將公開發布。如果您登錄到Docker Hub,則會在其中看到新圖像及其pull命令。

 

九、從遠程存儲庫中拉出並運行映像

如果映像在計算機上不可用,則Docker會從存儲庫中提取映像。

image.png無論在哪裏docker run執行,它都會提取您的圖像,以及Python和所有依賴項requirements.txt,並運行您的代碼。它們都在一個整潔的小包中一起旅行,你不需要在主機上安裝任何東西,以便Docker運行它。

 

回顧和備忘單(摘取自官網)

以下是此頁面中基本Docker命令的列表,以及一些相關的命令,如果您想在繼續之前稍微探索一下。

docker build -t friendlyhello .           # Create image using this directory's Dockerfile

docker run -p 4000:80 friendlyhello       # Run "friendlyname" mapping port 4000 to 80

docker run -d -p 4000:80 friendlyhello         # Same thing, but in detached mode

docker container ls                                # List all running containers

docker container ls -a                  # List all containers, even those not running

docker container stop <hash>             # Gracefully stop the specified container

docker container kill <hash>            # Force shutdown of the specified container

docker container rm <hash>              # Remove specified container from this machine

docker container rm $(docker container ls -a -q)          # Remove all containers

docker image ls -a                               # List all images on this machine

docker image rm <image id>                    # Remove specified image from this machine

docker image rm $(docker image ls -a -q)     # Remove all images from this machine

docker login                                         # Log in this CLI session using your Docker credentials

docker tag <image> username/repository:tag      # Tag <image> for upload to registry

docker push username/repository:tag                # Upload tagged image to registry

docker run username/repository:tag                   # Run image from a registry

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章