Dockerfile build run 手动操作,单个容器!
微服务,100个微服务,依赖关系。
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.
翻译:
定义运行多个容器
YAML file配置文件
single command。命令有哪些?
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.
三步骤:
Dockerfile保证我们的项目再任何地方可以运行
services 什么是服务。
作用:批量容器编排 启动项目
Compose是Docker官方的开源项目,需要安装!
Dockerfile让程序在任何地方运行。web服务、redis、mysql、nginx… 多个容器。 run
给个例子看看:
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:重要概念
# 官网提供 (没有下载成功)
curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 国内地址
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
地址:https://docs.docker.com/compose/gettingstarted/
python应用。 计数器。redis!
FROM python:3.6-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
# 官网的用来flask框架,我们这里不用它
# 这告诉Docker
# 从python3.7开始构建镜像
# 将当前目录添加到/code印像中的路径中
# 将工作目录设置为/code
# 安装Python依赖项
# 将容器的默认命令设置为python app.py
version: '3.8'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: "redis:alpine"
流程:
创建网络
执行Docker-compose.yaml
启动服务
Docker镜像,run =>容器
DockerFile 构建镜像(服务打包)
Docker-composer
Docker网络
docker-compose.yaml 核心!
https://docs.docker.com/compose/compose-file/compose-file-v3/
# 3层!
version:'' # 版本
services # 服务
服务1:web
# 服务配置
images
build
network
......
例子
version: "3"
services:
web:
build: # 构建镜像
context: ./ # 上下文环境
dockerfile: ./compose/node/Dockerfile # Dockerfile路径
ports:
- "3000:3000"
volumes:
- static:/code/static # 使用前面声明的static挂在容器/code/static
restart: always # 总是重启
nginx:
build:
context: ./
dockerfile: ./compose/nginx/Dockerfile
ports:
- "80:80"
volumes:
- static:/code/static
restart: always
mysql:
image: mysql:5.7 # 直接使用镜像构建
env_file: .env # 环境变量env,我们写入到了配置文件里,避免密码泄漏
volumes:
- db:/var/lib/mysql
ports:
- "3306:3306"
restart: always
volumes:
static: # 数据卷名称
db: # 数据卷名称
https://docs.docker.com/compose/wordpress/
下载程序、安装数据库、配置…
compose应用 => 一键启动
下载项目(docker-compse.yaml)
如果需要文件。Dockerfile
文件准备齐全,一键启动项目即可
FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]
docker-compose.yml编排项目
version '3.8'
services:
xiaofanapp:
build: .
image: xiaofanapp
depends_on:
- redis
ports:
- "8080:8080"
redis:
image: "library/redis:alpine"
docker-compose down # 关闭容器
docker-compose up --build # 重新构建
总结:
工程、服务、容器
项目 compose: 三层
工程 Project
服务
容器 运行实例! docker k8s 容器
后面可能直接学k8s就不更这一系列了.
linux入门–服务器购买—宝塔部署环境说明
Nginx简单入门–学习笔记狂神说
Docker基础01–入门总结–(狂神说docker学习笔记)
Docker基础02–Docker容器数据卷详解–(狂神说docker学习笔记)
Docker基础03–Dockerfile详解与镜像发布–(狂神说docker学习笔记)
Docker基础04–Docker网络–(狂神说docker学习笔记)
Docker进阶01–Docker Compose–(狂神说docker学习笔记)
Docker知识点翻阅手册–Docker常用命令、Dockererfile、Compose、网络等整理合集
Docker实战:Mysql、Nginx、web的Docker化部署(安装、自定义镜像、compose管理容器、自定义网络、部署问题及解决)