博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Docker 容器
阅读量:4556 次
发布时间:2019-06-08

本文共 3678 字,大约阅读时间需要 12 分钟。

原文:

1.  容器

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

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

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

2.  用Dockerfile定义一个容器

Dockerfile

示例:

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

# Use an official Python runtime as a parent imageFROM python:2.7-slim# Set the working directory to /appWORKDIR /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

FlaskRedis

app.py

from flask import Flaskfrom 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 = "cannot connect to Redis, counter disabled" html = "

Hello {name}!

" \ "Hostname: {hostname}
" \ "Visits: {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

$ lsDockerfile		app.py			requirements.txt
docker build --tag=friendlyhello .
$ docker image lsREPOSITORY            TAG                 IMAGE IDfriendlyhello         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.02 git push origin v1.0 

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

 

7.  备忘单

docker build -t friendlyhello .  # Create image using this directory's Dockerfiledocker 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 
# Gracefully stop the specified container docker container kill
# Force shutdown of the specified container docker container rm
# 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
# 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
username/repository:tag # Tag
for upload to registry docker push username/repository:tag # Upload tagged image to registry docker run username/repository:tag # Run image from a registry

 

posted on
2019-04-29 08:27 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/lonelyxmas/p/10788331.html

你可能感兴趣的文章
Educational #39C
查看>>
threejs学习笔记(7)
查看>>
NOIP2016
查看>>
docker使用笔记
查看>>
推荐一款在IntelliJ IDEA中使用微信/QQ的插件
查看>>
github简单操作
查看>>
ZendStudio10 代码格式化 xml
查看>>
TWaver动画之雷达扫描效果
查看>>
linux压缩及vi操作
查看>>
蓝桥杯java 基础练习 FJ的字符串
查看>>
jQuery文档处理
查看>>
[国嵌攻略][119][Linux中断处理程序设计]
查看>>
upstream实现内网网站在公网访问
查看>>
排序——归并排序
查看>>
JAVA实现对服务器的访问的两种方法
查看>>
搭建GitLab的Hexo博客记录
查看>>
NYOJ 496 [巡回赛-拓扑排序]
查看>>
redis五种数据类型的使用
查看>>
Form表单中的onClick,onSubmit和submit
查看>>
Python-SocketServer源码
查看>>