Docker 容器


1.  容器

在過去,如果要開始編寫Python應用程序,首先要做的就是在機器上安裝Python運行時環境。但是,這就造成了這樣一種情況:你的機器上的環境需要完美,以便你的應用程序能夠按預期運行,而且還需要與你的生產環境相匹配。

使用Docker,你只需要獲取一個可移植的Python運行時作為鏡像,不需要安裝。然后,當你構建應用程序時就會在代碼旁邊包含基本的Python鏡像,確保應用程序、它的依賴項和運行時一起運行。

這些可移植的鏡像被一些稱之為“Dockerfile”來定義。

2.  用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 COPY . /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"]

可以看到,Dockerfile文件需要引用app.py和requirements.txt文件,於是,在與Dockerfile同級的目錄下創建這兩個文件

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)

構建App

$ ls Dockerfile app.py requirements.txt
docker build --tag=friendlyhello .
$ docker image ls REPOSITORY TAG IMAGE ID friendlyhello latest 326387cea398

注意:tag默認是latest,當然你可以手動指定,比如:--tag=friendlyhello:v0.0.1

3.  運行App

運行app,將本地4000端口映射到容器對外公布的80端口

docker run -p 4000:80 friendlyhello

4.  共享你的鏡像

為了能夠在任何地方都運行,我們需要將我們的鏡像上傳到注冊中心。注冊中心是倉庫的集合,而倉庫是鏡像的集合。這很像GitHub倉庫或者Maven倉庫。一個賬號可以在注冊中心中創建許多個倉庫。

用Docker ID登錄

$ docker login

5.  給鏡像打標簽

將本地鏡像關聯到注冊中心的某個倉庫,格式是這樣的: username/repository:tag

其中,tag是可選的,但是推薦加上tag。當你關聯到倉庫后,注冊中心會給這個鏡像分配一個版本號

給鏡像打Tag的格式如下:

docker tag image username/repository:tag

例如,我們給我們剛才的friendlyhello打一個tag

docker tag friendlyhello gordon/get-started:part2

6.  發布鏡像

docker push username/repository:tag

(PS:這個過程很像git在本地打標簽並推送到遠程倉庫)

1 git tag v1.0
2 git push origin v1.0 

推送成功以后,我們就可以在注冊中心看到了

 

7.  備忘單

docker build -t friendlyhello . # Create image using this directory's Dockerfile docker run -p 4000:80 friendlyhello # Run "friendlyhello" 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM