日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Docker技术笔记:Docker入门浅尝

發(fā)布時間:2025/3/21 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Docker技术笔记:Docker入门浅尝 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 https://blog.csdn.net/zhaobryant/article/details/79600059

簡介

本文將用Docker的方式來構(gòu)建一個應(yīng)用APP。

過去,如果要開發(fā)一個Python應(yīng)用APP,所需做的第一件事就是在開發(fā)機上安裝Python運行時環(huán)境。在這種情形下,開發(fā)機的環(huán)境必須與APP所要求的環(huán)境一致,同時還需要與生產(chǎn)環(huán)境相匹配。

通過使用Docker,可以將一個可移植的Python運行時環(huán)境作為一個image獲取,而無需安裝。然后,就可以基于Python運行時環(huán)境image,將APP代碼及其依賴庫合并構(gòu)建,從而簡化了應(yīng)用APP的部署難度。

用Dockerfile定義容器Container

Dockerfile是由一系列命令和參數(shù)構(gòu)成的腳本,這些命令應(yīng)用于基礎(chǔ)鏡像并最終創(chuàng)建一個新的鏡像。它簡化了業(yè)務(wù)部署的流程,大大提高了業(yè)務(wù)的部署速度。Dockerfile的產(chǎn)出為一個新的可以用于創(chuàng)建容器的鏡像。

Dockerfile語法由兩部分構(gòu)成,分別是“注釋”和“命令+參數(shù)”。

# Line blocks used for commenting COMMAND argument1 argument2 ...

對于Dockerfile,我們首先創(chuàng)建一個空目錄,然后cd到該目錄,并創(chuàng)建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"]

其中,app.py和requirements.txt都位于與Dockerfile相同的目錄下,具體如下:

zjl@ubuntu:~/docker/pyapp$ ls app.py Dockerfile requirements.txt

對于requirements.txt,其內(nèi)容如下:

Flask Redis

對于app.py,其內(nèi)容如下:

from flask import Flask from redis import Redis, RedisError import os import socketredis = 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)

現(xiàn)在,我們知道,pip install -r requirements.txt為Python安裝了Flask和Redis庫,同時APP會打印出環(huán)境變量NAME,同時將socket.gethostname()打印出來。

構(gòu)建新的image鏡像

下面,我們進(jìn)行構(gòu)建,具體如下:

zjl@ubuntu:~/docker/pyapp$ ls app.py Dockerfile requirements.txtzjl@ubuntu:~/docker/pyapp$ sudo docker build -t fhello . Sending build context to Docker daemon 4.608kB Step 1/7 : FROM python:2.7-slim 2.7-slim: Pulling from library/python d2ca7eff5948: Pull complete cef69dd0e5b9: Pull complete 50e1d7e4f3c6: Pull complete 861e9de5333f: Pull complete Digest: sha256:e9baca9b405d3bbba71d4c3c4ce8a461e4937413b8b910cb1801dfac0a2423aa Status: Downloaded newer image for python:2.7-slim---> 52ad41c7aea4 Step 2/7 : WORKDIR /app ... Step 3/7 : ADD . /app ... Step 4/7 : RUN pip install --trusted-host pypi.python.org -r requirements.txt ... Step 5/7 : EXPOSE 80 ... Step 6/7 : ENV NAME World ... Step 7/7 : CMD ["python", "app.py"] ... Successfully built d3fafd68e807 Successfully tagged fhello:latestzjl@ubuntu:~/docker/pyapp$ sudo docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE fhello latest 12d000cd7a1b 12 minutes ago 148MB

運行新的image鏡像

$ sudo docker run -p 4000:80 fhello* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)# 訪問該網(wǎng)站 $ curl localhost:4000 <h3>Hello World!</h3><b>Hostname:</b> f4e37f061593<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>$ sudo docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4a150bdc67cd fhello "python app.py" 10 seconds ago Up 9 seconds 0.0.0.0:4000->80/tcp upbeat_austin

關(guān)閉容器,命令如下:

$ sudo docker container stop 4a150bdc67cd 4a150bdc67cd

給鏡像打上標(biāo)簽

syntax: -->> docker tag image username/repository:tag $ sudo docker tag fhello zhjl/getstarted:alpha-1 $ sudo docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE fhello latest 12d000cd7a1b 18 minutes ago 148MB zhjl/getstarted alpha-1 12d000cd7a1b 18 minutes ago 148MB$ docker run -p 4000:80 zhjl/getstarted:alpha-1

Recap and cheat sheet

# Create image using this directory's Dockerfile docker build -t fhello .# Run "friendlyname" mapping port 4000 to 80 docker run -p 4000:80 fhello# Same thing, but in detached mode docker run -d -p 4000:80 fhello# List all running containers docker container ls# List all containers, even those not running docker container ls -a# Gracefully stop the specified container docker container stop <hash># Force shutdown of the specified container docker container kill <hash># Remove specified container from this machine docker container rm <hash># Remove all containers docker container rm $(docker container ls -a -q)# List all images on this machine docker image ls -a# Remove specified image from this machine docker image rm <image id># Remove all images from this machine docker image rm $(docker image ls -a -q)# Log in this CLI session using your Docker credential docker login# Tag <image> for upload to registry docker tag <image> username/repository:tag# Upload tagged image to registry docker push username/repository:tag# Run image from a registry docker run username/repository:tag

總結(jié)

以上是生活随笔為你收集整理的Docker技术笔记:Docker入门浅尝的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。