Docker学习笔记4: Docker-Compose—简化复杂容器应用的利器
本文转载自http://www.tuicool.com/articles/AnIVJn. 因Python语言,个人也没学过,不是太熟悉,这篇文章的代码格式排版不准确爆了很多错,让我走了好多坑,不过还是要感谢原文作者。
在CentOS7系统上安装了docker-compose之后,我们以一个小例子来熟悉一下这个工具的使用.
Docker-Compose使用
[root@localhost ~]# cd dockerfile_dir/
[root@localhost dockerfile_dir]# ll
total 16
-rw-r--r--. 1 root root 313 Oct 31 09:52 app.py
-rw-r--r--. 1 root root 222 Oct 31 09:21 docker-compose.yaml
-rw-r--r--. 1 root root 79 Oct 31 09:42 Dockerfile
-rw-r--r--. 1 root root 12 Oct 31 08:19 requirements.txt
[root@localhost ~]# cd dockerfile_dir/
创建Dockerfile文件,内容如下:
FROM python:2.7
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
其中,requirements.txt中的内容包括:
flask
redis
from flask import Flask
from redis import Redis
import os
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)
(2)第二步、用一个compose.yaml来定义你的应用,他们可以在下个互隔离的容器中组成你的应用
web:
build: .
command:
python app.py
ports:
- "5000:5000"
volumes:
- .:/code
links:
- redis
redis: image: redis
注意,以上内容必须对齐,短杠和后面的字符是分离的,有一个空格.
[root@localhost dockerfile_dir]# docker-compose up
Building web
Step 1 : FROM python:2.7
---> 77cf0ea98df6
Step 2 : ADD . /code
---> 75cd864e0ccb
Removing intermediate container ac7333aef426
Step 3 : WORKDIR /code
---> Running in 5b5313e1b03c
---> e972204373ea
Removing intermediate container 5b5313e1b03c
Step 4 : RUN pip install -r requirements.txt
---> Running in 8b5e6cb8288a
Collecting flask (from -r requirements.txt (line 1))
Downloading Flask-0.11.1-py2.py3-none-any.whl (80kB)
Collecting redis (from -r requirements.txt (line 2))
Downloading redis-2.10.5-py2.py3-none-any.whl (60kB)
Collecting itsdangerous>=0.21 (from flask->-r requirements.txt (line 1))
Downloading itsdangerous-0.24.tar.gz (46kB)
Collecting Jinja2>=2.4 (from flask->-r requirements.txt (line 1))
Downloading Jinja2-2.8-py2.py3-none-any.whl (263kB)
Collecting Werkzeug>=0.7 (from flask->-r requirements.txt (line 1))
Downloading Werkzeug-0.11.11-py2.py3-none-any.whl (306kB)
Collecting click>=2.0 (from flask->-r requirements.txt (line 1))
Downloading click-6.6.tar.gz (283kB)
Collecting MarkupSafe (from Jinja2>=2.4->flask->-r requirements.txt (line 1))
Downloading MarkupSafe-0.23.tar.gz
Building wheels for collected packages: itsdangerous, click, 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/fc/a8/66/24d655233c757e178d45dea2de22a04c6d92766abfb741129a
Running setup.py bdist_wheel for click: started
Running setup.py bdist_wheel for click: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/b0/6d/8c/cf5ca1146e48bc7914748bfb1dbf3a40a440b8b4f4f0d952dd
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/a3/fa/dc/0198eed9ad95489b8a4f45d14dd5d2aee3f8984e46862c5748
Successfully built itsdangerous click MarkupSafe
Installing collected packages: itsdangerous, MarkupSafe, Jinja2, Werkzeug, click, flask, redis
Successfully installed Jinja2-2.8 MarkupSafe-0.23 Werkzeug-0.11.11 click-6.6 flask-0.11.1 itsdangerous-0.24 redis-2.10.5
---> cbdb581853a2 Starting dockerfiledir_redis_1
Starting dockerfiledir_web_1
Attaching to dockerfiledir_redis_1, dockerfiledir_web_1
redis_1 | 1:C 31 Oct 13:52:41.359 # 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.4 (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 31 Oct 13:52:41.363 # 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 31 Oct 13:52:41.363 # Server started, Redis version 3.2.4
redis_1 | 1:M 31 Oct 13:52:41.363 # 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 31 Oct 13:52:41.364 # 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 31 Oct 13:52:41.364 * DB loaded from disk: 0.000 seconds
redis_1 | 1:M 31 Oct 13:52:41.364 * The server is now ready to accept connections on port 6379
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1 | * Restarting with stat
web_1 | * Debugger is active!
web_1 | * Debugger pin code: 160-426-538
至此,两个应用就跑起来了,演示完毕.
3. Yaml文件参考
在上面的yaml文件中,我们可以看到compose文件的基本结构。首先是定义一个服务名,下面是yaml服务中的一些选项条目:
image
:镜像的ID
build
:直接从pwd的Dockerfile来build,而非通过image选项来pull
links
:连接到那些容器。每个占一行,格式为SERVICE[:ALIAS],例如 – db[:database]
external_links
:连接到该compose.yaml文件之外的容器中,比如是提供共享或者通用服务的容器服务。格式同links
command
:替换默认的command命令
ports
: 导出端口。格式可以是:
ports:-"3000"-"8000:8000"-"127.0.0.1:8001:8001"
expose
:导出端口,但不映射到宿主机的端口上。它仅对links的容器开放。格式直接指定端口号即可。
volumes
:加载路径作为卷,可以指定只读模式:
volumes:-/var/lib/mysql
- cache/:/tmp/cache
-~/configs:/etc/configs/:ro
volumes_from
:加载其他容器或者服务的所有卷
environment:- RACK_ENV=development
- SESSION_SECRET
env_file
:从一个文件中导入环境变量,文件的格式为RACK_ENV=development
extends
:扩展另一个服务,可以覆盖其中的一些选项。一个sample如下:
common.yml
webapp:
build:./webapp
environment:- DEBUG=false- SEND_EMAILS=false
development.yml
web:extends:
file: common.yml
service: webapp
ports:-"8000:8000"
links:- db
environment:- DEBUG=true
db:
image: postgres
net
:容器的网络模式,可以为”bridge”, “none”, “container:[name or id]”, “host”中的一个。
dns
:可以设置一个或多个自定义的DNS地址。
dns_search
:可以设置一个或多个DNS的扫描域。
其他的 working_dir, entrypoint, user, hostname, domainname, mem_limit, privileged, restart, stdin_open, tty, cpu_shares
,和 docker run
命令是一样的,这些命令都是单行的命令。例如:
cpu_shares:73
working_dir:/code
entrypoint: /code/entrypoint.sh
user: postgresql
hostname: foo
domainname: foo.com
mem_limit:1000000000
privileged:true
restart: always
stdin_open:true
tty:true
4. docker-compose常用命令
在第二节中的 docker-compose up
,这两个容器都是在前台运行的。我们可以指定-d命令以daemon的方式启动容器。除此之外,docker-compose还支持下面参数:
--verbose
:输出详细信息
-f
制定一个非docker-compose.yml命名的yaml文件
-p
设置一个项目名称(默认是directory名)
docker-compose的动作包括:
build
:构建服务
kill -s SIGINT
:给服务发送特定的信号。
logs
:输出日志
port
:输出绑定的端口
ps
:输出运行的容器
pull
:pull服务的image
rm
:删除停止的容器
run
: 运行某个服务,例如docker-compose run web python manage.py shell
start
:运行某个服务中存在的容器。
stop
:停止某个服务中存在的容器。
up
:create + run + attach容器到服务。
scale
:设置服务运行的容器数量。例如:docker-compose scale web=2 worker=3
参考:
Docker学习笔记4: Docker-Compose—简化复杂容器应用的利器的更多相关文章
- DOCKER 学习笔记9 Kubernetes (K8s) 弹性伸缩容器 下
前言 从上一篇看来,我们已经对于Kubernetes ,通过minikube 建立集群,而后使用kubectl 进行交互,对Deployment 部署以及服务的暴露等.这节,将学习弹性的将服务部署到多 ...
- Docker学习笔记2: Docker 概述
一.什么是Docker Docker是基于Go语言实现的云开源项目. Docker 的主要目标是:"Bulid,Ship and Run Any App ,AnyWhere" , ...
- Docker学习笔记之Docker应用于服务化开发
0x00 概述 上一节里我们谈到了小型的独立项目如何使用 Docker Compose 来搭建程序的运行环境,对于由多人或多部门参与的中大型服务化架构的项目,仅由一个 Docker Compose 项 ...
- Docker学习笔记之Docker 的简历
0x00 概述 在了解虚拟化和容器技术后,我们就更容易理解 Docker 的相关知识了.在这一小节中,我将介绍关于 Docker 的出现和发展,Docker 背后的技术.同时,我们将阐述 Docker ...
- DOCKER 学习笔记7 Docker Machine 在阿里云实例化ECS 以及本地Windows 实例化虚拟机实战
前言 通过以上6小节的学习,已经可以使用DOCKER 熟练的部署应用程序了.大家都可以发现使用 DOCKER 带来的方便之处,因为现在的话,只是在一台服务器上部署,这样部署,我们只需要一条命令,需要的 ...
- DOCKER 学习笔记7 Docker Machine 建立虚拟机实战,以及错误总结
前言 通过以上6小节的学习,已经可以使用DOCKER 熟练的部署应用程序了.大家都可以发现使用 DOCKER 带来的方便之处,因为现在的话,只是在一台服务器上部署,这样部署,我们只需要一条命令,需要的 ...
- DOCKER 学习笔记8 Docker Swarm 集群搭建
前言 在前面的文章中,已经介绍如何在本地通过Docker Machine 创建虚拟Docker 主机,以及也可以在本地Windows 创建虚拟主机,也是可以使用的.这一节,我们将继续学习 Docker ...
- Docker学习笔记(1) — docker 常用命令
1. docker version显示 Docker 版本信息.2. docker info显示 Docker 系统信息,包括镜像和容器数.3. docker searchdocker search ...
- Docker学习笔记之docker volume 容器卷的那些事(一)
预览目录 volume 方式 相关用例 使用方式 使用 volume driver bind mount 方式 相关用例 使用方式 配置selinux标签 配置macOS的安装一致性 tmpfs 方式 ...
- Docker学习笔记之Docker的Build 原理
0x00 概述 使用 Docker 时,最常用的命令无非是 docker container 和 docker image 相关的子命令,当然最初没有管理类命令(或者说分组)的时候,最常使用的命令也无 ...
随机推荐
- 【转载】C++基本功和 Design Pattern系列 ctor & dtor
最近实在是太忙了,无工夫写呀.只能慢慢来了.呵呵,今天Aear讲的是class.ctor 也就是constructor, 和 class.dtor, destructor. 相信大家都知道const ...
- HBase表创建、删除、清空
HBase shell窗口进入 执行命令hbase shell HBase表的创建 # 语法:create <table>, {NAME => <family>, VER ...
- 二 Djano模型层之模型字段选项
字段选项 以下参数是全部字段类型都可用的,而且是可选的 null 如果为True,Django将在数据库中将空值存储为NULL.默认值为False 对于字符串字段,如果设置了null=True意味着& ...
- java利用poi来读取execl表格返回对象
利用poi来读取execl表格,返回一个对象(可能有点不完善,但是应该能满足平常的所用),用到了反射等等; 使用的jar包有: commons-collections4-4.1.jar poi-3.1 ...
- 机器学习技法:09 Decision Tree
Roadmap Decision Tree Hypothesis Decision Tree Algorithm Decision Tree Heuristics in C&RT Decisi ...
- day 1——字典树练习
cojs 173. 词链 ★☆ 输入文件:link.in 输出文件:link.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述]给定一个仅包含小写字母的英文单词表, ...
- bzoj3156防御准备 斜率优化dp
3156: 防御准备 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2279 Solved: 959[Submit][Status][Discuss ...
- 基于 Hexo + GitHub Pages 搭建个人博客(三)
一.添加扫描二维码关注功能 打开 themes 目录下的 next 主题配置文件,找到 Wechat Subscriber 标签,将该标签下的配置改成如下形式: # Wechat Subscriber ...
- 在 Mac OS X 上安装 TensorFlow
在 Mac OS X 上安装 TensorFlow 这个文档说明了如何在 Mac OS X 上安装 TensorFlow. 注意:从 1.2 版本开始,在 Mac OS X 上 TensorFlow ...
- Hinton's paper Dynamic Routing Between Capsules 的 Tensorflow , Keras ,Pytorch实现
Tensorflow 实现 A Tensorflow implementation of CapsNet(Capsules Net) in Hinton's paper Dynamic Routing ...