Docker Compose入门指南

为什么要使用Docker Compose

可以使用Dockerfile(或Maven)构建镜像,使用微服务架构的应用系统一般包含若干个微服务,每个微服务一般有多个实例。如果每个微服务都要手动启停那么维护成本会非常高,docker compose只能控制当前主机的服务,无法控制集群

安装Compose

方法一:

# curl -L https://github.com/docker/compose/releases/download/1.8.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose 

# chmod +x /usr/local/bin/docker-compose
查看版本信息
# docker-compose --version
但是此方法会经常因为网络的原因而无法安装

方法二: 

1、安装python-pip

yum -y install epel-release

yum -y install python-pip

2、安装docker-compose

pip install docker-compose

待安装完成后,执行查询版本的命令,即可安装docker-compose

docker-compose version

入门示例

1. mvn clean package microservice-discovery-eureka-0.0.1-SNAPSHOT.jar

2.microservice-discovery-eureka-0.0.1-SNAPSHOT.jar目录下创建Dockerfile文件

FROM java:8
VOLUME /tmp
ADD microservice-discovery-eureka-0.0.1-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 9000
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

3.microservice-discovery-eureka-0.0.1-SNAPSHOT.jar目录下创建docker-compose.yml

version: '3'        # 表示该docker-compose.yml文件使用的是Version 2 file format
services:           # Version 2 file format的固定写法,为project定义服务。
  microservice-discovery-eureka:                                  # 指定服务名称
    image: itmuch/microservice-discovery-eureka:0.0.1-SNAPSHOT    # 指定服务所使用的镜像
    ports:                                                        # 暴露端口信息
      - "8761:8761"

4.在docker-compose.yml目录下执行以下命令:

docker-compose up -d # 在后台构建镜像并且启动容器

5.访问htthp://宿主机ip:8761 即可访问eureka server首页

docker-compose.yml常用命令

Docker Compose入门指南_第1张图片

Docker Compose入门指南_第2张图片

Docker Compose入门指南_第3张图片

Docker Compose入门指南_第4张图片

综合实战

编排SpingCloud微服务

Docker Compose入门指南_第5张图片

1.使用Maven构建Docker镜像,各个项目pom文件添加以下代码

  
  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
 
      
      
        com.spotify
        docker-maven-plugin
        0.4.13
        
          itmuch/${project.artifactId}:${project.version}
          java
          ["java", "-jar", "/${project.build.finalName}.jar"]
          
            
              /
              ${project.build.directory}
              ${project.build.finalName}.jar
            
          
        
      
    
  

2.编写 docker-compose.yml文件

version: '3'
services:
  microservice-discovery-eureka:
    image: itmuch/microservice-discovery-eureka:0.0.1-SNAPSHOT
    network_mode: host
    ports:
    - 8761:8761
  microservice-provider-user:
    image: itmuch/microservice-provider-user:0.0.1-SNAPSHOT
    network_mode: host
    ports:
    - 8000:8000
  microservice-consumer-movie-ribbon-hystrix:
    image: itmuch/microservice-consumer-movie-ribbon-hystrix:0.0.1-SNAPSHOT
    network_mode: host
    ports:
    - 8010:8010
  microservice-hystrix-dashboard:
    image: itmuch/microservice-hystrix-dashboard:0.0.1-SNAPSHOT
    network_mode: host
    ports:
    - 8030:8030
  microservice-hystrix-turbine:
    image: itmuch/microservice-hystrix-turbine:0.0.1-SNAPSHOT
    network_mode: host
    ports:
    - 8031:8031
  microservice-gateway-zuul:
    image: itmuch/microservice-gateway-zuul:0.0.1-SNAPSHOT
    network_mode: host
    ports:
    - 8040:8040

## 注1:本Compose文件使用的网络模式是host,使用这种模式,可以让所有的微服务都无需修改eureka.client.serviceUrl.defautlZone的配置,而直接使用http://localhost:8761/eureka/。
## 注2:该方式一般不适合生产,实体书里也并未提及,仅作拓展阅读。

3.简化docker-compose.yml文件 

compose中所有服务共享一个隔离网络, 可使用服务名作为主机名发现其他服务

version: '3'
services:
  discovery:
    image: itmuch/microservice-discovery-eureka:0.0.1-SNAPSHOT
    ports:
      - "8761:8761"
  microservice-provider-user:
    image: itmuch/microservice-provider-user:0.0.1-SNAPSHOT
  microservice-consumer-movie-ribbon-hystrix:
    image: itmuch/microservice-consumer-movie-ribbon-hystrix:0.0.1-SNAPSHOT
  microservice-gateway-zuul:
    image: itmuch/microservice-gateway-zuul:0.0.1-SNAPSHOT
  microservice-hystrix-dashboard:
    image: itmuch/microservice-hystrix-dashboard:0.0.1-SNAPSHOT
  microservice-hystrix-turbine:
    image: itmuch/microservice-hystrix-turbine:0.0.1-SNAPSHOT

# 最简单的配置方式,等价于docker-compose.yml。

4.编排高可用的eureka server 

version: "3"            # 表示使用docker-compose.yml的Version 3 file format编写
services:
  microservice-discovery-eureka-ha1:
    hostname: peer1             # 指定hostname
    image: itmuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT
    links:
      - microservice-discovery-eureka-ha2
    ports:
      - "8761:8761"
    environment:
      - spring.profiles.active=peer1
  microservice-discovery-eureka-ha2:
    hostname: peer2
    image: itmuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT
    links:
      - microservice-discovery-eureka-ha1
    ports:
      - "8762:8762"
    environment:
      - spring.profiles.active=peer2
      
## 该文件是最自然的写法,但无法正常使用。运行docker-compose up时,会报循环依赖的异常。

docker-compose up 

version: "3"
services:
  peer1:      # 默认情况下,其他服务可使用服务名称连接到该服务。对于peer2节点,它需连接http://peer1:8761/eureka/,因此,我们可配置该服务的名称为peer1。
    image: itmuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT
    ports:
      - "8761:8761"
    environment:
      - spring.profiles.active=peer1
  peer2:
    image: itmuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT
    hostname: peer2
    ports:
      - "8762:8762"
    environment:
      - spring.profiles.active=peer2

## 使用Compose编排高可用的Eureka Server。

5.spring cloud 微服务动态伸缩

docker-compose up 
docker-compose scala microservice-simple-provider-user=3 microservice-gateway-zuul=3

让microservice-simple-provider-user,microservice-gateway-zuul都启动3个实例

你可能感兴趣的:(云计算)