dockerfile制作镜像文件
Dockerfile:
简言之,就是把我们安装环境的每个步骤和指令,放到一个文件,最后一键执行,最后做成一个你想要的环境;也可以轻松的迁移dockerfile到其他机器来实现镜像的迁移。
Dockerfile相关指令
Dockerfile 有以下指令选项:
- FROM 基础镜像,当前新镜像是基于哪个镜像的
- MAINTAINER 镜像维护者的姓名和邮箱地址
- RUN 容器构建时需要运行的命令
- CMD 指定一个容器启动时要运行的命令。dockerfile中可以有多个CMD指令,但只有最后一个生效,CMD会被docker run之后的参数替换。
- EXPOSE 当前容器对外暴露的端口号
- ENV 用来在构建镜像过程中设置环境变量
- ADD 将宿主机目录下的文件拷贝到镜像里面并且ADD命令会自动处理URL和解压tar压缩包
- COPY COPY:类似ADD,拷贝文件和目录到镜像中,但是它只是拷贝,不会自动处理URL和解压tar压缩包。
- ENTRYPOINT 指定一个容器启动时要运行的命令。ENTRYPOIT的目的和CMD一样,都是在指定容器启动程序及参数。
- VOLUME 容器数据卷,用于数据保存和持久化工作
- USER 指定运行容器时的用户名或UID,后续的 RUN 也会使用指定用户
- WORKDIR 指定在容器创建后,终端默认登录进来工作目录,一个落脚点
- ONBUILD 当构建一个被继承的Dockerfile时运行命令,父镜像在被子继承后,父镜像的onbuild被触发。
Dokcerfile编写
当前目录新建docker-run,cd到新建目录,touch Dockerfile ,编辑Dockerfile文件
[root@xiaoxiao ~]# mkdir docker-run [root@xiaoxiao ~]# cd docker-run/ [root@xiaoxiao docker-run]# touch Dockerfile [root@xiaoxiao docker-run]# vi Dockerfile [root@xiaoxiao docker-run]#
里面的内容如下:
# 基于python3.6.8镜像 FROM python:3.6.8 MAINTAINER xiao <2933290293@qq.com> # 更新pip RUN pip install --upgrade pip --index-url https://pypi.tuna.tsinghua.edu.cn/simple/# 工作目录 WORKDIR /code ADD . /code # pip安装依赖包 RUN pip install -r requirements.txt --index-url https://pypi.tuna.tsinghua.edu.cn/simple/# 传递参数 ENTRYPOINT ["pytest"] # 默认显示help帮助信息 CMD ["--help"]
requirements.txt
requirements.txt是python的相关依赖包, 可以通过freeze命令生成
pip3 freeze >requirements.txt
因为用命令生成,很多依赖需要安装,我这里直接touch和vi的
[root@xiaoxiao docker-run]# touch requirements.txt [root@xiaoxiao docker-run]# vi requirements.txt [root@xiaoxiao docker-run]# cat requirements.txt pytest==3.6.3 requests==2.20.1 [root@xiaoxiao docker-run]#
build构建文件镜像
docker build 命令用于使用 Dockerfile 创建镜像。OPTIONS说明:
-
-f :指定要使用的Dockerfile路径;
-
--pull :尝试去更新镜像的新版本;
-
--quiet, -q :安静模式,成功后只输出镜像 ID;
-
--tag, -t: 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。
-t参数设置镜像名称xiao_pytes和tag标签名称v1,注意最后面有个点.
docker build -t xiao_pytest:v1 .
[root@xiaoxiao docker-run]# docker build -t xiao_pytest:v1 . Sending build context to Docker daemon 3.072kB Step 1/7 : FROM python:3.6.8 ---> 48c06762acf0 Step 2/7 : MAINTAINER xiao <2933290293@qq.com> ---> Using cache ---> 77d255d9a222 Step 3/7 : WORKDIR /code ---> Using cache ---> 54799f933569 Step 4/7 : ADD . /code ---> 1d86d3758265 Step 5/7 : RUN pip install -r requirements.txt --index-url https://pypi.mirrors.ustc.edu.cn/simple/ ---> Running in 0a6d472f5d1c Looking in indexes: https://pypi.mirrors.ustc.edu.cn/simple/ Collecting selenium==2.53.6 (from -r requirements.txt (line 1)) Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/3f/f8/e46684764161fee7b7d2e0fd9b82d56915438b68e498181dc45183643c82/selenium-2.53.6-py2.py3-none-any.whl (884kB) Collecting requests==2.20.1 (from -r requirements.txt (line 2)) WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.mirrors.ustc.edu.cn', port=443): Read timed out. (read timeout=15)",)': /simple/requests/ ...
Removing intermediate container 0a6d472f5d1c ---> fe4bfec655df Step 6/7 : ENTRYPOINT ["pytest"] ---> Running in faf6d701ee68 Removing intermediate container faf6d701ee68 ---> 9529cc54882e Step 7/7 : CMD ["--help"] ---> Running in bf88fb88e78b Removing intermediate container bf88fb88e78b ---> b4539e67b69e Successfully built b4539e67b69e Successfully tagged xiao_pytest:v1
docker images查看生成的镜像
[root@xiaoxiao docker-run]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE xiao_pytest v1 b4539e67b69e 2 minutes ago 935MB
run运行容器
在当前目录新建一个test_h.py文件,写入pytest测试脚本
#encoding:utf-8 #@Time:2020/3/10 15:00 #@Author:sunny import pytest def test_a(): print("正在执行第一个测试用例") a="apple" assert 'a' in a def test_b(): print("正在执行第二个测试用例") b="banan" assert 'b' in b def test_c(): print("正在执行第三个测试用例") c='h' d="hello" assert c in d if __name__ == '__main__': pytest.main(["-s"],"test_a.py")
使用docker run运行容器
- -it -t让docker分配一个伪终端并绑定到容器的标准输入上, -i则让容器的标准输入保持打开.
- --rm 容器退出时,自动清除容器。 --rm选项不能与-d同时使用
- -v 将容器的工作目录/code挂载到宿主机的$PWD,也就是当前目录
- xiao_pytest:v1 容器名称和tag名称
- test_a.py 后面跟着需要执行的脚本名称
[root@xiaoxiao docker-run]# docker run -it --rm -v "$PWD":/code xiao_pytest:v1 test_a.py -s =============================================== test session starts =============================================== platform linux -- Python 3.6.8, pytest-3.6.3, py-1.8.1, pluggy-0.6.0 rootdir: /code, inifile: collected 3 items test_a.py 正在执行第一个测试用例 .正在执行第二个测试用例 .正在执行第三个测试用例 . ============================================ 3 passed in 0.03 seconds ============================================= [root@xiaoxiao docker-run]#
技术参考:https://www.cnblogs.com/yoyoketang/p/11397597.html
善于跌倒仍喜爱奔跑~

浙公网安备 33010602011771号