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 相关的子命令,当然最初没有管理类命令(或者说分组)的时候,最常使用的命令也无 ...
随机推荐
- DbContext(String)+SqlQuery一起使用
DbContext(String) 可以将给定字符串用作将连接到的数据库的名称或连接字符串来构造一个新的上下文实例. Database.SqlQuery 方法 (Type, String, Objec ...
- HTML笔记05------AJAX
AJAX初探01 AJAX概念 概念:即"Asynchronous JavaScript And XML" 通过在后台与服务器进行少量数据交换,AJAX可以使网页实现异步更新.这意 ...
- [LeetCode] Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- [LeetCode] Number Complement 补数
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- WebApi-路由机制
一.WebApi路由机制是什么? 路由机制通俗点来说:其实就是WebApi框架将用户在浏览器中输入的Url地址和路由表中的路由进行匹配,并根据最终匹配的路由去寻找并匹配相应的Controller和Ac ...
- hibernate--HelloWorld
本次学习版本:hibernate-release-5.2.6.Final,要求java 1.8 和JDBC 4.2. hibernate是一个开放源代码的对象关系映射框架.对JDBC进行了非常轻量的封 ...
- [USACO 06NOV]Corn Fields
Description 题库链接 给你一个 \(0,1\) 矩阵,只准你在 \(1\) 上放物品:并且要满足物品不能相邻.允许空放,问方案数,取模. \(1\leq n,m\leq 12\) Solu ...
- [HNOI2008]神奇的国度
题目描述 K国是一个热衷三角形的国度,连人的交往也只喜欢三角原则.他们认为三角关系:即AB相互认识,BC相互认识,CA相互认识,是简洁高效的.为了巩固三角关系,K国禁止四边关系,五边关系等等的存在. ...
- codeforces 868B Race Against Time
Have you ever tried to explain to the coordinator, why it is eight hours to the contest and not a si ...
- [CEOI2008]order
Description 有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成,你可以通过购买或租用机器来完成. 现在给出这些参数,求最大利润 Solu ...