1. 编写Dockerfile

# 将官方 Python 运行时用作父镜像
FROM python:3.6. # 将工作目录设置为 /app
WORKDIR /app # 将当前目录内容复制到位于 /app 中的容器中
ADD . /app # 安装 requirements.txt 中指定的任何所需软件包
RUN pip install -r requirements.txt # 使端口 可供此容器外的环境使用
EXPOSE # 定义环境变量
ENV NAME World # 在容器启动时运行 app.py
CMD ["python", "app.py"]

2. 提供依赖文件

2.1. requirements.txt

Flask
Redis

2.2. app.py

from flask import Flask
from redis import Redis, RedisError
import os
import socket # Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2) app = Flask(__name__) @app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to Redis, counter disabled</i>" html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>" \
"<b>Visits:</b> {visits}"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits) if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)

3. 构建应用

root@VM_0_7_centos p]# ls
app.py Dockerfile requirements.txt [root@VM_0_7_centos p]# docker build -t friendlyhello .
Sending build context to Docker daemon .608kB
Step / : FROM python:3.6.
3.6.: Pulling from library/python
741437d97401: Already exists
34d8874714d7: Already exists
0a108aa26679: Already exists
7f0334c36886: Already exists
65c95cb8b3be: Already exists
36185429e34c: Pull complete
99d78cb27656: Pull complete
735a9c9a10ba: Pull complete
aa45785265a0: Pull complete
Digest: sha256:00110125bd9c23f200cfd2cfa82e68b8ab2006e1358f7a048e005794aa51568f
Status: Downloaded newer image for python:3.6.
---> e0a418687f6c
Step / : WORKDIR /app
---> Running in 5402422af25a
Removing intermediate container 5402422af25a
---> e1f264441e23
Step / : ADD . /app
---> 36cf6907a33f
Step / : RUN pip install -r requirements.txt
---> Running in 1f1a15b13b08
Collecting Flask (from -r requirements.txt (line ))
Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
Collecting Redis (from -r requirements.txt (line ))
Downloading https://files.pythonhosted.org/packages/d0/8b/c43ef27d02382853b22c49bc41a8389e47d60811dd1d72b9a45bc905a5f8/redis-3.2.0-py2.py3-none-any.whl (65kB)
Collecting Werkzeug>=0.14 (from Flask->-r requirements.txt (line ))
Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
Collecting itsdangerous>=0.24 (from Flask->-r requirements.txt (line ))
Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting click>=5.1 (from Flask->-r requirements.txt (line ))
Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting Jinja2>=2.10 (from Flask->-r requirements.txt (line ))
Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->Flask->-r requirements.txt (line ))
Downloading https://files.pythonhosted.org/packages/08/04/f2191b50fb7f0712f03f064b71d8b4605190f2178ba02e975a87f7b89a0d/MarkupSafe-1.1.0-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: Werkzeug, itsdangerous, click, MarkupSafe, Jinja2, Flask, Redis
Successfully installed Flask-1.0. Jinja2-2.10 MarkupSafe-1.1. Redis-3.2. Werkzeug-0.14. click-7.0 itsdangerous-1.1.
Removing intermediate container 1f1a15b13b08
---> 4b5a84e8ba00
Step / : EXPOSE
---> Running in 3f41d16ac324
Removing intermediate container 3f41d16ac324
---> e757e6f67a39
Step / : ENV NAME World
---> Running in 72edb9f98355
Removing intermediate container 72edb9f98355
---> 7dafe4033354
Step / : CMD ["python", "app.py"]
---> Running in bfbd6350bd43
Removing intermediate container bfbd6350bd43
---> 0667cb3d8f91
Successfully built 0667cb3d8f91
Successfully tagged friendlyhello:latest

4. 运行应用

# docker run -d -p : friendlyhello
d5ef6edaa6d1e7df9bd6d997a0c672a8e2d43895c7db0c5ff373b681e84849f0 # curl http://localhost:4000
<h3>Hello World!</h3><b>Hostname:</b> d5ef6edaa6d1<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i> # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d5ef6edaa6d1 friendlyhello "python app.py" seconds ago Up seconds 0.0.0.0:->/tcp priceless_chebyshev 停止应用
# docker stop d5ef6edaa6d1

5. 发布镜像

  • 在cloud.docker.com网站注册
  • docker login 命令登录
  • 标记本地镜像:docker tag friendlyhello timscm/get-started:part1
  • 发布镜像: docker push timscm/get-started:part1
  • 下载远程镜像(测试目的,可能需要先删除已存在的镜像):docker run -p 4000:80 timscm/get-started:part1
[root@VM_0_7_centos ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: timscm
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
[root@VM_0_7_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest 0667cb3d8f91 minutes ago 932MB
python 3.6. e0a418687f6c days ago 922MB
python latest ac069ebfe1e1 days ago 927MB
hello-world latest fce289e99eb9 weeks ago .84kB
[root@VM_0_7_centos ~]# docker tag image timscm/get-started:part1
Error response from daemon: No such image: image:latest
[root@VM_0_7_centos ~]# docker tag friendlyhello timscm/get-started:part1
[root@VM_0_7_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest 0667cb3d8f91 minutes ago 932MB
timscm/get-started part1 0667cb3d8f91 minutes ago 932MB
python 3.6. e0a418687f6c days ago 922MB
python latest ac069ebfe1e1 days ago 927MB
hello-world latest fce289e99eb9 weeks ago .84kB
[root@VM_0_7_centos ~]# docker push timscm/get-started:part1
The push refers to repository [docker.io/timscm/get-started]
c21f48b71c61: Pushed
599fd8743518: Pushed
07de0bdbb7e4: Pushed
195394b646ef: Mounted from library/python
f50f856f49fa: Mounted from library/python
80b43ad4adf9: Mounted from library/python
14c77983a1cf: Mounted from library/python
a22a5ac18042: Mounted from library/python
6257fa9f9597: Mounted from library/python
578414b395b9: Mounted from library/python
abc3250a6c7f: Mounted from library/python
13d5529fd232: Mounted from library/python
part1: digest: sha256:6e167aed79463d62a48d0dffaf9e70c1e030e34ce09120bd020b0fe3b9178f7c size:
[root@VM_0_7_centos ~]# docker run -d -p : timscm/get-started:part1
96092534319b79111e7bf3343eb99409fa9ea0d5b87e7aa7eec6facacd98421d
[root@VM_0_7_centos ~]# docker images -q
0667cb3d8f91
0667cb3d8f91
e0a418687f6c
ac069ebfe1e1
fce289e99eb9
[root@VM_0_7_centos ~]# docker rmi $(docker images -q)
Untagged: python:latest
Untagged: python@sha256:fb877e7ea5c40de100534c6d6f515c486247bfd899cb2767f2de0e876a8127fa
Deleted: sha256:ac069ebfe1e184fc65f70cf83dbb4eeee0c495dbba6757954c130e4125e674e6
Deleted: sha256:926dd46f86107116ae928b9efa391acc9d2efd2656e0f15117139dfc817fb074
Deleted: sha256:70cea1735c6327330cf2c69a48fa543b6f83de73288228047a579c6b31ddcf17
Deleted: sha256:3d46a7823c655242fec7260b8e77a19e3ddf954555f11e91f983a84a0c4b01b2
Deleted: sha256:9f1a83701a94dae5b5546c0cc5d4af12fc215002b79437cef0320c586bd959a7
Error response from daemon: conflict: unable to delete 0667cb3d8f91 (cannot be forced) - image is being used by running container 96092534319b
Error response from daemon: conflict: unable to delete 0667cb3d8f91 (cannot be forced) - image is being used by running container 96092534319b
Error response from daemon: conflict: unable to delete e0a418687f6c (cannot be forced) - image has dependent child images
Error response from daemon: conflict: unable to delete fce289e99eb9 (must be forced) - image is being used by stopped container ebf4c60de4b6
[root@VM_0_7_centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96092534319b timscm/get-started:part1 "python app.py" About a minute ago Up About a minute 0.0.0.0:->/tcp nostalgic_brahmagupta
[root@VM_0_7_centos ~]# docker stop 96092534319b
96092534319b
[root@VM_0_7_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest 0667cb3d8f91 minutes ago 932MB
timscm/get-started part1 0667cb3d8f91 minutes ago 932MB
python 3.6. e0a418687f6c days ago 922MB
hello-world latest fce289e99eb9 weeks ago .84kB
[root@VM_0_7_centos ~]# docker rmi $(docker images -q)
Error response from daemon: conflict: unable to delete 0667cb3d8f91 (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete 0667cb3d8f91 (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete e0a418687f6c (cannot be forced) - image has dependent child images
Error response from daemon: conflict: unable to delete fce289e99eb9 (must be forced) - image is being used by stopped container ebf4c60de4b6
[root@VM_0_7_centos ~]# docker rmi -f $(docker images -q)
Untagged: friendlyhello:latest
Untagged: timscm/get-started:part1
Untagged: timscm/get-started@sha256:6e167aed79463d62a48d0dffaf9e70c1e030e34ce09120bd020b0fe3b9178f7c
Deleted: sha256:0667cb3d8f915c1fb390f0f27d7f573771771b1f8c3a08ca60778fba36d867f7
Deleted: sha256:7dafe4033354697ee5e0100a1abda609fcdf9641f9c9026c9df1b8ddac7b2613
Deleted: sha256:e757e6f67a39c40b8d7efb150c658a1eb9a7ec2dd9056c98fc944718cc22f10e
Deleted: sha256:4b5a84e8ba00246ebc717a3dda8506745644bb33c45bafdbd6dbc8f0730636c1
Deleted: sha256:36cf6907a33f4bd53ab7c55f07e4f91d374c4011b89fd55cf34b85100a788423
Deleted: sha256:e1f264441e2316d6b2f4c422b8319b2e50ef089e6ef2d2a9d8e3e050b1ea94bf
Untagged: python:3.6.
Untagged: python@sha256:00110125bd9c23f200cfd2cfa82e68b8ab2006e1358f7a048e005794aa51568f
Deleted: sha256:e0a418687f6c53e1c097b413ab16306dbfc1d530498fbac64fbd5373cd8f885b
Untagged: hello-world:latest
Untagged: hello-world@sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Error: No such image: 0667cb3d8f91
[root@VM_0_7_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@VM_0_7_centos ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@VM_0_7_centos ~]# docker run -d -p : timscm/get-started:part1
Unable to find image 'timscm/get-started:part1' locally
part1: Pulling from timscm/get-started
741437d97401: Already exists
34d8874714d7: Already exists
0a108aa26679: Already exists
7f0334c36886: Already exists
65c95cb8b3be: Already exists
36185429e34c: Already exists
99d78cb27656: Already exists
735a9c9a10ba: Already exists
aa45785265a0: Already exists
ac35858a33a0: Already exists
5288ebc86076: Already exists
7c5bb950aa22: Already exists
Digest: sha256:6e167aed79463d62a48d0dffaf9e70c1e030e34ce09120bd020b0fe3b9178f7c
Status: Downloaded newer image for timscm/get-started:part1
7c449713451aac9e6fa7a5cb64b289283e5afa203262e5cfc8cd5eec4b55645d
[root@VM_0_7_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
timscm/get-started part1 0667cb3d8f91 minutes ago 932MB
[root@VM_0_7_centos ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
timscm/get-started part1 0667cb3d8f91 minutes ago 932MB
[root@VM_0_7_centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c449713451a timscm/get-started:part1 "python app.py" seconds ago Up seconds 0.0.0.0:->/tcp gifted_swartz
[root@VM_0_7_centos ~]# curl http://localhost:4000
<h3>Hello World!</h3><b>Hostname:</b> 7c449713451a<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>
[root@VM_0_7_centos ~]#

[TimLinux] docker CentOS7 入门——容器(1)的更多相关文章

  1. [TimLinux] docker CentOS7入门——服务(2)

    1. 服务含义 分布式应用中,应用的不同部分即称为“服务”,视频网站是一个分布式应用,包含有:数据的存储,视频的转码,前端展示等部分,对应的这些部分即称为相应的服务.docker平台中,定义.运行和扩 ...

  2. [TimLinux] docker CentOS7安装docker-ce最新版

    1. 环境 $ lsb_release -a # 需要安装 redhat-lsb-core 包 LSB Version: :core-4.1-amd64:core-4.1-noarch Distrib ...

  3. Docker从入门到掉坑(三):容器太多,操作好麻烦

    前边的两篇文章里面,我们讲解了基于docker来部署基础的SpringBoot容器,如果阅读本文之前没有相关基础的话,可以回看之前的教程. Docker 从入门到掉坑 Docker从入门到掉坑(二): ...

  4. 容器技术之Docker基础入门

    前文我们了解了下LXC的基础用法以及图形管理工具LXC WEB Panel的简单使用,有兴趣的朋友可以参考https://www.cnblogs.com/qiuhom-1874/p/12904188. ...

  5. .Net Core微服务入门全纪录(八)——Docker Compose与容器网络

    Tips:本篇已加入系列文章阅读目录,可点击查看更多相关文章. 前言 上一篇[.Net Core微服务入门全纪录(七)--IdentityServer4-授权认证]中使用IdentityServer4 ...

  6. CentOS7 下Docker最新入门教程 超级详细 (安装以及简单的使用)

    转载https://blog.csdn.net/wzsy_ll/article/details/82866627 1.为什么使用Docker(本人) 最近总是频繁的在新服务器发布项目, 每次发布都需要 ...

  7. Windows10下的docker安装与入门 (二)使用docker引擎在容器中运行镜像

    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有任何 ...

  8. Docker 学习1 容器技术基础入门

    一.虚拟化 1.主机级别虚拟化(两种) a.类型一虚拟化:在硬件上直接安装hyper-ver,然后再安装虚拟机 1).操作系统就两棵树:进程树,文件系统树. 2).一个内核只能有一个root b.类型 ...

  9. centos7下安装docker(19容器架构)

    What,Why,How What:什么是容器? 说起容器大家想到的是什么?      集装箱(container),虚拟机,docker,k8s 1. 没毛病,因为容器与集装箱的英文都可以翻译成co ...

随机推荐

  1. Jsp自学2

    Jsp简单来说就是java代码与Html代码的组合,类,方法,属性跟网页展示夹杂在一起.Jsp就是Servlet,但比Servle简单,不需要配置web.xml(当然也可以配置).Jsp由模板数据与元 ...

  2. Xshell选中的同时把内容复制到剪贴板

    1.设置对话框 工具 -> 选项 -> 键盘和鼠标 -> 将选定的文本自动复制到剪贴板 2.贴图如下 2.1.打开设置对话框 2.2.设置键盘鼠标,左键复制

  3. 利用SSH隧道技术穿越内网访问远程设备

    本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/11899478.html 通常,我们用于调试的计算机无法远程访问位于局域网中的待调试设备. ...

  4. hdu 1863 畅通工程 (prim)

    畅通工程Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. 在开发框架中扩展微软企业库,支持使用ODP.NET(Oracle.ManagedDataAccess.dll)访问Oracle数据库

    在前面随笔<在代码生成工具Database2Sharp中使用ODP.NET(Oracle.ManagedDataAccess.dll)访问Oracle数据库,实现免安装Oracle客户端,兼容3 ...

  6. 力扣(LeetCode)买卖股票的最佳时机 个人题解

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...

  7. 力扣(LeetCode)颠倒二进制位 个人题解

    颠倒给定的 32 位无符号整数的二进制位. 示例 1: 输入: 00000010100101000001111010011100 输出: 0011100101111000001010010100000 ...

  8. Linux下安装和使用WPS,体验良好

    最近,我在ubuntu18.04.3下面使用LibreOffice,感觉良好. 正值政府机关在进行2019年度正版软件使用情况整改,保护知识产权,我表示热烈欢迎并强烈支持. 通过摸底,因为以前采购的w ...

  9. <automate the boring stuff with python>---第七章 正则实例&正则贪心&匹配电话号码和邮箱

    第七章先通过字符串查找电话号码,比较了是否使用正则表达式程序的差异,明显正则写法更为简洁.易扩展.模式:3 个数字,一个短横线,3个数字,一个短横线,再是4 个数字.例如:415-555-4242 i ...

  10. android 网络异步加载数据进度条

    ProgressDialog progressDialog = null; public static final int MESSAGETYPE = 0; private void execute( ...