通过前面几篇文章,我们了解了docker的基本知识,docker帮我们解决了服务打包安装的问题,但是随着服务的不断增多带来了如下问题:
这时候docker compose的出现,Docker Compose可以做到以下几点:
Docker Compose有以下特征:
需求:使用docker-compose 搭建一个springboot+redis的项目
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author: lianghengyu* @time: 2022-12-07* @desc:*/
@RestController
public class MyDockerTest {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@RequestMapping("/user/query")public String query(){return "hello";}@GetMapping("/query/redis")public String queryCounter(){Long counter = stringRedisTemplate.opsForValue().increment("counter");return "页面访问的次数"+counter;}
}
server.port=8080 # 服务端口号
spring.redis.host=redis # 因为项目启动在容器中,所以直接配置容器中的redis即可
spring.redis.port=6379
使用maven ==> install 打包springboot项目 上传到服务器中
3 配置dockerfile文件
FROM java:8
MAINTAINER LHY
LABEL name='docker-test' version='1.0' author='lhy'
COPY docker-0.0.1-SNAPSHOT.jar springboot-mybatis.jar
CMD ["java","-jar","springboot-mybatis.jar"]
version: '2.0'
services:myapp:build: .image: myappdepends_on:- redisports:- "8080:8080"redis:image: "library/redis:alpine"
5 在当前位置执行 docker-compose up 启动compose
项目启动成功