Dockerfile入门
指令参考:https://docs.docker.com/engine/reference/builder/
我们先理解一下什么是Dockerfile:
Dockerfile 是软件的原材料,Docker 镜像是软件的交付品,而 Docker 容器则可以认为是软件的运行态。从应用软件的角度来看,Dockerfile、Docker 镜像与 Docker 容器分别代表软件的三个不同阶段,Dockerfile 面向开发,Docker 镜像成为交付标准,Docker 容器则涉及部署与运维,三者缺一不可,合力充当 Docker 体系的基石。
Dockerfile 中的每一条命令,都在 Docker 镜像中以一个独立镜像层的形式存在,如下图:

例子1:

创建一个Dockerfile文件:
1
FROM alpine:3.11
2
3
RUN apk update && apk --no-cache add python3 curl bind-tools iproute2 iptables ipvsadm
4
RUN pip3 install -i https://pypi.doubanio.com/simple --no-cache-dir --upgrade pip && \
5
pip3 install --no-cache-dir -q Flask==0.12.4 requests==2.21.0
6
7
ADD demo.py /usr/local/bin/
8
9
ENV DEPLOYENV='Production' RELEASE='Stable' PS1="[\[email protected]\h \w]\\$ "
10
11
EXPOSE 80
12
13
VOLUME ["/simon-testing/dockerfile/data"]
14
15
ENTRYPOINT python3 /usr/local/bin/demo.py
Copied!
其中-i https://pypi.doubanio.com/simpl使用国内的镜像加速,避免出现超时错误。
在同一目录下创建一个demo.py,使用Flask框架简单搭建一个服务:
1
#!/usr/bin/python3
2
#
3
from flask import Flask, request, abort, Response, jsonify as flask_jsonify, make_response
4
import argparse
5
import sys, os, getopt, socket, json, time
6
7
app = Flask(__name__)
8
9
start_time = time.time()
10
11
@app.route('/')
12
def index():
13
return ('ClientIP: {}, ServerName: {}, ''ServerIP: {}!\n'.format(request.remote_addr, socket.gethostname(),
14
socket.gethostbyname(socket.gethostname())))
15
16
def main(argv):
17
port = 80
18
host = '0.0.0.0'
19
debug = False
20
21
if os.environ.get('PORT') is not None:
22
port = os.environ.get('PORT')
23
24
if os.environ.get('HOST') is not None:
25
host = os.environ.get('HOST')
26
27
try:
28
opts, args = getopt.getopt(argv,"vh:p:",["verbose","host=","port="])
29
except getopt.GetoptError:
30
print('server.py -p <portnumber>')
31
sys.exit(2)
32
for opt, arg in opts:
33
if opt in ("-p", "--port"):
34
port = arg
35
elif opt in ("-h", "--host"):
36
host = arg
37
elif opt in ("-v", "--verbose"):
38
debug = True
39
40
app.run(host=str(host), port=int(port), debug=bool(debug))
41
42
43
if __name__ == "__main__":
44
main(sys.argv[1:])
Copied!
然后可以运行指令docker build . -t demoapp:v0.1
第一次运行大约需要20分钟,八个步骤对应Dockerfile里面的八层:
1
Sending build context to Docker daemon 4.608kB
2
Step 1/8 : FROM alpine:3.11
3
---> f70734b6a266
4
Step 2/8 : RUN apk update && apk --no-cache add python3 curl bind-tools iproute2 iptables ipvsadm
5
---> Using cache
6
---> 2ebf276753f5
7
Step 3/8 : RUN pip3 install --no-cache-dir --upgrade pip && pip3 install --no-cache-dir -q Flask==0.12.4 requests==2.21.0
8
---> Using cache
9
---> bcf41b3ffeff
10
Step 4/8 : ADD demo.py /usr/local/bin/
11
---> Using cache
12
---> 8bab90aeff83
13
Step 5/8 : ENV DEPLOYENV='Production' RELEASE='Stable' PS1="[\[email protected]\h \w]\\$ "
14
---> Using cache
15
---> 2421ecaf4dea
16
Step 6/8 : EXPOSE 80
17
---> Using cache
18
---> 437beef4b0ef
19
Step 7/8 : VOLUME ["/simon-testing/dockerfile/data"]
20
---> Using cache
21
---> 31f154aaef03
22
Step 8/8 : ENTRYPOINT python3 /usr/local/bin/demo.py
23
---> Using cache
24
---> 6d4e0298f516
25
Successfully built 6d4e0298f516
26
Successfully tagged demoapp:v0.1
Copied!
镜像打包完成后,用docker run --name myapp demoapp:v0.1运行起来,容器内默认会执行python3 /usr/local/bin/demo.py
1
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
Copied!
然后打开另一个terminal,查看一下容器ip地址,并用curl访问容器80端口:
1
# docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' myapp
2
172.17.0.2
3
[email protected]:curl 172.17.0.2
4
ClientIP: 172.17.0.1, ServerName: 48bcba8c4858, ServerIP: 172.17.0.2!
Copied!
其他替换entrypoint和进入容器的操作,请查阅前面的章节。
Last modified 1yr ago
Copy link
Contents
例子1: