1 Docker Compose
官方文档:https://docs.docker.com/compose/install/
1.1 简介
定义、运行多个容器
YAML file配置文件
single command 命令有哪些
作用:批量容器编排
Compose
version: '2.0'
services:
web:
build:.
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
1.2 安装
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.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
授权
sudo chmod +x /usr/local/bin/docker-compose
1.3 yaml规则
https://docs.docker.com/compose/compose-file/compose-file-v3/
# 3层
version: ' ' # 版本
services: # 服务
服务1:web
# 服务配置
images
build
network
...
服务2:redis
...
服务2:redis
...
# 其他配置 网路/卷、全局规则
volumes:
networks:
configs:
2 官方 实战 flask
2.1 创建项目文件
https://docs.docker.com/compose/gettingstarted/
1、创建项目
mkdir /home/composetest
cd /home/compose
2、创建 app.py
import time
import redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
3、requirements.txt
flask
redis
2.2 创建Dockerfile
# syntax=docker/dockerfile:1
FROM python:3.8-alpine
ADD . /code
WORKDIR /code
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
从 Python 3.7 映像开始构建映像。
将工作目录设置为/code.
设置命令使用的环境变量flask。
安装 gcc 和其他依赖项
复制requirements.txt并安装 Python 依赖项。
向镜像添加元数据以描述容器正在侦听端口 5000
将项目中的当前目录复制.到镜像中的workdir .。
将容器的默认命令设置为flask run.
2.3 在Compose文件中定义service
vim docker-compose.yml
version: "3.3"
services:
web:
build: . # 自己构建镜像
ports:
- "5000:5000"
redis:
image: "redis:alpine" # 拉取镜像
2.4 Build and run your app with Compose
构建并运行
docker-compose up
构建并运行 -- 重构
docker-compose up --build
构建并运行 - - 重构 - 后台运行
docker-compose up --build -d
停止:
docker-compose down
docker-compose
以前都是单个docker run 启动容器
docker-compose 通过docker-compose编写yaml配置文件、可以通过compose 一键启动所有服务,停止
3 实战 博客 wordpress
https://docs.docker.com/samples/wordpress/
mkdir my_wordpress
cd my_wordpress
3.1 docker-compose.yml
version: "3.3"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
wordpress_data: {}
启动
docker-compose up
4 实战 spring-boot
4.1 编辑代码
勾选依赖
Web -> Spring Web
NoSQL -> Spring Data Redis
application.properties
server.port=8080
spring.redis.host=redis
HelloController.java
package com.example.springboot.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello charles";
}
}
打包项目
4.2 编辑dokcer文件
Dockerfile
FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yml
编排项目
version: '3.3'
services:
vitcloud-spring-demo:
build: .
# dockerfile: Dockerfile # 指定Dokcerfile
image: spring-demo-images
depends_on:
- redis
ports:
- "8080:8080" # 外部地址:内部地址
redis:
# image:"library/redis:alpine"
image: redis
4.3 启动项目
丢到服务器
- demo-0.0.1-SNAPSHOT.jar
- docker-compose.yml
- Dockerfile
启动
docker-compose up
docker-compose up --build -d
--build
:重新构建-d
:后台模式