1. 首页
  2. docker教程

20-二十、Docker build 构建镜像

在上一章节中我们学习了 Dockerfile 的语法结构,那么我们就很轻松很容易的就能创建一个 Python 3.6.5 和 Flask 1.0.2 的镜像


FROM python:3.6.5 RUN pip install flask

我们将这些代码保存到当前目录下的 flask_365_102 目录中的文件 Dockerfile


[root@localhost ~]# mkdir flask_365_102 [root@localhost ~]# cd flask_365_102 [root@localhost flask_365_102] vi Dockerfile

然后我们就可以使用 docker build 命令, 从零开始来创建一个新的镜像


[root@localhost flask_365_102]# docker build -t ycbbs/flask_365_102 . Sending build context to Docker daemon 2.048kB Step 1/2 : FROM python:3.6.5 ---> 29d2f3226daf Step 2/2 : RUN pip install flask ---> Running in efaa520b0e38 Collecting flask Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB) Collecting itsdangerous>=0.24 (from flask) Downloading https://files.pythonhosted.org/packages/dc/b4/a60bcdba945c00f6d608d8975131ab3f25b22f2bcfe1dab221165194b2d4/itsdangerous-0.24.tar.gz (46kB) Collecting Jinja2>=2.10 (from flask) Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB) Collecting Werkzeug>=0.14 (from flask) Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB) Collecting click>=5.1 (from flask) Downloading https://files.pythonhosted.org/packages/34/c1/8806f99713ddb993c5366c362b2f908f18269f8d792aff1abfd700775a77/click-6.7-py2.py3-none-any.whl (71kB) Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask) Downloading https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz Building wheels for collected packages: itsdangerous, MarkupSafe Running setup.py bdist_wheel for itsdangerous: started Running setup.py bdist_wheel for itsdangerous: finished with status 'done' Stored in directory: /root/.cache/pip/wheels/2c/4a/61/5599631c1554768c6290b08c02c72d7317910374ca602ff1e5 Running setup.py bdist_wheel for MarkupSafe: started Running setup.py bdist_wheel for MarkupSafe: finished with status 'done' Stored in directory: /root/.cache/pip/wheels/33/56/20/ebe49a5c612fffe1c5a632146b16596f9e64676768661e4e46 Successfully built itsdangerous MarkupSafe Installing collected packages: itsdangerous, MarkupSafe, Jinja2, Werkzeug, click, flask Successfully installed Jinja2-2.10 MarkupSafe-1.0 Werkzeug-0.14.1 click-6.7 flask-1.0.2 itsdangerous-0.24 Removing intermediate container efaa520b0e38 ---> 083eecd092af Successfully built 083eecd092af Successfully tagged ycbbs/flask_365_102:latest
参数 说明
-t 指定要创建的目标镜像名
. Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径

然后我们就可以使用 docker images 查看创建的镜像已经在列表中存在


REPOSITORY TAG IMAGE ID CREATED SIZE ycbbs/flask_365_102 latest 083eecd092af 27 seconds ago 922MB ycbbs/py365flask102 latest aaf108c1dbdc 4 hours ago 922MB python 3.6.5 29d2f3226daf 3 weeks ago 912MB nginx latest ae513a47849c 4 weeks ago 109MB ubuntu latest 452a96d81c30 4 weeks ago 79.6MB ubuntu 17.10 e4422b8da209 4 weeks ago 99.2MB hello-world latest e38bc07ac18e 7 weeks ago 1.85kB jcdemo/flaskapp latest 084bae02af1b 4 months ago 98.9MB ubuntu 17.04 fe1cc5b91830 5 months ago 95.6MB ubuntu 16.10 7d3f705d307c 10 months ago 107MB

我们可以使用新的镜像来创建容器


[root@localhost flask_365_102]# docker run -t -i ycbbs/flask_365_102 python -c "import flask;print(flask.__version__)" .0、2
JS中文网,Javascriptc中文网是中国领先的新一代开发者社区和专业的技术媒体,一个帮助开发者成长的社区,是给开发者用的 Hacker News,技术文章由为你筛选出最优质的干货,其中包括:Android、iOS、前端、后端等方面的内容。目前已经覆盖和服务了超过 300 万开发者,你每天都可以在这里找到技术世界的头条内容。

本文著作权归作者所有,如若转载,请注明出处

转载请注明:文章转载自「 Java极客技术学习 」https://www.javajike.com

标题:20-二十、Docker build 构建镜像

链接:https://www.javajike.com/article/1192.html

« 21-二十一、Docker 镜像打标签
19-十九、Docker Dockerfile 创建镜像»

相关推荐

QR code