0%

docker之Dockerfile

Dockerfile

  • 本文搭建步骤来自这里

  • Dockerfile 可以自定义编写镜像,简单来说就类似写shell脚本

  • Dockerfile命令科普

关键字 说明
FROM 基础镜像,当前新镜像是基于哪个镜像的
MAINTAINER 镜像维护者的姓名和邮箱地址
RUN 容器构建时需要运行的命令
EXPOSE 当前容器对外暴露出的端口
WORKDIR 指定在创建容器后,终端默认登陆的进来工作目录,一个落脚点
ENV 用来在构建镜像过程中设置环境变量
ADD 将宿主机目录下的文件拷贝进镜像且 ADD 命令会自动处理 URL 和解压 tar 压缩包
COPY 类似 ADD,拷贝文件和目录到镜像中。(COPY src dest 或 COPY [“src”,”dest”])
VOLUME 容器数据卷,用于数据保存和持久化工作
CMD 指定一个容器启动时要运行的命令,Dockerfile 中可以有多个 CMD 指令,但只有最后一个生效,CMD 会被 docker run 之后的参数替换
ENTRYPOINT 指定一个容器启动时要运行的命令,ENTRYPOINT 的目的和 CMD 一样,都是在指定容器启动程序及参数
ONBUILD 当构建一个被继承的 Dockerfile 时运行命令,父镜像在被子继承后父镜像的 onbuild 被触发
  • 网上的图片介绍的还不错,仅供参考

image-20220322162743943

本地代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import requests
import pytest


@pytest.mark.finished
def test_add():
print("测试函数:test_add")


@pytest.mark.finished
def test_subtract():
print("测试函数:test_subtract")


@pytest.mark.finished
def test_request():
res = requests.get("http://www.baidu.com")
assert res.status_code == 201


@pytest.mark.unfinished
def test_no_finish():
pass


if __name__ == "__main__":
pytest.main(["-s", "test.py"])
  • 导出依赖文件
1
2
pip install pipreqs
pipreqs --use-local --encoding=utf8 --force .
  • 得到依赖文件(requirements.txt)如下
1
2
pytest==6.2.4
requests==2.24.0

服务配置

1
2
3
4
5
6
7
8
9
[root@VM-24-13-centos data]# pwd
/usr/local/docker-demo/data

[root@VM-24-13-centos data]# ll
total 16
-rw-r--r-- 1 root root 264 Mar 22 17:47 Dockerfile
drwxr-xr-x 2 root root 4096 Mar 22 17:57 __pycache__
-rw-r--r-- 1 root root 33 Mar 22 17:36 requirements.txt # 依赖文件
-rw-r--r-- 1 root root 467 Mar 22 17:36 test.py # 源代码
  • 编写Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
vi Dockerfile
# 基础镜像
FROM rackspacedot/python37
# 作者信息
MAINTAINER gsxl <xxxx@qq.com>
# 升级pip
RUN pip install --upgrade pip
# 容器的工作目录
WORKDIR /python
# 把依赖文件拷贝到容器的工作目录
COPY requirements.txt /python
# 安装容器工作目录下的依赖文件
RUN pip install -r /python/requirements.txt -i https://pypi.douban.com/simple
# 指定容器启动程序及参数
ENTRYPOINT ["pytest"]
CMD ["--help"]
  • 编译dockerfile build 运行(注意后面有个 . ):dcoker build -t 镜像:标签 .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@VM-24-13-centos data]# docker build -t test_py:v1 .
Sending build context to Docker daemon 4.096kB
Step 1/8 : FROM rackspacedot/python37
---> 4513b6d75e1c
Step 2/8 : MAINTAINER gsxl <xxxx@qq.com>
---> Using cache
---> 97ee10a89cc0
Step 3/8 : RUN pip install --upgrade pip
---> Using cache
---> 7e71f8ad49e6
Step 4/8 : WORKDIR /python
---> Using cache
---> 961389b2c6d2
Step 5/8 : COPY requirements.txt /python
---> 26d36133bc07
Step 6/8 : RUN pip install -r /python/requirements.txt -i https://pypi.douban.com/simple
---> Running in 8d36f9506780
Looking in indexes: https://pypi.douban.com/simple
Collecting pytest==6.2.4
Downloading https://pypi.doubanio.com/packa
Successfully built 1473b91e578e
Successfully tagged test_py:v1
  • 编译dockerfile成功后,会生成镜像文件,下面的test_py 就是刚刚生成的
1
2
3
4
5
6
[root@VM-24-13-centos data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test_py v1 1473b91e578e 25 seconds ago 1.05GB
rackspacedot/python37 latest 4513b6d75e1c 4 weeks ago 1.03GB
mysql 5.7 c20987f18b13 3 months ago 448MB
hello-world latest feb5d9fea6a5 5 months ago 13.3kB
  • 开启测试:docker run -it --rm -v /usr/local/docker-demo/data:/python test_py:v1 test.py -s
  • -it :以交互模式运行容器
  • -v:挂载目录/usr/local/docker-demo/data到 容器内python文件夹
  • --rm:容器退出时,自动清除容器
  • test_py:v1:镜像:标签
  • test.py 就是源代码文件,因为-v挂载到了本地,使用pytest就能执行到本地源代码
  • -s:pytest 详细信息
1
2
3
4
5
[root@VM-24-13-centos data]# docker run -it --rm -v /usr/local/docker-demo/data:/python test_py:v1 test.py -s

====================================================== short test summary info ======================================================
FAILED test.py::test_request - assert 200 == 201
============================================== 1 failed, 3 passed, 4 warnings in 0.12s ==========