Docker Compose实例

目录

一、前提说明

二、简单的Docker容器部署案例

1. Dockerfile 配置

2. docker-compose.yml 配置

3. application-prod.properties 配置

4. pom.xml 配置

5. 上传文件

6. 创建基础Docker镜像

7. docker-compose.yml编排

8. 停止并删除容器编排

三、案例地址


一、前提说明

在配置好Docker和Docker Compose之后,部署一个JavaWeb项目。

使用到 spingboot+mysql+redis 

二、简单的Docker容器部署案例

1. Dockerfile 配置

# 基于那个镜像使用 java
# FROM openjdk:8-oracle
FROM java:8

# 作者
MAINTAINER cj

# 在主机 /var/lib/docker 目录下创建一个临时文件,并链接到容器的 /tmp
VOLUME /tmp

# 将jar包添加到容器中,并命名为 deploy-docker.jar
ADD deploy-docker-1.0-SNAPSHOT.jar /deploy-docker.jar

# 运行jar包
RUN bash -c 'touch /deploy-docker.jar'
# 使用 ENTRYPOINT 设置容器启动时执行的命令,并指定 Spring profile 为 prod
ENTRYPOINT ["java", "-jar", "/deploy-docker.jar", "--spring.profiles.active=prod"]

# SpringBoot 项目配置的端口号为 1931,需要将 1931 暴露出去
EXPOSE 1931

2. docker-compose.yml 配置

# docker-compose文件版本号
version: "3"

# 定义自定义网络 springboot_network_1
networks:
  springboot_network_1:

# 配置各个容器服务
services:
  # 定义名为 microService 的服务
  microService:
    # 使用 deploy-docker:1.1 镜像作为容器
    image: deploy-docker:1.1
    # 设置容器的名称为 ms01
    container_name: ms01  # 容器名称,如果不指定,会生成一个服务名加上前缀的容器名
    # 将容器内部端口 1931 映射到宿主机端口 1931
    ports:
      - "1931:1931"
    # 挂载宿主机的 /app/microService 目录到容器内的 /data 目录
    volumes:
      - /app/microService:/data
    # 连接到名为 springboot_network_1 的自定义网络
    networks:
      - springboot_network_1
    # 定义 microService 服务依赖的其他服务
    depends_on:
      - redis
      - mysql

  # 定义名为 redis 的服务
  redis:
    # 使用 redis:6.0.8 镜像作为容器
    image: redis:6.0.8
    # 主机端口:容器内部端口
    ports:
      - "6379:6379"
    # 挂载宿主机的 redis 配置文件和数据目录到容器内
    volumes:
      - /app/redis/redis.conf:/etc/redis/redis.conf
      - /app/redis/data:/data
    # 连接到名为 springboot_network_1 的自定义网络
    networks:
      - springboot_network_1
    # 指定容器启动命令为 redis-server /etc/redis/redis.conf
    command: redis-server /etc/redis/redis.conf

  # 定义名为 mysql 的服务
  mysql:
    # 使用 mysql:8.3 镜像作为容器
    image: mysql:8.3
    # 设置 MySQL 的环境变量
    environment:
      MYSQL_ROOT_PASSWORD: '123456'
      MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
      MYSQL_DATABASE: 'deploy-docker'
      MYSQL_USER: 'cj'
      MYSQL_PASSWORD: '123456'
    # 主机端口(3306):容器内部端口(3306)
    ports:
      - "3306:3306"
    # 挂载宿主机的 MySQL 数据库文件、配置文件和初始化脚本到容器内
    volumes:
      - /app/mysql/db:/var/lib/mysql
      - /app/mysql/conf/my.cnf:/etc/my.cnf
      - /app/mysql/init:/docker-entrypoint-initdb.d
    # 连接到名为 springboot_network_1 的自定义网络
    networks:
      - springboot_network_1
    # 指定容器启动命令为 --default-authentication-plugin=mysql_native_password
    command: --default-authentication-plugin=mysql_native_password # 解决外部无法访问

3. application-prod.properties 配置

# 服务器端口号
server.port=1931
server.servlet.context-path=/
# ======================== Alibaba Druid 数据源配置 ======================
# 配置使用 Alibaba Druid 数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 数据库驱动类名
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库连接 URL
#spring.datasource.url=jdbc:mysql://192.168.153.133:3306/deploy_docker?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
# 使用容器内部服务名进行连接
spring.datasource.url=jdbc:mysql://mysql:3306/deploy_docker?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
# 数据库用户名
spring.datasource.username=root
# 数据库密码
spring.datasource.password=123456
# 配置 Druid 连接池,禁用空闲时检测
spring.datasource.druid.test-while-idle=false
# ======================== Redis 缓存配置 ======================
# Redis 数据库索引
spring.redis.database=0
# 使用 IP 地址进行连接
#spring.redis.host=192.168.153.133
# 使用容器内部服务名进行连接
spring.redis.host=redis
# Redis 端口号
spring.redis.port=6379
# Redis 密码
spring.redis.password=123456
# 配置 Lettuce 连接池参数
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
# ======================== MyBatis 配置 ====================
# MyBatis Mapper 文件路径
mybatis.mapper-locations=classpath:mapper/*.xml
# MyBatis 实体类包路径
mybatis.type-aliases-package=org.example.entities
# ======================== Swagger 配置 ======================
# 启用 Swagger2 API 文档
spring.swagger2.enabled=true

4. pom.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- 从仓库中查找父项目 -->
    </parent>

    <groupId>org.study</groupId>
    <artifactId>deploy-docker</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <!-- 定义项目使用的属性,如编码、编译版本等 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <!-- 定义各个依赖版本 -->
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
        <mysql.version>8.0.30</mysql.version>
        <druid.version>1.1.17</druid.version>
        <mapper.version>4.1.5</mapper.version>
        <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
        <swagger.version>2.9.2</swagger.version>
    </properties>

    <!--配置不同的profile,对应不同的生产环境-->
    <profiles>
        <profile>
            <!--开发-->
            <id>dev</id>
            <activation>
                <!--默认开发环境-->
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <activatedProperties>dev</activatedProperties>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <activatedProperties>prod</activatedProperties>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <activatedProperties>test</activatedProperties>
            </properties>
        </profile>
    </profiles>

    <!-- 项目依赖配置 -->
    <dependencies>
        <!-- Guava:Google开源的Guava中自带的布隆过滤器 -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>23.0</version>
        </dependency>
        <!-- Redisson:Redis Java客户端库 -->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.13.4</version>
        </dependency>
        <!-- Spring Boot Starter Web:Spring Boot的Web起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Spring Boot Starter Actuator:Spring Boot的监控与管理依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- Swagger2:API文档生成工具 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <!-- Spring Boot Starter Data Redis:Spring Boot集成Redis依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- Spring Boot Starter Cache:Spring Boot缓存起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <!-- Apache Commons Pool2:通用对象池 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <!-- Jedis:Redis的Java客户端库 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- MySQL Connector Java:MySQL数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!-- Druid Spring Boot Starter:Spring Boot集成Druid连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <!-- MyBatis Spring Boot Starter:Spring Boot集成MyBatis依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.spring.boot.version}</version>
        </dependency>
        <!-- Apache Commons Codec:通用编解码库 -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
        <!-- Hutool:Java工具类库 -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.2.3</version>
        </dependency>
        <!-- JUnit:Java单元测试框架 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <!-- Spring Boot DevTools:Spring Boot开发工具,用于热部署等 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!-- Spring Boot Starter Test:Spring Boot测试起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Log4j:Java日志框架 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <!-- Lombok:Java简化代码库 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <optional>true</optional>
        </dependency>
        <!-- Javax Persistence API:Java持久化API -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>
        <!-- 通用Mapper:MyBatis通用Mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>${mapper.version}</version>
        </dependency>
    </dependencies>


    <!-- 项目构建配置 -->
    <build>
        <!-- 配置资源处理 -->
        <resources>
            <!-- 第一个资源配置 -->
            <resource>
                <!-- 指定资源目录 -->
                <directory>src/main/resources</directory>
                <!-- 是否进行资源过滤 -->
                <filtering>true</filtering>
            </resource>
            <!-- 第二个资源配置 -->
            <resource>
                <!-- 指定资源目录 -->
                <directory>src/main/resources</directory>
                <!-- 是否进行资源过滤 -->
                <filtering>false</filtering>
                <!-- 包含的文件列表 -->
                <includes>
                    <!-- 包含所有的 .xml 文件 -->
                    <include>**/*.xml</include>
                    <include>application.properties</include>
                    <!-- 包含根据 activatedProperties 动态生成的             
                    application-${activatedProperties}.properties 文件 -->
                    <include>application-${activatedProperties}.properties</include>
                    <!-- 包含所有的 .properties 文件 -->
                    <include>**/*.properties</include>
                    <include>logback.xml</include>
                </includes>
            </resource>
        </resources>

        <!-- 配置 Maven 插件 -->
        <plugins>
            <!-- Spring Boot Maven Plugin:用于打包Spring Boot应用 -->
            <plugin>
                <!-- 插件组ID -->
                <groupId>org.springframework.boot</groupId>
                <!-- 插件ID -->
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- Maven Resources Plugin:Maven资源处理插件 -->
            <plugin>
                <!-- 插件组ID -->
                <groupId>org.apache.maven.plugins</groupId>
                <!-- 插件ID -->
                <artifactId>maven-resources-plugin</artifactId>
                <!-- 插件版本 -->
                <version>3.1.0</version>
            </plugin>
        </plugins>
    </build>


</project>

5. 上传文件

上传文件到CentOS中,创建一个mydocker的文件夹,将打包的jar、Dockerfile、docker-compose.yml放在同一级目录中。如同:

ps:以下命令需要在包含jar包、Dockerfile、docker-compose.yml目录下进行,不然可能会出现找不到文件的错误

6. 创建基础Docker镜像

docker build -t deploy-docker:1.1 .

7. docker-compose.yml编排

编写完成docker-compose.yml后,进行语法检查

# 进行语法检查
docker-compose config -q

如果语法检查没有任何问题,进行创建、启动

docker-compose up -d

8. 停止并删除容器编排

docker-compose down

三、案例地址

放在gitee上,有需要的可以获取,如果有帮助记得★starhttps://gitee.com/CJ_HGX/deploy-dockericon-default.png?t=N7T8https://gitee.com/CJ_HGX/deploy-docker

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我来为您介绍一下使用docker-compose实现容器编排的具体步骤。 首先,您需要安装DockerDocker Compose。具体安装方法可以参考官方文档。 接着,您需要创建一个docker-compose.yml文件,在该文件中定义您需要的服务和容器。例如,以下是一个简单的docker-compose.yml文件: ``` version: '3' services: web: image: nginx ports: - "8080:80" db: image: mysql environment: MYSQL_ROOT_PASSWORD: example MYSQL_DATABASE: test MYSQL_USER: test MYSQL_PASSWORD: test ``` 该文件定义了两个服务:web和db。web服务使用nginx镜像,并将本地主机的8080端口映射到容器的80端口。db服务使用mysql镜像,并设置了一些环境变量。 接下来,您可以使用docker-compose命令来启动这些服务。在终端中进入docker-compose.yml文件所在目录,输入以下命令: ``` docker-compose up ``` 该命令会启动所有定义在docker-compose.yml文件中的服务,并将它们连接在一起。 如果您需要在后台运行服务,可以使用以下命令: ``` docker-compose up -d ``` 如果您需要停止服务,可以使用以下命令: ``` docker-compose down ``` 这些命令将会停止并删除所有的容器和网络。如果您只想停止服务,而不删除容器和网络,可以使用以下命令: ``` docker-compose stop ``` 以上就是使用docker-compose实现容器编排的基本步骤。当然,您可以根据需要定义更多的服务和容器,并使用docker-compose命令来管理它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值