docker-compose实战部署项目

第一步、编写简单的springboot项目(引入web和redis)

编写一个controller,用于外部访问,当用户访问hello的时候,redis自增,并返回浏览量

@RestController
@RequestMapping("app/")
public class HelloController {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping("hello")
    public String hello()
    {
        Long view = stringRedisTemplate.opsForValue().increment("views");
        return "当前浏览量为:"+view;
    }
}

配置项目端口,能保证项目在本地能运行


第二步、编写Dockerfile,用来构建镜像


第三步、编写docker-compose.yml,进行项目的编排和启动


第四步、丢到服务器,使用docker-compose up启动

修改yml配置,因为服务之间的访问不能用localhost,所以改用服务名的方式访问

打好jar包

将jar包、Dockerfile、docker-compose.yml上传到服务器的文件夹下

进入当前目录,使用docker-compose up前台启动 或者 docker-compose up -d后台启动

可以看到项目启动成功

我们通过外网也成功进行了访问


最后我们查看镜像,可以看到总共构建了3个镜像

我们再查看容器,可以看到两个容器被创建

猜你喜欢

转载自blog.csdn.net/bbj12345678/article/details/114221610