Foreword
Dockerfile can be custom-written image, in short, it is like writing scripts, shell scripts should be a lot of contacts, is executed after a completion.
Of course, you can also do a good job of mirroring a mirror spread to warehouses, storage like github as above, a command will be able to copy down the installation.
I. Introduction
I believe many of my friends will ask, why docker run about python stopped it? (That does not stop execution over what?)
And why my pip installation, each installation is complete run python container next to the library is not already installed? Clearly run is activated every time a new container. So many users advocate making use Dockerfile.
1, learning just three steps:
- Write dockerfile
- docker build image mirror
- containers docker run run
2, Dockerfile science command:
FROM: base image, a new image is based on which the current mirror
MAINTAINER: Mirror defenders name and e-mail address
RUN: Run command is required when building container
EXPOSE: Foreign exposed current container port
WORKDIR: After you create a designated container terminal default landing came in the working directory, a foothold
ENV: to set the environment variable to build the mirror process
ADD: the host file in the directory image and copied into the ADD command processing URL and automatically extract the tar archive
COPY: Similar ADD, copy files and directories to the mirror. (COPY src dest or COPY [ "src", "dest"])
VOLUME: container volume data, for data storage and persistence work
CMD: Specifies the command to start a container to run, Dockerfile can have multiple CMD command, but only the last one will work, CMD parameter will be replaced after the docker run
ENTRYPOINT: Specifies the command to start a container to run, ENTRYPOINT purpose and CMD, are in the specified container and start the program parameters
ONBUILD: Run command when building an inherited Dockerfile, the father of the mirror is triggered after the quilt in his father's image of onbuild
3, the pictures online presentation is also good, for reference only:
Second, the custom began making python3.6.5
Installation python3, and install the library in requirements.txt file.
1、导出 requirements.txt:pip3 freeze >requirements.txt
[root@iZbp19lugf22zbsubsf1y6Z data]# pip3 freeze >requirements.txt [root@iZbp19lugf22zbsubsf1y6Z data]# ll total 8 -rw-r--r-- 1 root root 469 Jan 3 20:09 Dockerfile -rw-r--r-- 1 root root 1107 Jan 3 20:10 requirements.txt [root@iZbp19lugf22zbsubsf1y6Z data]# cat requirements.txt allure-pytest==2.8.6 allure-python-commons==2.8.6 amqp==1.4 . . 9 anyjson == 0.3 . . 3 appdirs == 1.4 . . 3 attrs == 19.3 . 0 Babel == 2.7 . 0 bcrypt == 3.1 . . 7 ... etc.
2, create a file: vi dockerfile
[root@iZbp19lugf22zbsubsf1y6Z data]# vi Dockerfile
3, write dockerfile, save
Base image #:. To python3 . 6 .5 mirroring the FROM Python: 3.6 . . 5 # defenders mirror name and email address the MAINTAINER on gsxl < 772 262 624 @ qq.com> command to run when the construct #, updates pip, pip for later installation requirements .txt RUN PIP install - upgrade PIP # working directory WORKDIR / Python the ADD. / Python # PIP installation requirements.txt RUN PIP install - r requirements.txt # specified container and start the program parameters EntryPoint [ " pytest " ] # default display help help CMD [ " --help "]
4, build runs (note that there is the back . ): Build dcoker mirror -t: label.
Mirroring name and label == test_py: v1
[root@iZbp19lugf22zbsubsf1y6Z data]# docker build -t test_py:v1 .
Step 8, the following results:
root@iZbp19lugf22zbsubsf1y6Z data]# docker build -t test_py:v1 . Sending build context to Docker daemon 4.096kB Step 1/8 : FROM python:3.6.5 ---> 9a58cce9b09f Step 2/8 : MAINTAINER gsxl <772262624@qq.com> ---> Using cache ---> ef78f308960d Step 3/8 : RUN pip install --upgrade pip ---> Using cache ---> fcd21328b7d4 Step 4/8 : WORKDIR /python ---> Running in 70727434375c Removing intermediate container 70727434375c ---> 89e7bb9cd6b5 Step 5/8 : ADD . /python ---> 32b2714720b4 Step 6/8 : RUN pip install -r requirements.txt ---> Running in 7a1727fa9b6f Collecting allure-pytest==2.8.6 Downloading https://files.pythonhosted.org/packages/8d/be/cc4877a9e2f998bbfd0289b6a2ffac153126b9c8f843ea733cd14371b7e3/allure_pytest-2.8.6-py2.py3-none-any.whl Collecting allure-python-commons==2.8.6 Downloading https://files.pythonhosted.org/packages/1a/7d/cd568f55d54012891ecea14f70ec3d8f2471f038f53df7a828f4b3e5080d/allure_python_commons-2.8.6-py2.py3-none-any.whl Collecting amqp==1.4.9...此处省略 Successfully built 18ccc91cdaf2 Successfully tagged test_py:v1
5. View Mirror: docker images
[root@iZbp19lugf22zbsubsf1y6Z data]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test_py v1 18ccc91cdaf2 18 minutes ago 1.07GB
Third, start the container
1、docker run -it --rm -v ~/data:/python test_py:v1 api_excel_demo/cases/test_register_api.py -s
- -it: Run vessel in interactive mode
- -v: mount directory ~ / data folder into the container python
- --rm: exit the container, the container is automatically cleared
- test_py: v1: Mirror: Label
- -s: pytest Details
[root@iZbp19lugf22zbsubsf1y6Z data]# docker run -it --rm -v ~/data:/python test_py:v1 api_excel_demo/cases/test_register_api.py -s ============================================================================================================================ test session starts ============================================================================================================================= platform linux -- Python 3.6.5, pytest-5.3.2, py-1.8.1, pluggy-0.13.1 rootdir: /python plugins: allure-pytest- 2.8 . . 6 Collected . 3 items api_excel_demo /cases/test_register_api.py { ' the Content-the Type ' : ' file application / JSON ' , ' X-Requested-With- ' : ' the XMLHttpRequest ' } ******* being executed use Case: ----- ID: . 1 request method: post, a request URL: HTTP: //47.97.194.84:8000/api/register/ type of request: json, body parameters: { " Account " : " gsxl11 " , " In Email " : " [email protected] " , " password " : " gsxl11 " , " repassword " : " gsxl11 " } response information: Congratulations, you have successfully registered account with Example test results: ID: . 1 ----> Pass checkpoint ----> : Congratulations, you have successfully registered account . { ' the Content The -type ': 'file application / JSON ' , ' X-Requested-With- ' : ' the XMLHttpRequest ' } ******* executing the use case: ----- ID: 2 request method: post, a request URL: HTTP: // 47.97. 194.84: 8000 / api / register / type of request: json, body parameters: { " Account " : " gsxl11 " , " In Email " : " [email protected] " , " password " : " gsxl11 " , " repassword " :"gsxl11 " } in response to information: the user name is already registered, the user name Replace with Example Test Results: ID: 2 ----> Pass checkpoint ----> : This user name is already registered, replace username . { ' the Content-the Type ' : ' file application / JSON ' , ' X-requested-with- ' : ' the XMLHttpRequest ' } ******* executing the use case: ----- ID: . 3 request method: post, request URL: HTTP: // 47.97.194.84:8000/api/register/ type of request: json, body parameters: { " Account " : "" , " In Email ": "" , " Password " : "" , " repassword " : "" } in response to information: the user name is already registered, the user name Replace with Example Test Results: ID: . 3 ----> Pass checkpoint --- -> : this username is already registered, please replace the user name . =================================== ================================================== ======================================== 3 passed in 0 .42s ==== ================================================== ================================================== =====================
Welcome to QQ exchange group: 482 713 805