1.  容器

在过去,如果要开始编写Python应用程序,首先要做的就是在机器上安装Python运行时环境。但是,这就造成了这样一种情况:你的机器上的环境需要完美,以便你的应用程序能够按预期运行,而且还需要与你的生产环境相匹配。

使用Docker,你只需要获取一个可移植的Python运行时作为镜像,不需要安装。然后,当你构建应用程序时就会在代码旁边包含基本的Python镜像,确保应用程序、它的依赖项和运行时一起运行。

这些可移植的镜像被一些称之为“Dockerfile”来定义。

2.  用Dockerfile定义一个容器

Dockerfile

示例:

在你的本地机器上创建一个空目录,进入该目录,然后在此目录下创建一个名字叫Dockerfile的文件,将下列内容复制粘贴到文件中,保存。

# Use an official Python runtime as a parent image
FROM python:2.7-slim # Set the working directory to /app
WORKDIR /app # Copy the current directory contents into the container at /app
COPY . /app # Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt # Make port 80 available to the world outside this container
EXPOSE 80 # Define environment variable
ENV NAME World # Run app.py when the container launches
CMD ["python", "app.py"]

可以看到,Dockerfile文件需要引用app.py和requirements.txt文件,于是,在与Dockerfile同级的目录下创建这两个文件

requirements.txt

Flask
Redis

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)

构建App

$ ls
Dockerfile app.py requirements.txt
docker build --tag=friendlyhello .
$ docker image ls

REPOSITORY            TAG                 IMAGE ID
friendlyhello latest 326387cea398

注意:tag默认是latest,当然你可以手动指定,比如:--tag=friendlyhello:v0.0.1

3.  运行App

运行app,将本地4000端口映射到容器对外公布的80端口

docker run -p 4000:80 friendlyhello

4.  共享你的镜像

为了能够在任何地方都运行,我们需要将我们的镜像上传到注册中心。注册中心是仓库的集合,而仓库是镜像的集合。这很像GitHub仓库或者Maven仓库。一个账号可以在注册中心中创建许多个仓库。

用Docker ID登录

$ docker login

5.  给镜像打标签

将本地镜像关联到注册中心的某个仓库,格式是这样的: username/repository:tag

其中,tag是可选的,但是推荐加上tag。当你关联到仓库后,注册中心会给这个镜像分配一个版本号

给镜像打Tag的格式如下:

docker tag image username/repository:tag

例如,我们给我们刚才的friendlyhello打一个tag

docker tag friendlyhello gordon/get-started:part2

6.  发布镜像

docker push username/repository:tag

(PS:这个过程很像git在本地打标签并推送到远程仓库)

1 git tag v1.0
2 git push origin v1.0 

推送成功以后,我们就可以在注册中心看到了

7.  备忘单

docker build -t friendlyhello .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello # Run "friendlyhello" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello # Same thing, but in detached mode
docker container ls # List all running containers
docker container ls -a # List all containers, even those not running
docker container stop <hash> # Gracefully stop the specified container
docker container kill <hash> # Force shutdown of the specified container
docker container rm <hash> # Remove specified container from this machine
docker container rm $(docker container ls -a -q) # Remove all containers
docker image ls -a # List all images on this machine
docker image rm <image id> # Remove specified image from this machine
docker image rm $(docker image ls -a -q) # Remove all images from this machine
docker login # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag # Tag <image> for upload to registry
docker push username/repository:tag # Upload tagged image to registry
docker run username/repository:tag # Run image from a registry

Docker 容器的更多相关文章

  1. docker——容器安装tomcat

    写在前面: 继续docker的学习,学习了docker的基本常用命令之后,我在docker上安装jdk,tomcat两个基本的java web工具,这里对操作流程记录一下. 软件准备: 1.jdk-7 ...

  2. Docker容器环境下ASP.NET Core Web API应用程序的调试

    本文主要介绍通过Visual Studio 2015 Tools for Docker – Preview插件,在Docker容器环境下,对ASP.NET Core Web API应用程序进行调试.在 ...

  3. 在docker容器中vi指令找不到

    在使用docker容器时,有时候里边没有安装vi,敲vi命令时提示说:vi: command not found,这个时候就需要安装vi,可是当你敲apt-get install vi命令时,提示: ...

  4. 理解Docker(4):Docker 容器使用 cgroups 限制资源使用

    本系列文章将介绍Docker的有关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...

  5. docker学习(5) 在mac中创建mysql docker容器

    github上有一个专门的docker-libary项目,里面有各种各样常用的docker镜像,可以做为学习的示例,今天研究下其中mysql镜像的用法,国内镜像daocloud.io也能找到mysql ...

  6. 在Linux和Windows的Docker容器中运行ASP.NET Core

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 译者序:其实过去这周我都在研究这方面的内容,结果周末有事没有来得及总结为文章,Scott H ...

  7. 自定义Docker容器的 hostname

    自定义Docker容器的 hostname   作者: John Deng 原创内容,欢迎传播,请注明出处:http://www.cnblogs.com/johnd/p/set-docker-host ...

  8. Docker容器是否可以改变世界?

    Docker容器是否可以改变世界? 2016-01-15 杜亦舒 2016年了,很多大牛开始预测技术趋势,其中一个普遍的观点我也很认同: Docker会更加流行,会改变程序世界 2015年的上半年我接 ...

  9. 搭建ubuntu14.04的hadoop集群【docker容器充当服务器】

    首先弄出来装有hadoop.java.ssh.vim的镜像起名badboyf/hadoop.做镜像有两种方法,一种是用Dockerfile来生成一个镜像,一种是基于ubuntu14.04的基础镜像生成 ...

  10. 在docker容器中安装和使用,linux版的powershell

    powershell 传教士 原创文章.始于 2016-09-18 ,2016-10-27修改powershell docker官网.允许转载,但必须保留名字和出处,否则追究法律责任 1 在任意版本的 ...

随机推荐

  1. mac os x 10.9.3 升级到10.10.4 记录

    昨天终于忍不住,将mac pro 的操作系统从 os x 10.9.3 升级到10.10.4,因为网络不给力,500k/s,光系统包都要5.6G,所以整整下来了一个工作白天,等下班的时候开始安装,不过 ...

  2. ThinkPHP5零基础搭建CMS系统(一)

    了解学习thinkphp5应该是2016年年底的事情,当时还没有接触过thinkphp3版本,觉得通过手册直接上手学习tp5蛮轻松的,现在从零记录下,搭建可扩展的CMS. 1.ThinkPHP环境搭建 ...

  3. 如何找某个样式属于哪个Element

    如果找不到样式所在的Element,那么可以参考排除法,逐个删除覆盖在同一位置的元素,如果该样式消失,那么可以判断为这个样式.

  4. Linux的安装(虚拟机环境)与基础配置

    一.背景 本文介绍如何安装虚拟机VMware以及如果在虚拟机上安装Linux系统以及Linux安装完毕之后的基础配置 需要准备的东西有VMware以及Linux镜像文件 二.下载安装VMware 下载 ...

  5. Visual Studio 和 c# 正则表达式

    今天集中说说VS生产环境下的正则. Visual Sturdio 2012以上版本查找替换 对于VS的正则,准确说,是VS2012之后的IDE下VS的正则. VS的查找和替换功能支持基础的正则表达式, ...

  6. Python 基础【一】

    python运行流程 一.变量及注释 命名: 合法-变量名由字母.数字和下划线组成,并且不能以数字开头.以下保留字不可以当变量名: ['False', 'None', 'True', 'and', ' ...

  7. SSIS 处理 bit 列

    一般的编程语言,例如,C#.Java和R等都有布尔类型,用于表示逻辑真(true)和假(false),SQL Server没有布尔类型,但是,在编程时,可以使用bit 类型来代替,bit类型只有两个有 ...

  8. vue 路由嵌套高亮问题

    正常路由嵌套是没有问题的,但是如果你已经在当前主路由页面了,然后再次点击主路由就会出现页面数据空白的情况 看代码: //主路由通过v-for循环出来 <div class="list- ...

  9. 接口调用(发送http请求)

    // 向对应的url地址发送http请求, 并获取响应的json字符串    public String getHttpResponse(String url) {        // result用 ...

  10. 向Oracle数据库插入中文乱码解决方法

    解决方法:    第一步:sqlplus下执行:select userenv('language') from dual;//查看oracle字符集     注:如果oracle字符集与后台代码设置的 ...