docker 7-Dockerfile learn to make your own image file

Foreword

If you are a python automation testers, one day you finally complete interface automation scripts in the company working on a project on your own machine or server commonly used in the commissioning of the script, firmly no problem.
But home from work at night, you found a linux server and want to try your hand at home, then re-install python3.6 version of Internet to find a whole bunch of tutorials barabara installation instructions.

The next day you go back to the company, the leadership says you can not put this script additionally performed (with your native environment is unreasonable) on a server environment, so you install repeated again python3.6 environment and execute scripts Some dependencies.
Good luck but also networking pip-line installation, bad luck, can not connect to external networks, can only be installed locally. Ever since you began to wonder life, why do so many times I have to repeat the installation environment ah. . . .

If 100 different servers, I is not gotta install 100 ah, Ever since you think: I can not do a virtual python3.6 environment, I need to pre-install the dependencies.
The well reached a virtual warehouse environment (similar to github), which need to use the machine, it is a key import in the past ok, so docker make your own image files imperative, which is to say today Dockerfile make one of their own image files.

Dockerfile Profile

Dockerfile Docker image is used to construct the build file, the script is a series of commands and configuration parameters.
In simple terms, Dockerfile is to us each step of the installation instructions and the environment, in a file, the final key to execute, you want to make a final environment.

Dockerfile Docker image is used to construct the build file, the script is a series of commands and configuration parameters.

Docker build trilogy:

  • Write dockerfile file
  • docker build building image image file
  • containers docker run run

Dockerfile related instructions

Dockerfile is to create a mirror image of the text file that contains all the commands, you can build mirrored by docker build a command based on the contents of Dockerfile,
before how to build introduce basic grammatical structure under the Dockerfile.

Dockerfile following command options:

  • FROM base image, a new image is based on which the current mirror
  • MAINTAINER mirror maintainer name and email address
  • Command needs to be run when building RUN container
  • CMD command to specify a container when you start to run. dockerfile can have multiple CMD command, but only the last one will work, CMD parameter will be replaced after the docker run.
  • EXPOSE current container exposed to the external port number
  • ENV environment variables to set the process to build the mirror
  • ADD copy the files in the directory to the host inside of the mirror and the ADD command processing URL and automatically extract the tar archive
  • COPY COPY: Similar ADD, copy files and directories to the mirror, but it is only copy, and URL processing does not automatically decompressing compressed tar.
  • ENTRYPOINT specified command a container when you start to run. ENTRYPOIT purpose and CMD, are in the specified container and start the program parameters.
  • VOLUME data volume container, for persistent data storage and work
  • USER username or UID run at specified container, subsequent RUN will use the specified user
  • WORKDIR designated after the container is created, the default terminal logged in the working directory, a foothold
  • ONBUILD run a command when building an inherited Dockerfile, Father mirrored in succession after the quilt, the parent mirrored onbuild is triggered.

Write Dockerfile

Create a folder in the current directory docker-run, cd into the folder, touch a new Dockerfile, vi and then open the file and start editing

[root@yoyo ~]# mkdir docker-run
[root@yoyo ~]# cd docker-run/
[root@yoyo docker-run]# touch Dockerfile
[root@yoyo docker-run]# vi Dockerfile 

Edit follows:

# 基于python3.6.8镜像
FROM python:3.6.8

MAINTAINER yoyo  <[email protected]>

# 更新pip
RUN pip install --upgrade pip 

# 工作目录
WORKDIR /code
ADD . /code

# pip安装依赖包
RUN pip install -r requirements.txt

# 传递参数
ENTRYPOINT ["pytest"]

# 默认显示help帮助信息
CMD ["--help"]

requirements.txt

requirements.txt of python is dependent packages, can be produced by freeze command

pip3 freeze >requirements.txt

[root@yoyo docker-run]# cat requirements.txt 
APScheduler==3.5.3
asn1crypto==0.24.0
atomicwrites==1.3.0
attrs==18.2.0
backports.csv==1.0.7
bcrypt==3.1.7
beautifulsoup4==4.7.1
cached-property==1.5.1
certifi==2018.11.29
cffi==1.12.3
chardet==3.0.4
cryptography==2.7
DBUtils==1.3
defusedxml==0.5.0
diff-match-patch==20181111
Django==2.1.4
django-bootstrap3==11.0.0
django-crispy-forms==1.7.2
django-formtools==2.1
django-import-export==1.2.0
django-ranged-response==0.2.0
django-reversion==3.0.3
django-simple-captcha==0.5.10
django-stdimage==4.0.1
docker==3.7.3
docker-compose==1.24.1
docker-pycreds==0.4.0
dockerpty==0.4.1
docopt==0.6.2
et-xmlfile==1.0.1
future==0.17.1
httplib2==0.12.1
idna==2.7
jdcal==1.4
jsonschema==2.6.0
more-itertools==6.0.0
mysqlclient==1.4.2.post1
odfpy==1.4.0
openpyxl==2.6.1
paramiko==2.6.0
Pillow==5.4.1
pluggy==0.6.0
progressbar2==3.39.3
py==1.7.0
pycparser==2.19
pymssql==2.1.4
PyMySQL==0.9.3
PyNaCl==1.3.0
pytest==3.6.3
python-utils==2.3.0
pytz==2018.7
PyYAML==3.13
requests==2.20.1
six==1.12.0
soupsieve==1.7.3
tablib==0.13.0
texttable==0.9.1
tzlocal==1.5.1
urllib3==1.24.1
websocket-client==0.56.0
xlrd==1.2.0
xlwt==1.3.0

build building image file

docker build command is used to create a mirror using Dockerfile. OPTIONS Description:

  • -f: Dockerfile Specifies the path to be used;

  • --pull: try to update the new version of the mirror;

  • --quiet, -q: quiet mode, only after the success of an output image ID;

  • --tag, -t: image name and label, typically name: tag name or format; may be provided to a plurality of image labels in a build.

-t parameter setting image tag name and tag name yoyo_pytes v1, attention has rearmost points.

docker build -t yoyo_pytest:v1 .

[root@yoyo docker-run]# docker build -t yoyo_pytest:v1 .
Sending build context to Docker daemon  11.78kB
Step 1/8 : FROM python:3.6
 ---> cfcdf565ff94
Step 2/8 : MAINTAINER yoyo  <[email protected]>
 ---> Using cache
 ---> f523b919fcf9
Step 3/8 : RUN pip install --upgrade pip
 ---> Using cache
 ---> 3399b50dab4e
Step 4/8 : WORKDIR /code
 ---> Using cache
 ---> 7223a20e17fd
Step 5/8 : ADD . /code
 ---> 650b554ccd6c
Step 6/8 : RUN pip install -r requirements.txt
 ---> Running in 0e49d444f7d8

During operation can be seen by running the steps, such as: Step 1/8

After the run is completed, the generated image can be viewed by the docker images

[root@yoyo docker-run]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
yoyo_pytest                v1                  6b4267ce7ac4        21 seconds ago      1.02GB
[root@yoyo docker-run]# 

run run container

Test_h.py create a new file in the current directory, write test scripts pytest

import pytest

# ** 作者:上海-悠悠 QQ交流群:717225969**
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", "test_h.py"])

Use docker run run container

  • -it -t docker so assigned and bound to a pseudo-terminal input standard container, -i standard container so that the input remains open.
  • When --rm container exits, automatically clear container. --rm option can not be used with -d
  • -v working directory container / code mount to $ PWD host, that is, the current directory
  • yoyo_pytest: v1 container name and tag name
  • Followed by the name of the script to be executed later test_h.py
[root@yoyo docker-run]# docker run -it --rm -v "$PWD":/code yoyo_pytest:v1 test_h.py  -s
================================================================== test session starts ==================================================================
platform linux -- Python 3.6.9, pytest-3.6.3, py-1.7.0, pluggy-0.6.0
rootdir: /code, inifile:
collected 3 items                                                                                                                                       

test_h.py 正在执行----test_one
.正在执行----test_two
.正在执行----test_three
.

=============================================================== 3 passed in 0.01 seconds ================================================================
[root@yoyo docker-run]# 

python Interface QQ group: 717 225 969

Guess you like

Origin www.cnblogs.com/yoyoketang/p/11397597.html