close

 

建立dockerfile

建立server.py

 

from flask import Flask
server = Flask(__name__)

@server.route("/")
def hello():
    return "Hello World"

if __name__=="__main__":
    server.run(host='0.0.0.0')

 

建立requirements.txt

 

Flask==2.2.2
Werkzeug==2.2.2

 

 

查看目前本地端所有鏡像(images)檔案

docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker/logs-explorer-extension 0.2.5 215b2410d333 3 months ago 12.1MB
docker/disk-usage-extension 0.2.8 e2e81f16dc58 5 months ago 2.82MB
docker/resource-usage-extension 1.0.3 d92fcc1d0f64 8 months ago 14.1MB

建立image

docker build -t flask_python39 .

-t 表示鏡像檔案標籤(tag),加上標籤名稱flask_python39 

> docker images

 下面藍色字為剛才建立的鏡像(image)檔案

> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask_python39 latest 591e90a3d573 45 minutes ago 1.01GB
docker/logs-explorer-extension 0.2.5 215b2410d333 3 months ago 12.1MB
docker/disk-usage-extension 0.2.8 e2e81f16dc58 5 months ago 2.82MB
docker/resource-usage-extension 1.0.3 d92fcc1d0f64 8 months ago 14.1MB

建立container(可以當作某個image的實體)

docker run --rm -d -p 5000:5000 flask_python39  

  • docker run: This command is used to run a Docker container.
  • --rm: This flag specifies that the container should be removed once it exits.
  • -d: This flag runs the container in detached mode, meaning it runs in the background.
  • 5000:5000: 前面為本地端port,冒號後5000為container的port

 查看目前執行中container狀態

> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f1640a38923 flask_python39 "flask run --host=0.…" 2 minutes ago Up 2 minutes 0.0.0.0:5000->5000/tcp reverent_tesla

 

 Now, your Flask application should be running in a Docker container, and you can access it at http://localhost:5000 in your web browser

 

 

> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f1640a38923 flask_python39 "flask run --host=0.…" 2 minutes ago Up 2 minutes 0.0.0.0:5000->5000/tcp reverent_tesla

刪除某個正在執行container

> docker rm 1f1640a38923
Error response from daemon: You cannot remove a running container 1f1640a38923a1a14e560fdf88ec1c5deccd995b2c601a80ad258e1736a86ffb. Stop the container before attempting removal or force remove

停止執行某個正在執行container


> docker stop 1f1640a38923
1f1640a38923

由於前面執行container參數有包含--rm所以停止後會自動刪除container

 查看目前執行中container狀態

 

上面顯示沒有任何執行中的container,因此再次查詢現有的鏡像(images)

> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask_python39 latest 591e90a3d573 23 hours ago 1.01GB
docker/logs-explorer-extension 0.2.5 215b2410d333 3 months ago 12.1MB
docker/disk-usage-extension 0.2.8 e2e81f16dc58 5 months ago 2.82MB
docker/resource-usage-extension 1.0.3 d92fcc1d0f64 8 months ago 14.1MB

 

修改dockerfile

 

修改server.py

 

from flask import Flask
import datetime
server = Flask(__name__)

@server.route("/")
def hello():
    return str(datetime.datetime.now()) + "Hello World"

if __name__=="__main__":
    server.run(host='0.0.0.0')

 

接著建立新的鏡像檔案flask_python39-slim 

docker build -t flask_python39-slim .

> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask_python39-slim latest 1ba1f6ca422a 7 seconds ago 137MB
flask_python39 latest 591e90a3d573 24 hours ago 1.01GB
docker/logs-explorer-extension 0.2.5 215b2410d333 3 months ago 12.1MB
docker/disk-usage-extension 0.2.8 e2e81f16dc58 5 months ago 2.82MB
docker/resource-usage-extension 1.0.3 d92fcc1d0f64 8 months ago 14.1MB

很明顯,選擇python:3.9-slim可以大幅瘦身鏡像檔案大小 

建立container

> docker run --rm -d -p 5000:5000 flask_python39-slim

 

確認container是否正在執行

> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
329c3ee8356e flask_python39-slim "flask run --host=0.…" 6 minutes ago Up 6 minutes 0.0.0.0:5000->5000/tcp sad_matsumoto

停止執行某個container

> docker stop 329c3ee8356e

 

(1) 修改dockerfile

# Use an official Python runtime as a parent image
FROM python:3.9-alpine

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed dependencies specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Define environment variable
ENV FLASK_APP server.py

# Run app.py when the container launches
CMD ["flask", "run", "--host=0.0.0.0"]

(2) 修改server.py

from flask import Flask
import datetime
server = Flask(__name__)

@server.route("/")
def hello():
    return str(datetime.datetime.now()) + ": Hello World"

if __name__=="__main__":
    server.run(host='0.0.0.0')

(3) 建立鏡像檔案

> docker build -t flask_python39_alpine .

 

(4) 列出全部鏡像檔案

> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask_python39_alpine latest 9b6407c5ce0b 3 minutes ago 137MB
flask_python39-slim latest 1f5d8350dbb7 38 minutes ago 137MB
flask_python39 latest 591e90a3d573 25 hours ago 1.01GB
docker/logs-explorer-extension 0.2.5 215b2410d333 3 months ago 12.1MB
docker/disk-usage-extension 0.2.8 e2e81f16dc58 5 months ago 2.82MB
docker/resource-usage-extension 1.0.3 d92fcc1d0f64 8 months ago 14.1MB

(5) 建立container
> docker run --rm -d -p 5000:5000 flask_python39_alpine

(6) 查詢所有container
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6b10693f1f14 flask_python39_alpine "flask run --host=0.…" 6 seconds ago Up 4 seconds 0.0.0.0:5000->5000/tcp wonderful_moore

 

(7) 確認執行結果

http://127.0.0.1:5000/

 

(8) 停止container

> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
6b10693f1f14 flask_python39_alpine "flask run --host=0.…" 2 minutes ago Up 2 minutes 0.0.0.0:5000->5000/tcp funny_haibt
> docker stop 6b10693f1f14

 (9) 刪除鏡像檔案: 方法1

PS D:\flask> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask_python39_alpine latest 04f14988c7bb 10 minutes ago 137MB
flask_python39-slim latest 1f5d8350dbb7 About an hour ago 137MB
flask_python39 latest 591e90a3d573 25 hours ago 1.01GB
docker/logs-explorer-extension 0.2.5 215b2410d333 3 months ago 12.1MB
docker/disk-usage-extension 0.2.8 e2e81f16dc58 5 months ago 2.82MB
docker/resource-usage-extension 1.0.3 d92fcc1d0f64 8 months ago 14.1MB
PS D:\flask> docker rmi 591e90a3d573
Untagged: flask_python39:latest
Deleted: sha256:591e90a3d57328184f57140d59ecfcba07d89f2ac25e20fc8a8900ff865bb9d2

 (10) 確認是否刪除成功

PS D:\flask> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask_python39_alpine latest 04f14988c7bb 11 minutes ago 137MB
flask_python39-slim latest 1f5d8350dbb7 About an hour ago 137MB
docker/logs-explorer-extension 0.2.5 215b2410d333 3 months ago 12.1MB
docker/disk-usage-extension 0.2.8 e2e81f16dc58 5 months ago 2.82MB
docker/resource-usage-extension 1.0.3 d92fcc1d0f64 8 months ago 14.1MB

 (11) 刪除鏡像檔案: 方法2

> docker rmi flask_python39-slim:latest
Untagged: flask_python39-slim:latest
Deleted: sha256:1f5d8350dbb73234b2a8ae50aeb7f051035db83ac6a6e4fdd7ccc60d71f9b87e
PS D:\flask> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask_python39_alpine latest 04f14988c7bb 12 minutes ago 137MB
docker/logs-explorer-extension 0.2.5 215b2410d333 3 months ago 12.1MB
docker/disk-usage-extension 0.2.8 e2e81f16dc58 5 months ago 2.82MB
docker/resource-usage-extension 1.0.3 d92fcc1d0f64 8 months ago 14.1MB

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()