Dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本。
简单来说,Dockerfile就是把我们安装环境的每个步骤和指令,放到一个文件,最后一键执行,最后做成一个你想要的环境。
Dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本。
Docker构建三步曲:
Dockerfile 是一个包含创建镜像所有命令的文本文件,通过docker build命令可以根据 Dockerfile 的内容构建镜像,
在介绍如何构建之前先介绍下 Dockerfile 的基本语法结构。
Dockerfile 有以下指令选项:
在当前目录新建一个文件夹docker-run, cd进入到文件夹,touch新建一个Dockerfile,然后vi打开文件,开始编辑
# 基于python3.6.8镜像
FROM python:3.6.8MAINTAINER leslie <327403110@qq.com>
# 更新pip
RUN pip install --update pip# 工作目录
WORKDIR /codeADD . /code
# pip安装依赖包
RUN pip install -r requirements.txt# 传递参数
ENTRYPOINT ["pytest"]# 默认显示help帮助信息
CMD ["--help"]
requirements.txt是python的相关依赖包, 可以通过freeze命令生成:
注意你的pip 还是pip3
我是从win上导出来
pip3 freeze >requirements.txt
asn1crypto==0.24.0
backports.csv==1.0.7
beautifulsoup4==4.6.3
certifi==2018.10.15
cffi==1.11.5
chardet==3.0.4
Click==7.0
configparser==3.5.0
construct==2.5.3
cryptography==2.3.1
ddt==1.2.0
defusedxml==0.6.0
diff-match-patch==20181111
Django==2.1.3
django-bootstrap3==11.0.0
Django-Bootstrap3-Validator==0.3.3
django-crispy-forms==1.7.2
django-formtools==1.0
django-haystack==2.8.1
django-import-export==1.2.0
django-pure-pagination==0.3.0
django-reversion==3.0.4
et-xmlfile==1.0.1
Flask==1.0.2
future==0.15.2
gevent==1.3.7
greenlet==0.4.15
httplib2==0.9.2
idna==2.7
itsdangerous==1.1.0
jdcal==1.4.1
jieba==0.39
Jinja2==2.10
locust==0.0
locustio==0.9.0
lxml==4.2.5
Markdown==3.1
MarkupSafe==1.1.0
msgpack==0.5.6
odfpy==1.4.0
openpyxl==2.6.2
pefile==2019.4.18
pycparser==2.19
pycryptodome==3.7.0
pymssql==2.1.4
PyMySQL==0.9.2
python-ptrace==0.9.3
pytest=5.1.2
pytz==2018.7
PyYAML==5.1.1
pyzmq==17.1.2
requests==2.20.0
selenium==3.141.0
six==1.10.0
tablib==0.13.0
urllib3==1.24.1
Werkzeug==0.14.1
Whoosh==2.7.4
xlrd==1.2.0
xlwt==1.3.0
docker build 命令用于使用 Dockerfile 创建镜像。OPTIONS说明:
-f :指定要使用的Dockerfile路径;
--pull :尝试去更新镜像的新版本;
--quiet, -q :安静模式,成功后只输出镜像 ID;
--tag, -t: 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。
-t参数设置镜像名称yoyo_pytes和tag标签名称v1,注意最后面有个点.
docker build -t leslie_pytest:v1 .
运行过程中可以看到按步骤运行,如:Step 1/8
运行完成后,可以通过docker images查看生成的镜像
[root@bogon docker-run]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
leslie_pytest v1 377c71839392 9 minutes ago 1.15GB
<none> <none> fc6ec955e6ec 13 minutes ago 936MB
centos 7.5 a7b3006aa711 29 hours ago 202MB
tomcat latest 96c4e536d0eb 13 days ago 506MB
mysql 5.7 e1e1680ac726 3 weeks ago 373MB
mongo 3.4 ab4287b7a939 4 weeks ago 428MB
python 3.6.8 48c06762acf0 2 months ago 924MB
easymock/easymock 1.6.0 193a7b904d4f 3 months ago 699MB
redis 4.0.6 1e70071f4af4 21 months ago 107MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
[root@bogon docker-run]#
在当前目录新建一个test_h.py文件,写入pytest测试脚本
# coding=utf-8 __author__ = "leslie" 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", "pytestTest.py"])
使用docker run运行容器
docker run -it --rm -v "$PWD":/code leslie_pytest:v1 pytestTest.py -s
[root@bogon docker-run]# docker run -it --rm -v "$PWD":/code leslie_pytest:v1 pytestTest.py -s
============================================== test session starts ===============================================
platform linux -- Python 3.6.8, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: /code
collected 3 itemspytestTest.py 正在执行----test_one
.正在执行----test_two
.正在执行----test_three
.=============================================== 3 passed in 0.03s ================================================
[root@bogon docker-run]#
原文:https://www.cnblogs.com/leslie003/p/11460415.html