通过dockerfile制作镜像
Dockerfile是一个用于构建Docker镜像的文本文件,其中包含了创建Docker镜像的全部指令。就是将我们安装环境的每个步骤使用指令的形式存放在一个文件中,最后生成一个需要的环境。
Docker使用Dockerfile构建镜像步骤:
- 创建dockerfile文件
- 通过build命令生成image镜像文件
- 运行容器
- 上传仓库
创建dockerfile文件
1、创建dockerfile
新建一个文件夹,api-demo
然后进入api-demo,创建dockerfile
TynamYang@XA-LSR-Tynam MINGW64 /d/docker/Docker Toolbox $ mkdir api—-demo $ cd api—-demo/ $ touch Dockerfile $ vi Dockerfile
编写Dockerfile文件如下:
# 基于基础镜像 FROM python:3.6 # 设置code文件夹是工作目录 WORKDIR /code # 将代码添加到code文件夹下 COPY test_api.py /code/ # 更新pip RUN pip install --upgrade pip --index-url https://pypi.douban.com/simple # pip安装依赖包 RUN pip install -U pytest RUN pip install -U requests # 执行命令 CMD ["python", "test_api.py"]
2、dockerfile常用指令
Dockerfile 语法:
Dockerfile语法由两部分构成,注释和命令+参数
示例:
# 基于基础镜像 FROM python:3.6
常用指令如下
ADD
ADD命令有两个参数,源和目标。它的基本作用是从源系统的文件系统上复制文件到目标容器的文件系统。如果源是一个URL,那该URL的内容将被下载并复制到容器中。
# Usage: ADD [source directory or URL] [destination directory]
ADD /my_app_folder /my_app_folder
COPY
用于将文件作为一个新的层添加到镜像中。通常使用 COPY 指令将应用代码赋值到镜像中。
COPY test_api.py /code/
CMD
和RUN命令相似,CMD可以用于执行特定的命令。和RUN不同的是,这些命令不是在镜像构建的过程中执行的,而是在用镜像构建容器后被调用。
CMD ["python", "test_api.py"]
ENTRYPOINT
配置容器启动后执行的命令,并且不可被 docker run 提供的参数覆盖。
每个 Dockerfile 中只能有一个 ENTRYPOINT,当指定多个时,只有最后一个起效。
ENTRYPOINT 帮助你配置一个容器使之可执行化,如果你结合CMD命令和ENTRYPOINT命令,你可以从CMD命令中移除“application”而仅仅保留参数,参数将传递给ENTRYPOINT命令。
# Usage: ENTRYPOINT application "argument", "argument", .. # Remember: arguments are optional. They can be provided by CMD # or during the creation of a container. ENTRYPOINT echo # Usage example with CMD: # Arguments set with CMD can be overridden during *run* CMD "Hello docker!" ENTRYPOINT echo
ENV
ENV命令用于设置环境变量。这些变量以”key=value”的形式存在,并可以在容器内被脚本或者程序调用。这个机制给在容器中运行应用带来了极大的便利。
# Usage: ENV key value ENV SERVER_WORKS 4
EXPOSE
EXPOSE用来指定端口,使容器内的应用可以通过端口和外界交互。
# Usage: EXPOSE [port] EXPOSE 8080
FROM
FROM命令可能是最重要的Dockerfile命令。改命令定义了使用哪个基础镜像启动构建流程。基础镜像可以为任意镜 像。如果基础镜像没有被发现,Docker将试图从Docker image index来查找该镜像。FROM命令必须是Dockerfile的首个命令。
# Usage: FROM [image name]
FROM ubuntu
MAINTAINER
我建议这个命令放在Dockerfile的起始部分,虽然理论上它可以放置于Dockerfile的任意位置。这个命令用于声明作者,并应该放在FROM的后面。
# Usage: MAINTAINER [name]
MAINTAINER authors_name
RUN
RUN命令是Dockerfile执行命令的核心部分。它接受命令作为参数并用于创建镜像。不像CMD命令,RUN命令用于创建镜像(在之前commit的层之上形成新的层)。
# pip安装依赖包
RUN pip install -U pytest
USER
USER命令用于设置运行容器的UID。
# Usage: USER [UID] USER 751
VOLUME
VOLUME命令用于让你的容器访问宿主机上的目录。
# Usage: VOLUME ["/dir_1", "/dir_2" ..] VOLUME ["/my_files"]
WORKDIR
WORKDIR命令用于设置CMD指明的命令的运行目录。
# Usage: WORKDIR /path
WORKDIR ~/
3、创建python文件
创建python文件 test_api.py,在这做一个简单的接口测试,一个成功的测试用例和一个失败的测试用例,代码如下:
# coding=utf-8 import requests import pytest def test_1(): search_url = 'https://baidu.com/' respose = requests.get(url=search_url) assert 200 == respose.status_code def test_2(): search_url = 'https://baidu.com/' respose = requests.get(url=search_url) assert 400 == respose.status_code if __name__ == "__main__": pytest.main(["-s", "test_api.py"])
通过build构建镜像文件
使用docker build 命令创建镜像。
使用命令:(注意命令最后面的点)
docker build -t tynam/api-demo:v1.1 .
OPTIONS说明:
-
-
--tag, -t: 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。例如 tynam/api-demo:v1.1
-
TynamYang@XA-LSR-Tynam MINGW64 /d/docker/Docker Toolbox/api—-demo $ docker build -t tynam/api-demo:v1.1 . Sending build context to Docker daemon 3.072kB Step 1/7 : FROM python:3.6 ---> 0db2e2bbf438 Step 2/7 : WORKDIR /code ---> Using cache ---> 688dd9856f6e Step 3/7 : COPY test_api.py /code/ ---> 87e29f28e665 Step 4/7 : RUN pip install --upgrade pip --index-url https://pypi.douban.com/simple ---> Running in 14f5c424c164 Looking in indexes: https://pypi.douban.com/simple Requirement already up-to-date: pip in /usr/local/lib/python3.6/site-packages (20.0.2) Removing intermediate container 14f5c424c164 ---> fbdb47946e97 Step 5/7 : RUN pip install -U pytest ---> Running in 6ffb142f983d Collecting pytest Downloading pytest-5.4.1-py3-none-any.whl (246 kB) Collecting wcwidth Downloading wcwidth-0.1.9-py2.py3-none-any.whl (19 kB) Collecting importlib-metadata>=0.12; python_version < "3.8" Downloading importlib_metadata-1.6.0-py2.py3-none-any.whl (30 kB) Collecting attrs>=17.4.0 Downloading attrs-19.3.0-py2.py3-none-any.whl (39 kB) Collecting more-itertools>=4.0.0 Downloading more_itertools-8.2.0-py3-none-any.whl (43 kB) Collecting pluggy<1.0,>=0.12 Downloading pluggy-0.13.1-py2.py3-none-any.whl (18 kB) Collecting packaging Downloading packaging-20.3-py2.py3-none-any.whl (37 kB) Collecting py>=1.5.0 Downloading py-1.8.1-py2.py3-none-any.whl (83 kB) Collecting zipp>=0.5 Downloading zipp-3.1.0-py3-none-any.whl (4.9 kB) Collecting pyparsing>=2.0.2 Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB) Collecting six Downloading six-1.14.0-py2.py3-none-any.whl (10 kB) Installing collected packages: wcwidth, zipp, importlib-metadata, attrs, more-itertools, pluggy, pyparsing, six, packaging, py, pytest Successfully installed attrs-19.3.0 importlib-metadata-1.6.0 more-itertools-8.2.0 packaging-20.3 pluggy-0.13.1 py-1.8.1 pyparsing-2.4.7 pytest-5.4.1 six-1.14.0 wcwidth-0.1.9 zipp-3.1.0 Removing intermediate container 6ffb142f983d ---> 0fe23e1c2a89 Step 6/7 : RUN pip install -U requests ---> Running in 21163a3acd67 Collecting requests Downloading requests-2.23.0-py2.py3-none-any.whl (58 kB) Collecting certifi>=2017.4.17 Downloading certifi-2020.4.5.1-py2.py3-none-any.whl (157 kB) Collecting chardet<4,>=3.0.2 Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB) Collecting idna<3,>=2.5 Downloading idna-2.9-py2.py3-none-any.whl (58 kB) Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 Downloading urllib3-1.25.9-py2.py3-none-any.whl (126 kB) Installing collected packages: certifi, chardet, idna, urllib3, requests Successfully installed certifi-2020.4.5.1 chardet-3.0.4 idna-2.9 requests-2.23.0 urllib3-1.25.9 Removing intermediate container 21163a3acd67 ---> 8a8ae3f778a5 Step 7/7 : CMD ["python", "test_api.py"] ---> Running in f7310b052d08 Removing intermediate container f7310b052d08 ---> 308b0f67af64 Successfully built 308b0f67af64 Successfully tagged tynam/api-demo:v1.1 SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
在运行过程中,可以看到运行步骤,及运行的指令。例如第五步:Step 5/7 : RUN pip install -U pytest
镜像创建完成后可以使用命令查看创建的镜像:
TynamYang@XA-LSR-Tynam MINGW64 /d/docker/Docker Toolbox/api—-demo $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE tynam/api-demo v1.1 308b0f67af64 12 minutes ago 927MB
运行容器
镜像创建完成后开始运行,使用命令;
docker run -it tynam/api-demo:v1.1
运行结果如下:
TynamYang@XA-LSR-Tynam MINGW64 /d/docker/Docker Toolbox/api—-demo $ docker run -it tynam/api-demo:v1.1 ================================================= test session starts ================================================== platform linux -- Python 3.6.10, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /code collected 2 items test_api.py .F ======================================================= FAILURES ======================================================= ________________________________________________________ test_2 ________________________________________________________ def test_2(): search_url = 'https://baidu.com/' respose = requests.get(url=search_url) > assert 400 == respose.status_code E assert 400 == 200 E + where 200 = <Response [200]>.status_code test_api.py:19: AssertionError =============================================== short test summary info ================================================ FAILED test_api.py::test_2 - assert 400 == 200 ============================================= 1 failed, 1 passed in 0.61s ============================================== TynamYang@XA-LSR-Tynam MINGW64 /d/docker/Docker Toolbox/api—-demo
上传仓库
docker拥有自己的镜像仓库,官方仓库网站是 https://hub.docker.com/
镜像上传仓库的流程为:登录>>tag操作>>push
1、创建账号
进入docker hub https://hub.docker.com/ 注册账号,然后创建自己的仓库
例如笔者创建的仓库名为 api
2、本地登录docker
使用命令登录自己的docker
docker login
示例:
TynamYang@XA-LSR-Tynam MINGW64 /d/docker/Docker Toolbox $ docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username (tynam): tynam Password: Login Succeeded
3、修改镜像名
修改镜像名和仓库名一致,镜像名称必须和 hub 中的仓库名称一致,否则将会抛出错误, 使用命令:
docker tag IMAGEID(镜像id) REPOSITORY:TAG(仓库:标签)
示例:
TynamYang@XA-LSR-Tynam MINGW64 /d/docker/Docker Toolbox/api—-demo $ docker tag 308b0f67af64 tynam/api:v1.1
修改后查看镜像
TynamYang@XA-LSR-Tynam MINGW64 /d/docker/Docker Toolbox/api—-demo $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE tynam/api-demo v1.1 308b0f67af64 16 minutes ago 927MB tynam/api v1.1 308b0f67af64 16 minutes ago 927MB
4、上传镜像
docker push [REPOSITORY]:[TAG]
示例:
TynamYang@XA-LSR-Tynam MINGW64 /d/docker/Docker Toolbox $ docker push tynam/api:v1.1 The push refers to repository [docker.io/tynam/api] de38accaade6: Pushed 52574f25c16c: Pushed c2e450965650: Pushed 86dd60275049: Pushed 58f7d369eb64: Pushed 7e8b1f6898b0: Mounted from tynam/test e91ff66f967f: Mounted from tynam/test e05388744d34: Mounted from tynam/test d4aeb4b1e8cc: Mounted from tynam/test 46829331b1e4: Mounted from tynam/test d35c5bda4793: Mounted from tynam/test a3c1026c6bcc: Mounted from tynam/test f1d420c2af1a: Mounted from tynam/test 461719022993: Mounted from tynam/test v1.1: digest: sha256:c821d6afcfb43cc03b47bf3e0241d04472f4ecc83455dd5b6a3acc6a1b5b3276 size: 3265
上传完成后进入自己的仓库可以查看上传结果