docker 使用指南
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.
Dockerfile 可以让用户管理一个单独的应用容器;而 Compose 则允许用户在一个模板(YAML 格式)中定义一组相关联的应用容器(被称为一个 project,即项目),例如一个 Web 服务容器再加上后端的数据库服务容器等,就拿官网的 Python 例子来说,功能很简单,利用 redis 的 incr 的功能对页面的访问量进行统计。
docker-compose 的安装可以参考官网,如果安装了 Docker for Mac 或者 Docker for windows 则直接就存在了。
创建项目目录:
$ mkdir composetest
$ cd composetest
创建 Python 的程序 app.py ,功能就是利用 redis 的 incr 方法进行访问计数。
from flask import Flask
from redis import Redis
app = Flask(__name__)
redis = Redis(host='redis', port=6379)
@app.route('/')
def hello():
redis.incr('hits')
return 'Hello World! I have been seen %s times.' % redis.get('hits')
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
由于 Python 依赖的 flask、redis 组件都需要另外安装,比如使用 pip来安装,单独设置一文件 requirements.txt,内容如下:
flask
redis
创建 service 依赖的第一个 image,app.py 程序的运行环境,利用 Dockerfile 来制作,内容如下:
FROM python:2.7 #基于 python:2.7 镜像
ADD . /code #将本地目录中的内容添加到 container 的 /code 目录下
WORKDIR /code #设置程序工作目录为 /code
RUN pip install -r requirements.txt #运行安装命令
CMD python app.py #启动程序
Dockerfile 创建好就可以制作镜像了,运行docker build -t compose/python_app . ,成功后通过docker images查看即能看到:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
compose/python_app latest a92fed00abd 59 minutes ago 680.4 MB
接下来制作 docker-compose 需要的配置文件 docker-compose.yml, version 要选择 2 ,1的版本很古老了,配置中可以看到创建了 2 个 service,web 和 redis ,各自有依赖的镜像,其中web 开放 container 的5000端口,并与 host 的5000端口应对,方便通过 localhost:5000 来访问, volumes 即将本地目录中的文件加载到容器的 /code 中,depends_on 表名 services web 是依赖另一个 service redis的,完整的配置如下:
version: '2'
services:
web:
image: compose/python_app
ports:
- "5000:5000"
volumes:
- .:/code
depends_on:
- redis
redis:
image: redis
前提都搞定了,就差最后一步启动了,命令 docker-compose up ,成功后如下:
Attaching to composetestbypython_redis_1
redis_1 | 1:C 04 Nov 10:35:17.448 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | _._
redis_1 | _.-``__ ''-._
redis_1 | _.-`` `. `_. ''-._ Redis 3.2.5 (00000000/0) 64 bit
redis_1 | .-`` .-```. ```\/ _.,_ ''-._
redis_1 | ( ' , .-` | `, ) Running in standalone mode
redis_1 | |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
redis_1 | | `-._ `._ / _.-' | PID: 1
redis_1 | `-._ `-._ `-./ _.-' _.-'
redis_1 | |`-._`-._ `-.__.-' _.-'_.-'|
redis_1 | | `-._`-._ _.-'_.-' | http://redis.io
redis_1 | `-._ `-._`-.__.-'_.-' _.-'
redis_1 | |`-._`-._ `-.__.-' _.-'_.-'|
redis_1 | | `-._`-._ _.-'_.-' |
redis_1 | `-._ `-._`-.__.-'_.-' _.-'
redis_1 | `-._ `-.__.-' _.-'
redis_1 | `-._ _.-'
redis_1 | `-.__.-'
redis_1 |
redis_1 | 1:M 04 Nov 10:35:17.450 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 04 Nov 10:35:17.450 # Server started, Redis version 3.2.5
redis_1 | 1:M 04 Nov 10:35:17.451 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | 1:M 04 Nov 10:35:17.451 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1 | 1:M 04 Nov 10:35:17.451 * The server is now ready to accept connections on port 6379
此时通过compose 的 ps 命令也能看到 docker-compose ps :
docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------
composetestbypython_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
composetestbypython_web_1 /bin/sh -c python app.py Up 0.0.0.0:5000->5000/tcp
ok ,在浏览器中访问 http://localhost:5000 就能看到最终的样子啦。
{% qnimg docker/2016-11-04-22-40-11.jpg title: alt: 'class:' %}
docker-compose 还有很多实用的命令,比如 logs、build、start、stop 等,可以通过 docker-compose --help来查看:
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file (default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name (default: directory name)
--verbose Show more output
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the name specified
in the client certificate (for example if your docker host
is an IP address)
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pulls service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
更多 docker-compose 例子参考官网 doc 文档 :
- Docker-compose with Django : https://docs.docker.com/compose/django/
- Get started with Rails : https://docs.docker.com/compose/rails/
- Get started with WordPress : https://docs.docker.com/compose/wordpress/
docker 使用指南的更多相关文章
- Docker终极指南:为什么Docker能做这么多事
Docker终极指南:为什么Docker能做这么多事 http://www.aboutyun.com/thread-11499-1-1.html
- Docker 使用指南 (四)—— 数据卷的使用
一.数据卷的使用 有时候需要使用数据库,但是又希望它的数据能保存在本地,Docker中提供了数据卷可以供你方便的操作数据.数据卷是一个可供一个或多个容器使用的特殊目录,它绕过 UFS,可以提供很多有用 ...
- Docker 入门指南——常用命令
前面已经介绍了 Docker 的安装方式,本文总结一下使用 Docker 的基本概念和常用命令. 基本概念 镜像 Image 镜像是一些打包好的已有的环境,可以被用来启动和创建容器 容器 Contai ...
- [转载]Docker 完全指南
Docker 完全指南 原作者地址: https://wdxtub.com/2017/05/01/docker-guide/ 发表于 2017-05-01 | 更新于 2017-08-03 | ...
- Docker 入门指南
Docker 入门指南 目录 基础概念 安装教程 基本操作 常用安装 构建操作 容器编排 壹.基础概念 什么是Docker? Docker是基于Go开发的应用容器引擎,属于 Linux 容器的一种封装 ...
- 斌哥的 Docker 进阶指南
过去的一年中,关于 Docker 的话题从未断过,而如今,从尝试 Docker 到最终决定使用 Docker 的转化率依然在逐步升高,关于 Docker 的讨论更是有增无减.另一方面,大家的注意力也渐 ...
- docker入门指南(转载)
原文: http://bg.biedalian.com/2014/11/20/docker-start.html 关于 docker 今天云平台的同事提到, 现在的运维就是恶性循环, 因为大家都在申请 ...
- Docker 使用指南 (二)—— 搭建本地仓库
版权声明:本文由田飞雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/94 来源:腾云阁 https://www.qclou ...
- Docker 使用指南 (一)—— 基本操作
版权声明:本文由田飞雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/98来源:腾云阁 https://www.qcloud ...
- Docker 使用指南 (六)—— 使用 Docker 部署 Django 容器栈
版权声明:本文由田飞雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/98 来源:腾云阁 https://www.qclou ...
随机推荐
- Chrome 浏览器的简单设置 无痕模式 暗黑模式 自定义用户目录
1. Chrome73 新增加了暗黑模式 可以通过修改快捷方式的方式来默认开启方法如下 1.1 关闭浏览器 2.2 鼠标焦点定位到任务栏 Chrome 图标处, 并且按住shift 按键 执行右键操作 ...
- HTML5经典实例——1基础语法和语义
1指定DOCTYPE 在页面的最开始处指定HTML5 DOCTYPE DOCTYPE是不区分大小写的.可以任意的使用大小写. <!DOCTYPE html> <html lang=& ...
- DAY07、字符编码和文件操作
一.字符编码 1.什么是字符编码? 人类能识别的是字符等高级标识符,电脑只能识别0,1组成的标识符,要完成人与机器之间的信息交流, 一定需要一个媒介,进行两种标识符的转化(两 ...
- mongodb3的使用
1.在windows下载安装mongodb 将下载好的zip压缩文件解压并重命名为mongo-3.0.6,并在根目录下新建文件夹data用于存放数据 2.启动mongod守护进程 使用命令mongod ...
- CF 1041 1042整理
终于回来整理了,这两场比赛我也是醉了,第一场-1分,第二场被skip,还是太菜啊qaq CF1041 T1T2过水忽略直接看后面 T3大意:给你一个长度为n的序列a1,a2,a3···an,你需要把这 ...
- 找工作的程序员必懂的Linux
一.为什么要学习Linux 首先,我想先说一下:“为什么要学习Linux”?Linux 是什么,它是一款操作系统,是一个支持多用户.多任务.支持多线程和多CPU的操作系统:32位和64位的硬件可以在L ...
- 浅析Android设备中grep命令处理流程
2017-04-18 概述 在TV开发板中,可以在串口中直接使用grep命令.这是因为在/system/bin/下有一个'grep'链接.这个链接指向'/system/bin/toolbo ...
- Android Studio 显示 logcat
首先调出 logcat,在整个android studio的左下角(藏的好,尼玛.) 接着设置下面三个红框 1 选择你自己的模拟器. 2 选择Warm 3 选择not filter
- 【BZOJ3522】【BZOJ4543】【POI2014】Hotel 树形DP 长链剖分 启发式合并
题目大意 给你一棵树,求有多少个组点满足\(x\neq y,x\neq z,y\neq z,dist_{x,y}=dist_{x,z}=dist_{y,z}\) \(1\leq n\leq 1 ...
- 如何判断是否为同一个App,Ionic3如何修改包名
如何判断是否同一个App 使用Ionic3创建了两个项目demo1.demo2,然后使用同一个JDK,生成了两个不同的keystore证书. 结果在手机端安装的时候,先安装demo1,没有任何替换的提 ...