
使用Docker部署Python脚本:测试工程师的容器化之旅
大约 4 分钟
使用Docker部署Python脚本:测试工程师的容器化之旅
还在为"在我的机器上能跑"而烦恼吗?Docker就像是给你的Python脚本穿上了一件万能的外套,无论走到哪里都能完美运行!作为测试工程师,掌握Docker部署技能,就像拥有了一个随身携带的测试环境工厂。
前置知识储备
在开始我们的Docker之旅前,你需要了解:
- Docker基本概念:镜像、容器、仓库
- Python基础:虚拟环境、包管理
- Linux基本命令:文件操作、权限管理
学习资源推荐
第一步:基础镜像探索
拉取Python镜像
就像搭积木一样,我们先要有一个基础的Python环境:
# 拉取指定版本的Python镜像
docker pull python:3.10.11
# 💡 测试工程师小贴士:
# 选择具体版本而不是latest,确保测试环境的一致性查看本地镜像库
docker images输出示例:
REPOSITORY TAG IMAGE ID CREATED SIZE
python 3.10.11 c339f65d6ddf 46 hours ago 912MB
yapi latest 7ddf608d0ea6 5 months ago 116MB
mongo 4 af86910c16da 5 months ago 438MB
mysql 5.7 14905234a4ed 5 months ago 495MB
consul latest 6608222931a0 5 months ago 138MB
nacos/nacos-server latest ff28177f850f 5 months ago 1.06GB🎯 测试场景应用: 这个镜像库就像你的测试工具箱,不同的镜像对应不同的测试环境需求。
运行run一个镜像并且进入
--rm 使用完成后,直接删除该镜像
docker run -it --rm python:3.10.11 bash
root@9bf854ed7a7b:/# python
Python 3.10.11 (main, Apr 12 2023, 14:46:22) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
root@9bf854ed7a7b:/# python3
Python 3.10.11 (main, Apr 12 2023, 14:46:22) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
root@9bf854ed7a7b:/# python --version
Python 3.10.11如何运行命令
参考文献:http://www.04007.cn/article/1063.html
docker exec -t 1b8336efd3a4 /bin/bash -c "python --version"
Python 3.10.11
docker exec -it 1b8336efd3a4 python --version
Python 3.10.11实战演练:从零到一部署测试脚本
现在让我们通过一个完整的例子,学习如何使用Dockerfile创建自定义镜像并部署Python测试脚本。
场景设定
假设我们有一个API自动化测试项目,需要部署到Docker容器中运行。
参考资料:
第一步:准备测试环境
# 创建并启动一个Python容器,挂载本地测试代码
docker run -itd --name test-env \
-v /Users/qatestdance/PycharmProjects/test/automation_tests:/workspace \
python:3.10.11
# 💡 解释:
# -itd: 交互式、分配伪终端、后台运行
# --name: 给容器起个好记的名字
# -v: 挂载本地目录到容器内的/workspace项目结构示例
automation_tests/
├── dist/ # 打包后的文件
│ ├── qatest_automation-0.1.3-py3-none-any.whl
│ └── qatest_automation-0.1.3.tar.gz
├── tests/ # 测试用例
│ ├── test_api.py
│ └── test_ui.py
├── requirements.txt # 依赖包
├── Dockerfile # Docker构建文件
└── setup.py # 打包配置
### 创建镜像前准备
需要准备一个安装好的whl包,把相关依赖和脚本导入
```plain
ls
dist ## 这个文件夹里面是测试脚本,打包成了whl
dist
├── qatest_test-0.1.3-py3-none-any.whl
├── qatest_test-0.1.3.tar.gz创建dockerfile文件
touch Dockerfiledockerfile 文件如下:
关于dockerfile的语法,可以参考https://yeasy.gitbook.io/docker_practice/image/build
也可以参考我自己写的笔记
镜像需要如下创建方式:
FROM python:3.10.11
COPY ./dist/* /dist/
RUN pip3 install /dist/qatest_test-0.1.3-py3-none-any.whl创建镜像
docker build -t qatesttest:v1 .docker build -t qatesttest:v1 .
[+] Building 31.9s (8/8) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 139B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/python:3.10.11 0.0s
=> [internal] load build context 0.3s
=> => transferring context: 10.32MB 0.3s
=> [1/3] FROM docker.io/library/python:3.10.11 0.3s
=> [2/3] COPY ./dist/* /dist/ 0.1s
=> [3/3] RUN pip3 install /dist/qatest_test-0.1.3-py3-none-any.whl 28.2s
=> exporting to image 3.1s
=> => exporting layers 3.1s
=> => writing image sha256:0ccd946311c9584a4d3d4c2acf1c96d1c6cf28f4b84bceb3099cffe9794f7f0a 0.0s
=> => naming to docker.io/library/qatesttest:v1 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them镜像生成
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
qatesttest v1 0ccd946311c9 4 minutes ago 1.11GB
python 3.10.11 c339f65d6ddf 5 days ago 912MB
yapi latest 7ddf608d0ea6 5 months ago 116MB
mongo 4 af86910c16da 5 months ago 438MB
mysql 5.7 14905234a4ed 5 months ago 495MB
consul latest 6608222931a0 5 months ago 138MB
nacos/nacos-server latest ff28177f850f 5 months ago 1.06GB镜像导出
docker save -o qatesttestv1.tar qatesttest:v1[out]
ls
qatesttestv1.tar镜像加载
如果实在本地bulit,那么已经有了这个镜像了,不需要再执行这个步骤
docker load -i qatesttestv1.tar如何执行
## 创建容器
docker run -itd --name qatesttest -v /Users/qatestdance/PycharmProjects/test/take_example/container_test:/test qatesttest:v1
e9e2a93063e3eeb0979f9f3607965ac5196d540414ceb32e6abc9cae4486fd81
## 查看容器
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e9e2a93063e3 qatesttest:v1 "python3" 4 minutes ago Up 3 minutes qatesttest
## 执行
docker exec -it e9e2a93063e3eeb09 /bin/bash -c "cd /test && runtest "