Docker Compose
介绍
Docker Compose是为了更加轻松高效的管理多个容器。可以自动的进行容器编排
安装
- 下载Docker-Compose
# 安装docker-compose
sudo curl -L "https://get.daocloud.io/docker/compose/releases/download/1.28.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
显示安装成功
- 授权
sudo chmod +x docker-compose
体验
- 创建一个文件夹
mkdir composetest
cd composetest
- 编写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)
- 创建依赖
vim requirements.txt
# 保存下面两个依赖
flask
redis
- 创建Dockerfile
vim Dockerfile
# 保存下面值
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
- 编写docker-compose.yml 文件
version: "3.9"
# 表示有两个服务web服务,和Redis服务
# web服务用Dockerbuild 命令自定义 redis用官方提供的镜像
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
- 使用docker-compose up 命令运行
- 访问成功
yaml规则
docker-compose核心!
docker-compose配置详解:https://docs.docker.com/compose/compose-file/compose-file-v3/
#核心总共有三个 版本 服务 其他配置
#版本
version: ''
#服务
services:
服务1: web
image
build
network
服务2: web
image
build
network
.....
# 其他配置
network:
volumes:
configs:
:
WordPress搭建
- 创建目录
[root@pinyoyougou-docker /]# mkdir my_wordpress
[root@pinyoyougou-docker /]# cd my_wordpress
- 编写yaml脚本
version: "3.9"
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:
#依赖db 让db服务先运行
depends_on:
- db
#镜像
image: wordpress:latest
#卷挂载
volumes:
- wordpress_data:/var/www/html
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
wordpress_data: {}
- 运行docker-compose命令
docker-compose up -d #后台运行
实战
- 创建springboot项目
2. 编写代码
package com.fyx.dockercompose.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author yuanxin
* @Date 2021/7/2 15:52
**/
@RestController("/test")
public class RedisController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping(value = "incr")
public Long test() {
Long add = redisTemplate.opsForValue().increment("add");
return add;
}
}
- 编写dockerfile
FROM java:8
MAINTAINER 1349516890@qq.com
COPY ./target/docker-compose.jar ./
EXPOSE 8080
ENTRYPOINT ["java","-jar","docker-compose.jar"]
- 编写docker-compose
version: "3.9"
services:
my-docker-compose:
build: .
image: my-docker-compose
depends_on:
- redis
ports:
- "8080:8080"
redis:
image: "library/redis:alpine"
- java 配置
# 注意这里的ip 得是服务名 因为Docker内部网络可以通过域名访问
spring.redis.host=redis
spring.redis.port=6379
-
打包java服务把Dockerfile,docker-compose 放到Linux服务器上执行docker-compose up
-
访问成功
注意
如需重新加载app资源需要使用docker-compose up --build -d
-d 后台运行
–build 重新构建镜像