一、Overview of Docker Compose
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.
Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.
Using Compose is basically a three-step process:
-
Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
-
Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
-
Run docker-compose up and Compose starts and runs your entire app.
即:
Using Compose is basically a three-step process:
- Dockerfile保证我们的项目在任何地方运行
- services 什么是服务
- docker-compose.yml文件怎么编写
- docker-compose up 启动项目
作用:批量容器编排
Compose是Docker官方的开源项目
,需要安装
Dockerfile让程序在任何地方运行,web服务,redis,mysql,nginx…多个容器
Compose:
A docker-compose.yml looks like this:
version: '2.0'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
docker-compose up 100个服务
Compose重要概念:
- 服务service,容器,应用(web redis mysql)
- 项目project。一组关联的容器,博客,web,mysql
-
安装Compose
- 通过官网:
https://docs.docker.com/compose/install/ - 参考:
https://blog.csdn.net/fxbin123/article/details/104941460/
- 通过官网:
-
体验一把
https://docs.docker.com/compose/gettingstarted/
小结:
- Docker镜像 run =》容器
- Dockerfile构建镜像(服务打包)
- docker-compose 启动项目(编排、多个微服务环境)
- Docker网络
yaml规则
docker-compose.yaml(核心)
3层:
version: 版本
service: 服务
web: 服务1
# 服务配置
images
build
network
…
redis: 服务2
…
服务3
#其他配置 网络、卷、全局规则
volumes:
networks:
configs:
-
官方文档
https://docs.docker.com/compose/compose-file/#expose -
开源项目compose.yaml
开源项目:
博客
compose 应用 =>一键启动!
- 下载项目(docker-compose.yaml)
- 如果需要文件 Dockerfile
- 文件准备齐全(直接一键启动项目)
不加 -d 前台启动
docker -d 后台启动
开启:
docker-compose up -d
停止:
docker-compose down
掌握:docker 基础,原理,网络,服务,集群,错误排查,日志
linux docker k8s
实战:计数器
- 编写项目微服务
- Dockerfile构建镜像demo
- docker-compose.yaml 编排项目
- 丢到服务器 docker-compose up
假设项目要重新部署打包,使用如下命令,即重新构建
docker-compose up --build
小结:
未来项目只要有docker-compose 文件,按照这个规则,启动编排容器
公司:docker-compose 直接启动
网上开源项目:docker-compose 一键搞定
总结:
工程 服务 容器
项目compose :三层
- 工程
- 服务
- 容器 运行实例 ! docker k8s 容器