文章目錄
- 1. 安裝 docker
- 2. 撰寫代碼
- 3. 撰寫 Dockerfile
- 4. 上傳鏡像
- 5. 修改鏡像
learn from 《深入剖析Kubernetes》
1. 安裝 docker
在 WSL2 中安裝 docker https://www.runoob.com/docker/ubuntu-docker-install.html
會報錯:
# Executing docker install script, commit: 93d2499759296ac1f9c510605fef85052a2c32be
WSL DETECTED: We recommend using Docker Desktop for Windows.
Please get Docker Desktop from https://www.docker.com/products/docker-desktop
You may press Ctrl+C now to abort this script.
+ sleep 20
去下載安裝 windows 下的 docker


2. 撰寫代碼
使用 Flask 框架啟動了一個 Web 服務器,而它唯一的功能是:如果當前環境中有 “NAME” 這個環境變數,就把它列印在 “Hello” 后,否則就列印 “Hello world”,最后再列印出當前環境的 hostname
import os
from flask import Flask
import socket
from gevent import pywsgi
app = Flask(__name__)
@app.route('/')
def hello():
html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname())
if __name__ == "__main__":
server = pywsgi.WSGIServer(('0.0.0.0', 12345), app)
server.serve_forever()
匯出依賴包
pip freeze >requirements.txt
Flask==2.0.1
gevent==21.8.0
greenlet==1.1.1
itsdangerous==2.0.1
Jinja2==3.0.1
MarkupSafe==2.0.1
Werkzeug==2.0.1
zope.event==4.5.0
zope.interface==5.4.0
3. 撰寫 Dockerfile
# 使用官方提供的 Python 開發鏡像作為基礎鏡像
FROM python:3.8-slim
# 將作業目錄切換為 /app
WORKDIR /app
# 將當前目錄下的所有內容復制到 /app 下
ADD . /app
# 使用 pip 命令安裝這個應用所需要的依賴
# RUN pip install --trusted-host pypi.python.org -r requirements.txt
RUN pip install --trusted-host https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
# 國內的源更快
# 允許外界訪問容器的 12345 埠
EXPOSE 12345
# 設定環境變數
ENV NAME World
# 設定容器行程為:python app.py,即:這個 Python 應用的啟動命令
CMD ["python", "app.py"]
# CMD 前面 隱式的包含了 ENTRYPOINT , /bin/sh -c

在 WSL 里操作 :
- 讓 docker 制作鏡像,-t 加 tag,自動加載 Dockerfile,執行里面的陳述句
docker build -t helloworld .
[+] Building 17.4s (10/10) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 757B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/python:3.8-slim 2.9s
=> [auth] library/python:pull token for registry-1.docker.io 0.0s
=> [1/4] FROM docker.io/library/python:3.8-slim@sha256:4dd66d1ccaddaa0587851cb92b365bf3090dccb41393c6f8b 0.0s
=> [internal] load build context 0.1s
=> => transferring context: 813B 0.0s
=> CACHED [2/4] WORKDIR /app 0.0s
=> [3/4] ADD . /app 0.1s
=> [4/4] RUN pip install --trusted-host https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt 13.6s
=> exporting to image 0.6s
=> => exporting layers 0.6s
=> => writing image sha256:390d32b9f7a20ccd347361bd31450807d3e63d052e334865cf8460968ffceff4 0.0s
=> => naming to docker.io/library/helloworld 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
- 查看鏡像
(k8s)PC:/mnt/d/gitcode/k8s$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
helloworld latest 390d32b9f7a2 About a minute ago 169MB
- 啟動容器
docker run -p 4000:12345 helloworld
因為在 Dockerfile 中已經指定了 CMD,否則,就得把行程的啟動命令加在后面 python app.py
- 查看容器啟動
(base) $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6e051d1af6b helloworld "python app.py" 2 minutes ago Up 2 minutes 0.0.0.0:4000->12345/tcp, :::4000->12345/tcp upbeat_elion
通過 -p 4000:12345 告訴 Docker,把容器內的 12345 埠映射在宿主機的 4000 埠上
這樣做的目的是,只要訪問宿主機的 4000 埠,就可以看到容器里應用 回傳的結果
curl http://localhost:4000
# <h3>Hello World!</h3><b>Hostname:</b> dc1c1343e366<br/>
使用容器完成了一個應用的開發與測驗
4. 上傳鏡像
注冊 docker hub,docker login 命令登錄
docker tag helloworld kobe24o/helloworld:v0
kobe24o 是賬號名(鏡像倉庫),helloworld 鏡像名,v0自己分配的版本號
docker push kobe24o/helloworld:v0
(k8s) $ docker push kobe24o/helloworld:v0
The push refers to repository [docker.io/kobe24o/helloworld]
931022d457d6: Pushing [================> ] 16.07MB/47.27MB
c76dc68917fc: Pushed
047ca6dfe9ab: Pushed
d82f4c466b47: Mounted from library/python
5aa75f4e55e7: Mounted from library/python
74d6903a940b: Mounted from library/python
2f9c2b8e82bd: Mounted from library/python
ba5a5fe43301: Mounted from library/python

5. 修改鏡像
(base) $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
dd3bf057cb09 helloworld "python app.py" 7 seconds ago Up 5 seconds 0.0.0.0:4000->12345/tcp, :::4000->12345/tcp compassionate_carver
(base) $ docker exec -it dd3bf057cb09 /bin/sh
# pwd
/app
# touch newfile.txt
# ls
Dockerfile app.py newfile.txt requirements.txt
# exit
(base) $ docker commit dd3bf057cb09 kobe24o/helloworld:v1
sha256:ca8880f84040f9bdd7ef13763b9c64f8bd4a513a74bc2b095be06aae5b60268a
上面操作,新加了一個檔案到鏡像里,commit 保存
docker inspect --format '{{ .State.Pid}}' dd3bf057cb09
1763
# 查看正在運行的容器的行程號 PID
通過查看宿主機的 proc 檔案,看到這個 行程的所有 Namespace 對應的檔案
root:/# ls -l /proc/{PID}/ns/
total 0
lrwxrwxrwx 1 root root 0 Sep 14 11:15 cgroup -> 'cgroup:[4026531835]'
lrwxrwxrwx 1 root root 0 Sep 14 11:15 ipc -> 'ipc:[4026532220]'
lrwxrwxrwx 1 root root 0 Sep 14 09:49 mnt -> 'mnt:[4026532218]'
lrwxrwxrwx 1 root root 0 Sep 14 11:15 net -> 'net:[4026531992]'
lrwxrwxrwx 1 root root 0 Sep 14 11:15 pid -> 'pid:[4026532221]'
lrwxrwxrwx 1 root root 0 Sep 14 11:15 pid_for_children -> 'pid:[4026532221]'
lrwxrwxrwx 1 root root 0 Sep 14 11:15 user -> 'user:[4026531837]'
lrwxrwxrwx 1 root root 0 Sep 14 11:15 uts -> 'uts:[4026532219]'
一個行程,可以選擇 加入 到某個行程已有的 Namespace 當中,從而達到 “進入” 這個行程所在容器的目的,這正是 docker exec 的實作原理
- push 到 hub
(base) $ docker push kobe24o/helloworld:v1
The push refers to repository [docker.io/kobe24o/helloworld]
dfee38b42dbb: Pushed
931022d457d6: Layer already exists
c76dc68917fc: Layer already exists
047ca6dfe9ab: Layer already exists
d82f4c466b47: Layer already exists
5aa75f4e55e7: Layer already exists
74d6903a940b: Layer already exists
2f9c2b8e82bd: Layer already exists
ba5a5fe43301: Layer already exists
v1: digest: sha256:7af7ff571ea9fd70d48abeaa2b38a1ed1c1a4e5a933b722d82af25d3e889f84e size: 2206

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/300576.html
標籤:其他
