一、项目准备
app.py
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return "hello docker" if __name__ == '__main__': app.run(host="0.0.0.0", port=5000)
二、生成镜像
1、dockerfile
FROM python:3.6 LABEL maintainer="ninesun0318<wangxucs123@126.com>" RUN pip install flask COPY app.py /app WORKDIR /app EXPOSE 5000 CMD ["python", "app.py"]
------------------------------------update 2021年3月22日22:01:26 部署xd项目的dockerFile
FROM python:3.6 RUN mkdir -p /usr/src/app COPY pip.conf /root/.pip/pip.conf COPY xdProj /usr/src/app LABEL maintainer="ninesun@126.com" LABEL version="1.0" LABEL description="xd eda lab" WORKDIR /usr/src/app RUN pip install -r /usr/src/app/requirements.txt CMD [ "python", "./manage.py", "runserver", "0.0.0.0:8000","--insecure"] #CMD ["python","/usr/src/app/manage.py runserver 0.0.0.0:8000 --insecure"]
-------------------------------------update 2021年3月22日22:01:31
2、build image
[root@iZ2vceej7yes1abpm7yec2Z python]# docker build -t ninesun0318/flask . Sending build context to Docker daemon 3.072kB Step 1/7 : FROM python:3.6 3.6: Pulling from library/python e4c3d3e4f7b0: Pull complete 101c41d0463b: Pull complete 8275efcd805f: Pull complete 751620502a7a: Pull complete 0a5e725150a2: Pull complete 9ab4bf1101f3: Pull complete 9ba108bf0aed: Pull complete eac75d8e87c4: Pull complete b7d6e626c70b: Pull complete Digest: sha256:62ada7d65aa310020e5278a9905c66fb4a8e149a1b48c18fa76da1a9d61d046d Status: Downloaded newer image for python:3.6 ---> 1297140c6dd2 Step 2/7 : LABEL "maintainer=ninesun0318<wangxucs123@126.com>" failed to process "\"maintainer": unexpected end of statement while looking for matching double-quote [root@iZ2vceej7yes1abpm7yec2Z python]# vim Dockerfile [root@iZ2vceej7yes1abpm7yec2Z python]# [root@iZ2vceej7yes1abpm7yec2Z python]# [root@iZ2vceej7yes1abpm7yec2Z python]# [root@iZ2vceej7yes1abpm7yec2Z python]# docker build -t ninesun0318/flask . Sending build context to Docker daemon 3.072kB Step 1/7 : FROM python:3.6 ---> 1297140c6dd2 Step 2/7 : LABEL maintainer="ninesun0318<wangxucs123@126.com>" ---> Running in d43d5bd6b5ae Removing intermediate container d43d5bd6b5ae ---> 9484d8e54687 Step 3/7 : RUN pip install flask ---> Running in 9ac44134fb6e Collecting flask Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB) Collecting Jinja2>=2.10.1 Downloading Jinja2-2.11.2-py2.py3-none-any.whl (125 kB) Collecting itsdangerous>=0.24 Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB) ERROR: Exception: Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/pip/_vendor/urllib3/response.py", line 437, in _error_catcher yield File "/usr/local/lib/python3.6/site-packages/pip/_vendor/urllib3/response.py", line 519, in read data = self._fp.read(amt) if not fp_closed else b"" File "/usr/local/lib/python3.6/site-packages/pip/_vendor/cachecontrol/filewrapper.py", line 62, in read data = self.__fp.read(amt) File "/usr/local/lib/python3.6/http/client.py", line 463, in read n = self.readinto(b) File "/usr/local/lib/python3.6/http/client.py", line 507, in readinto n = self.fp.readinto(b) File "/usr/local/lib/python3.6/socket.py", line 586, in readinto return self._sock.recv_into(b) File "/usr/local/lib/python3.6/ssl.py", line 1012, in recv_into return self.read(nbytes, buffer) File "/usr/local/lib/python3.6/ssl.py", line 874, in read return self._sslobj.read(len, buffer) File "/usr/local/lib/python3.6/ssl.py", line 631, in read v = self._sslobj.read(len, buffer) socket.timeout: The read operation timed out During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/pip/_internal/cli/base_command.py", line 228, in _main status = self.run(options, args) File "/usr/local/lib/python3.6/site-packages/pip/_internal/cli/req_command.py", line 182, in wrapper return func(self, options, args) File "/usr/local/lib/python3.6/site-packages/pip/_internal/commands/install.py", line 324, in run reqs, check_supported_wheels=not options.target_dir File "/usr/local/lib/python3.6/site-packages/pip/_internal/resolution/legacy/resolver.py", line 183, in resolve discovered_reqs.extend(self._resolve_one(requirement_set, req)) File "/usr/local/lib/python3.6/site-packages/pip/_internal/resolution/legacy/resolver.py", line 388, in _resolve_one abstract_dist = self._get_abstract_dist_for(req_to_install) File "/usr/local/lib/python3.6/site-packages/pip/_internal/resolution/legacy/resolver.py", line 340, in _get_abstract_dist_for abstract_dist = self.preparer.prepare_linked_requirement(req) File "/usr/local/lib/python3.6/site-packages/pip/_internal/operations/prepare.py", line 469, in prepare_linked_requirement hashes=self._get_linked_req_hashes(req) File "/usr/local/lib/python3.6/site-packages/pip/_internal/operations/prepare.py", line 259, in unpack_url hashes=hashes, File "/usr/local/lib/python3.6/site-packages/pip/_internal/operations/prepare.py", line 130, in get_http_url link, downloader, temp_dir.path, hashes File "/usr/local/lib/python3.6/site-packages/pip/_internal/operations/prepare.py", line 282, in _download_http_url for chunk in download.chunks: File "/usr/local/lib/python3.6/site-packages/pip/_internal/network/utils.py", line 88, in response_chunks decode_content=False, File "/usr/local/lib/python3.6/site-packages/pip/_vendor/urllib3/response.py", line 576, in stream data = self.read(amt=amt, decode_content=decode_content) File "/usr/local/lib/python3.6/site-packages/pip/_vendor/urllib3/response.py", line 541, in read raise IncompleteRead(self._fp_bytes_read, self.length_remaining) File "/usr/local/lib/python3.6/contextlib.py", line 99, in __exit__ self.gen.throw(type, value, traceback) File "/usr/local/lib/python3.6/site-packages/pip/_vendor/urllib3/response.py", line 442, in _error_catcher raise ReadTimeoutError(self._pool, None, "Read timed out.") pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. The command '/bin/sh -c pip install flask' returned a non-zero code: 2 [root@iZ2vceej7yes1abpm7yec2Z python]# vim Dockerfile [root@iZ2vceej7yes1abpm7yec2Z python]# docker build -t ninesun0318/flask . Sending build context to Docker daemon 3.072kB Step 1/7 : FROM python:3.6 ---> 1297140c6dd2 Step 2/7 : LABEL maintainer="ninesun0318<wangxucs123@126.com>" ---> Using cache ---> 9484d8e54687 Step 3/7 : RUN pip --default-timeout=100 install flask ---> Running in 97cdabc628e9 Collecting flask Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB) Collecting Jinja2>=2.10.1 Downloading Jinja2-2.11.2-py2.py3-none-any.whl (125 kB) Collecting Werkzeug>=0.15 Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB) Collecting click>=5.1 Downloading click-7.1.2-py2.py3-none-any.whl (82 kB) Collecting itsdangerous>=0.24 Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB) Collecting MarkupSafe>=0.23 Downloading MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl (27 kB) Installing collected packages: MarkupSafe, Jinja2, Werkzeug, click, itsdangerous, flask Successfully installed Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.2 itsdangerous-1.1.0 Removing intermediate container 97cdabc628e9 ---> 1a5dcd204052 Step 4/7 : COPY app.py /app ---> 21ec0db0145d Step 5/7 : WORKDIR /app Cannot mkdir: /app is not a directory
这过程需要等10十分钟,第一次报错timeout之后修改了docker file,在pip install 时增加了--default-timeout=100参数。
再次builf又发生错误:
第5步说 /app 不是一个目录。
每一步之后都有一个临时的image。 可以进入到第四步生成的image里面去检查/app 是否为一个目录。
docker run -it 21ec0db0145d /bin/bash 以交互模式运行 /bin/bash 。发现app是一个文件而不是目录。
ython]# docker run -it 21ec0db0145d /bin/bash root@40602c46733a:/# ll bash: ll: command not found root@40602c46733a:/# ls -l total 4 -rwxr-xr-x 1 root root 170 Nov 8 12:41 app drwxr-xr-x 1 root root 179 Oct 13 02:15 bin drwxr-xr-x 2 root root 6 Sep 19 21:39 boot drwxr-xr-x 5 root root 360 Nov 9 13:35 dev
哪一步出问题了呢?
查看dockfile的第四步,问题出在copy那一步了, copy app.py /app。 不是copy到/app 目录而是copy文件,因此workdir那不会报错
修改docker file之后重新build
[root@iZ2vceej7yes1abpm7yec2Z python]# docker build -t ninesun0318/flask . Sending build context to Docker daemon 3.072kB Step 1/7 : FROM python:3.6 ---> 1297140c6dd2 Step 2/7 : LABEL maintainer="ninesun0318<wangxucs123@126.com>" ---> Using cache ---> 9484d8e54687 Step 3/7 : RUN pip --default-timeout=100 install flask ---> Using cache ---> 1a5dcd204052 Step 4/7 : COPY app.py /app/ ---> b7b7a93fcfc2 Step 5/7 : WORKDIR /app Removing intermediate container 1faabfdab3d9 ---> b55ed66d1d69 Step 6/7 : EXPOSE 5000 ---> Running in eb5d6ce4e9dd Removing intermediate container eb5d6ce4e9dd ---> 80743f74b6ed Step 7/7 : CMD ["python", "app.py"] ---> Running in cefe38fa1e9c Removing intermediate container cefe38fa1e9c ---> 1f7f63f7ff02 Successfully built 1f7f63f7ff02 Successfully tagged ninesun0318/flask:latest
docker images
python]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ninesun0318/flask latest 1f7f63f7ff02 4 minutes ago 884MB python 3.6 1297140c6dd2 5 days ago 874MB centos latest 0d120b6ccaa8 3 months ago 215MB hello-world latest bf756fb1ae65 10 months ago 13.3kB
3、后台运行container
python]# docker run -d ninesun0318/flask 6b54d60933d7db54911941f1a23f8c91d10d948fca3e4d3ea07c21be5f602900 [root@iZ2vceej7yes1abpm7yec2Z python]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6b54d60933d7 ninesun0318/flask "python app.py" 6 seconds ago Up 5 seconds 5000/tcp gracious_hawking
三、疑问
dockerfile中没有from 任何一个OS,这个image的base image没有OS是如何运作的?
这个问题还不是很清楚,希望各位大佬指导...