容器技术经历过多年发展后,已经被越拉越多的公司采用,同时越来越多的软件支持以容器的形式进行分发,我们今天就以python测开平台为例采用编写dockerfile文件并编译,生成容器镜像。
Dockerfile语法介绍
首先我们先通过编写完成的dockerfile文件了解下语法
FROMpython:3.6-alpine
COPY . /work
WORKDIR /work
RUN pip3 install -r requirements.txt
CMD python3 docker.py
RUNpip3install-rrequirements.txt
CMDpython3docker.py
from指令用于指定基础镜像,语法如下:
FROM <image>[:<tag> | @<digest>] [AS <name>]
这里我们指定基础镜像为python,tag为3.6-alpine,其中3.6为python的版本,alpine是linux操作系统,关于alpine的更多信息可以参考官网https://www.alpinelinux.org/。
copy指令用于复制文件到容器中,语法如下:
COPY <src> <dest>
这里我们将当前目录下的所有内容复制到容器中的/work目录下,容器会为我们自动创建work目录。
workdir指令用于指定工作目录,语法如下:
WORKDIR <dir>
这条指令运行效果相当于cd <dir>,在容器中会切换到work目录。
run指令用于指定构建镜像时的指令,可以是任何指令,语法如下:
RUN <command>
我们通过run指令告诉容器需要通过pip安装依赖,依赖使用requirements.txt描述。
cmd指令用于指定容器运行的内容,语法如下:
CMD command param1 param2
我们这里使用python启动平台。
编译镜像
我们在项目根目录下输入以下指令进行镜像编译
docker build -f Dockerfile .
注意指令末尾的点不能省略,编译过程较缓慢请耐心等待。
$ docker build -f Dockerfile .
Sending build context to Docker daemon 14.34kB
Step 1/5 : FROM python:3.6-alpine
---> 8880aaf979d2
Step 2/5 : COPY . /work
---> 57277dc5819c
Step 3/5 : WORKDIR /work
---> Running in bc22b0faee90
Removing intermediate container bc22b0faee90
---> 64b3f3ca976a
Step 4/5 : RUN pip3install-r requirements.txt
---> Running in 96f1f7faa2b7
Collecting flask==1.1.1
Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
Collecting Werkzeug>=0.15
Downloading https://files.pythonhosted.org/packages/ce/42/3aeda98f96e85fd26180534d36570e4d18108d62ae36f87694b476b83d6f/Werkzeug-0.16.0-py2.py3-none-any.whl (327kB)
Collecting click>=5.1
Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting itsdangerous>=0.24
Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2>=2.10.1
Downloading https://files.pythonhosted.org/packages/65/e0/eb35e762802015cab1ccee04e8a277b03f1d8e53da3ec3106882ec42558b/Jinja2-2.10.3-py2.py3-none-any.whl (125kB)
Collecting MarkupSafe>=0.23
Downloading https://files.pythonhosted.org/packages/b9/2e/64db92e53b86efccfaea71321f597fa2e1b2bd3853d8ce658568f7a13094/MarkupSafe-1.1.1.tar.gz
Building wheelsforcollected packages: MarkupSafe
Building wheelforMarkupSafe (setup.py): started
Building wheelforMarkupSafe (setup.py): finishedwithstatus'done'
Created wheelforMarkupSafe: filename=MarkupSafe-1.1.1-cp36-none-any.whlsize=12629sha256=8f9be0cc4e0b219015ffcd57680fb4b3b10a0a9f99acc9baff5b202dd0d0ee41
Storedindirectory: /root/.cache/pip/wheels/f2/aa/04/0edf07a1b8a5f5f1aed7580fffb69ce8972edc16a505916a77
Successfully built MarkupSafe
Installing collected packages: Werkzeug, click, itsdangerous, MarkupSafe, Jinja2, flask
Successfully installed Jinja2-2.10.3MarkupSafe-1.1.1Werkzeug-0.16.0click-7.0flask-1.1.1itsdangerous-1.1.0
Removing intermediatecontainer96f1f7faa2b7
---> c65963ff7ac3
Step5/5: CMD python3 docker.py
---> Running in 42eadb45fd22
Removing intermediatecontainer42eadb45fd22
---> c4176f52ee28
Successfully built
SECURITYWARNING: Youarebuilding a Docker imagefromWindows against a non-Windows Docker host. All filesanddirectories addedtobuildcontextwill have'-rwxr-xr-x'permissions. Itisrecommendedtodoublecheckandresetpermissionsforsensitive filesanddirectories.
我们可以看到名为c4176f52ee28的容器被成功编译,输入
docker run -it -p 5000:5000c4176f52ee28
启动容器并打开浏览器访问
注:Mac需替换192.168.99.100为自己的机器IP。
附录
平台代码
fromflaskimportFlask
app = Flask(__name__)
@app.route('/')
def index():
return"flask in docker!"
if__name__ =='__main__':
app.run(
host="0.0.0.0",
port=5000,
)
requirements.txt文件
flask==1.1.1
Dockerfile文件
FROMpython:3.6-alpine
COPY . /work
WORKDIR /work
RUN pip3 install -r requirements.txt
CMD python3 docker.py
作 者:Testfan 大猫老师
出 处:微信公众号:自动化软件测试平台
版权说明:欢迎转载,但必须注明出处,并在文章页面明显位置给出文章链接