前言
如果我们已经安装了一个python3的环境,如果另一台机器也需要安装同样的环境又要敲一遍,很麻烦,这里可以配置Dockerfile文件,让其自动安装,类似shell脚本
Dockerfile编写
在当前目录新建一个文件夹docker-run, cd进入到文件夹,touch新建一个Dockerfile,然后vi打开文件,开始编辑
[root@jkc ~]# mkdir docker-run [root@jkc ~]# cd docker-run/ [root@jkc docker-run]# touch Dockerfile [root@jkc docker-run]# vi Dockerfile
编辑内容如下:
# 1.基于python3.6.8镜像 FROM python:3.6.8 # 2.镜像维护者的姓名和邮箱地址 MAINTAINER jkc# 3.更新pip RUN pip install --upgrade pip --index-url https://pypi.douban.com/simple # 4.指定当前工作目i录 WORKDIR /code # 5.将当年目录文件夹下的所有文件拷贝到code目录 COPY . . # 6.安装第三方包 RUN pip install -r requirements.txt --index-url https://pypi.douban.com/simple # 7.传递参数 ENTRYPOINT ["pytest"] # 8.默认显示帮助信息 CMD ["--help"]
Dockerfile配置详细解释
- FROM:先拉取python3.6.8作为基础镜像,然后在上面修改
- MAINTAINER:填写镜像维护者的姓名和邮箱地址
- RUN:更新pip到最新版本
- WORKDIR:用来指定当前工作目录(或者称为当前目录),容器启动时执行的命令会在该目录下执行,比如接下来要执行pytest test_1.py就会在指定的目录下执行
- COPY:COPY
- RUN:安装requirements.txt文件
- ENTRYPOINT:默认命令行参数pytest
- CMD:与ENTRYPOINT连用,作为ENTRYPOINT的参数,这里默认命令就是pytest --help,如果你想默认执行命令是pytest -s,那么就把CMD改成CMD["-s"]就可以了
此时我们当前目录下还没有requirements.txt文件,接下来创建文件,并写入需要安装的第三方包
[root@jkc docker-run]# ls Dockerfile requirements.txt [root@jkc docker-run]# cat requirements.txt pytest==3.6.3 requests==2.20.1
build构建镜像文件
docker build 命令用于使用 Dockerfile 创建镜像。OPTIONS说明:
-f :指定要使用的Dockerfile路径;
--pull :尝试去更新镜像的新版本;
--quiet, -q :安静模式,成功后只输出镜像 ID;
--tag, -t: 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。
-t参数设置镜像名称jkc_pytest和tag标签名称v1,注意最后面有个点. 这个点代表上下文目录的路径,就是当前路径,我这里是/root/docker_run
docker build -t jkc_pytest:v1 .
[root@jkc docker-run]# docker build -t jkc_pytest:v1 . Sending build context to Docker daemon 7.168kB Step 1/8 : FROM python:3.6.8 ---> 48c06762acf0 Step 2/8 : MAINTAINER jkc---> Running in 986c40cdf6d9 Removing intermediate container 986c40cdf6d9 ---> 157699a7ac69 Step 3/8 : RUN pip install --upgrade pip --index-url https://pypi.douban.com/simple ---> Running in 1420e29b1810 Looking in indexes: https://pypi.douban.com/simple Collecting pip Downloading https://pypi.doubanio.com/packages/de/47/58b9f3e6f611dfd17fb8bd9ed3e6f93b7ee662fb85bdfee3565e8979ddf7/pip-21.0-py3-none-any.whl (1.5MB) Installing collected packages: pip Found existing installation: pip 19.1.1 Uninstalling pip-19.1.1: Successfully uninstalled pip-19.1.1 Successfully installed pip-21.0 Removing intermediate container 1420e29b1810 ---> f77fce861c3d Step 4/8 : WORKDIR /code ---> Running in 0a1229af5f02 Removing intermediate container 0a1229af5f02 ---> 2201ff40156b Step 5/8 : COPY . . ---> 89fd63a6e517 Step 6/8 : RUN pip install -r requirements.txt --index-url https://pypi.douban.com/simple ---> Running in 778b4c1a69e9 Looking in indexes: https://pypi.douban.com/simple Collecting pytest==3.6.3 Downloading https://pypi.doubanio.com/packages/77/64/3a76f6fbb0f392d60c5960f2b2fbad8c2b802dada87ca6d1b99c0083a929/pytest-3.6.3-py2.py3-none-any.whl (195 kB) Collecting requests==2.20.1 Downloading https://pypi.doubanio.com/packages/ff/17/5cbb026005115301a8fb2f9b0e3e8d32313142fe8b617070e7baad20554f/requests-2.20.1-py2.py3-none-any.whl (57 kB) Collecting atomicwrites>=1.0 Downloading https://pypi.doubanio.com/packages/2c/a0/da5f49008ec6e9a658dbf5d7310a4debd397bce0b4db03cf8a410066bb87/atomicwrites-1.4.0-py2.py3-none-any.whl (6.8 kB) Requirement already satisfied: setuptools in /usr/local/lib/python3.6/site-packages (from pytest==3.6.3->-r requirements.txt (line 1)) (41.0.1) Collecting pluggy=0.5 Downloading https://pypi.doubanio.com/packages/ba/65/ded3bc40bbf8d887f262f150fbe1ae6637765b5c9534bd55690ed2c0b0f7/pluggy-0.6.0-py3-none-any.whl (13 kB) Collecting six>=1.10.0 Downloading https://pypi.doubanio.com/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl (10 kB) Collecting attrs>=17.4.0 Downloading https://pypi.doubanio.com/packages/c3/aa/cb45262569fcc047bf070b5de61813724d6726db83259222cd7b4c79821a/attrs-20.3.0-py2.py3-none-any.whl (49 kB) Collecting py>=1.5.0 Downloading https://pypi.doubanio.com/packages/67/32/6fe01cfc3d1a27c92fdbcdfc3f67856da8cbadf0dd9f2e18055202b2dc62/py-1.10.0-py2.py3-none-any.whl (97 kB) Collecting more-itertools>=4.0.0 Downloading https://pypi.doubanio.com/packages/66/58/63146600d8c35188f2c4de3f1e2fe77a54e9c9ac99ac3b652f09e042f637/more_itertools-8.6.0-py3-none-any.whl (45 kB) Collecting idna=2.5 Downloading https://pypi.doubanio.com/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58 kB) Collecting certifi>=2017.4.17 Downloading https://pypi.doubanio.com/packages/5e/a0/5f06e1e1d463903cf0c0eebeb751791119ed7a4b3737fdc9a77f1cdfb51f/certifi-2020.12.5-py2.py3-none-any.whl (147 kB) Collecting chardet=3.0.2 Downloading https://pypi.doubanio.com/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133 kB) Collecting urllib3=1.21.1 Downloading https://pypi.doubanio.com/packages/01/11/525b02e4acc0c747de8b6ccdab376331597c569c42ea66ab0a1dbd36eca2/urllib3-1.24.3-py2.py3-none-any.whl (118 kB) Installing collected packages: urllib3, six, py, pluggy, more-itertools, idna, chardet, certifi, attrs, atomicwrites, requests, pytest Successfully installed atomicwrites-1.4.0 attrs-20.3.0 certifi-2020.12.5 chardet-3.0.4 idna-2.7 more-itertools-8.6.0 pluggy-0.6.0 py-1.10.0 pytest-3.6.3 requests-2.20.1 six-1.15.0 urllib3-1.24.3 Removing intermediate container 778b4c1a69e9 ---> 8dbb604ee32b Step 7/8 : ENTRYPOINT ["pytest"] ---> Running in 9f8af21deb12 Removing intermediate container 9f8af21deb12 ---> bc1569590d44 Step 8/8 : CMD ["--help"] ---> Running in f4a600409184 Removing intermediate container f4a600409184 ---> f7033ca36724 Successfully built f7033ca36724 Successfully tagged jkc_pytest:v1 [root@jkc docker-run]#
运行过程中可以看到按步骤运行,如:Step 1/8
运行完成后,可以通过docker images查看生成的镜像
[root@jkc docker-run]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE jkc_pytest v1 f7033ca36724 8 minutes ago 944MB
run运行容器
在当前目录新建一个test_1.py文件,写入pytest测试脚本
import pytest def test_one(): print("正在执行----test_one") x = "this" assert 'h' in x def test_two(): print("正在执行----test_two") x = "hello" assert x def test_three(): print("正在执行----test_three") a = "hello" b = "hello world" assert a in b if __name__ == "__main__": pytest.main(["-s", "test_h.py"])
使用docker run运行容器
- -it -t让docker分配一个伪终端并绑定到容器的标准输入上, -i则让容器的标准输入保持打开.
- --rm 容器退出时,自动清除容器。 --rm选项不能与-d同时使用
- -v 将容器的工作目录/code挂载到宿主机的$PWD,也就是当前目录
- jkc_pytest:v1 容器名称和tag名称
- test_1.py 后面跟着需要执行的脚本名称
[root@jkc docker-run]# docker run -it --rm -v "$PWD":/code jkc_pytest:v1 test_1.py -s ====================================================== test session starts ====================================================== platform linux -- Python 3.6.8, pytest-3.6.3, py-1.10.0, pluggy-0.6.0 rootdir: /code, inifile: collected 3 items test_1.py 正在执行----test_one .正在执行----test_two .正在执行----test_three . =================================================== 3 passed in 0.04 seconds ==================================================== [root@jkc docker-run]#