简单的Mac版Docker封装
之前没有封装过docker文件,所以这个是一个尝试的记录。
我的目录文件如下:
这里面的这个 requirements.txt 是自动生成的。生成方法如下:
安装pipreqs:
pip install pipreqs
进入你的工程所在目录,然后输入下面的命令,等待命令运行结束后,就会生成requirements.txt文件了。
(playwright_tianmao) (base) kaka@KakadeMacBook-Pro playwright_tianmao % pipreqs --encoding=utf8 ./project INFO: Successfully saved requirements file in ./project/requirements.txt
编写Dockerfile
FROM python:3.10 WORKDIR ./project ADD . . RUN pip install -r requirements.txt CMD ["python", "./src/tian_mao.py"]
Dockerfile 文件这么写,具体参数看这Docker Dockerfile。
Docker命令行
好了,重头戏来了,先cd 到文件夹内部,当然需要把Docker这个程序运行起来。
现在执行docker images
是没有我新打包的。
(base) kaka@KakadeMacBook-Pro ~ % docker images REPOSITORY TAG IMAGE ID CREATED SIZE clickhouse/clickhouse-server <none> 854810a3ea35 2 months ago 705MB redis alpine 7bf9ac7d3b84 3 months ago 28.1MB mysql/mysql-server 8.0 55af6abb77a6 4 months ago 527MB fp2_0-8.1/app latest 53b297f8c7a6 6 months ago 1.61GB clickhouse/clickhouse-server latest d846490c0466 14 months ago 794MB prom/prometheus latest ccf2871d6122 14 months ago 198MB grafana/grafana-enterprise latest 05048bc21afe 15 months ago 290MB prom/alertmanager latest 44a71f29f42b 18 months ago 55.3MB
我们执行命令:
1 (base) kaka@KakadeMacBook-Pro project % docker build -t python/playwright_tianmao:3.10 . 2 [+] Building 113.5s (9/9) FINISHED 3 => [internal] load build definition from Dockerfile 0.0s 4 => => transferring dockerfile: 36B 0.0s 5 => [internal] load .dockerignore 0.0s 6 => => transferring context: 2B 0.0s 7 => [internal] load metadata for docker.io/library/python:3.10 15.8s 8 => [internal] load build context 73.4s 9 => => transferring context: 1.04GB 73.4s 10 => [1/4] FROM docker.io/library/python:3.10@sha256:dbbfcbf95f6b596d2be1d8f3b368016619f78f829facf6f2e361bea1151794e5 0.0s 11 => CACHED [2/4] WORKDIR ./project 0.0s 12 => [3/4] ADD . . 3.0s 13 => [4/4] RUN pip install -r requirements.txt 19.1s 14 => exporting to image 2.1s 15 => => exporting layers 2.1s 16 => => writing image sha256:17393efb36ac8478379f160b11de4b8f17b553a86fa233870c272cc9308afe9c 0.0s 17 => => naming to docker.io/python/playwright_tianmao 0.0s 18 19 Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
再次执行docker images:
1 (base) kaka@KakadeMacBook-Pro project % docker images 2 REPOSITORY TAG IMAGE ID CREATED SIZE 3 python/playwright_tianmao latest 24afb40d4b24 5 seconds ago 1.01GB 4 clickhouse/clickhouse-server <none> 854810a3ea35 2 months ago 705MB 5 redis alpine 7bf9ac7d3b84 3 months ago 28.1MB 6 mysql/mysql-server 8.0 55af6abb77a6 4 months ago 527MB 7 fp2_0-8.1/app latest 53b297f8c7a6 7 months ago 1.61GB 8 clickhouse/clickhouse-server latest d846490c0466 14 months ago 794MB 9 prom/prometheus latest ccf2871d6122 14 months ago 198MB 10 grafana/grafana-enterprise latest 05048bc21afe 15 months ago 290MB 11 prom/alertmanager latest 44a71f29f42b 18 months ago 55.3MB 12 (base) kaka@KakadeMacBook-Pro project %
导出的命令:
docker save -o playwright_tianmao.tar python/playwright_tianmao
删除:
(base) kaka@KakadeMacBook-Pro ~ % docker rmi 24afb40d4b24
然后:
docker load -i playwright_tianmao.tar
查看结果:
(base) kaka@KakadeMacBook-Pro project % docker load -i playwright_tianmao.tar
Loaded image: playwright:tian_mao (base) kaka@KakadeMacBook-Pro project % docker images REPOSITORY TAG IMAGE ID CREATED SIZE playwright tian_mao 281e004ab42d 30 minutes ago 1.01GB clickhouse/clickhouse-server <none> 854810a3ea35 2 months ago 705MB redis alpine 7bf9ac7d3b84 3 months ago 28.1MB mysql/mysql-server 8.0 55af6abb77a6 4 months ago 527MB fp2_0-8.1/app latest 53b297f8c7a6 6 months ago 1.61GB clickhouse/clickhouse-server latest d846490c0466 14 months ago 794MB prom/prometheus latest ccf2871d6122 14 months ago 198MB grafana/grafana-enterprise latest 05048bc21afe 15 months ago 290MB prom/alertmanager latest 44a71f29f42b 18 months ago 55.3MB (base) kaka@KakadeMacBook-Pro project %
参考链接: