在Java的技术栈中可以使用maven插件来构建docker镜像,用起来没有docker-compose好用。
实现目标
- spring boot 、mysql 、redis 定义正常运行。
- 解决docker compose 启动顺序问题。
- 解决配置文件读取其它容器ip问题。
演示效果
docker运行情况
spring boot运行情况
1 2
| ➜ docker-run curl http://127.0.0.1:9000/docker/ {"code":"200","data":"[information_schema, docker_app_1, mysql, performance_schema, sys]","cache":{"redis_version": "5.0.5"}}
|
构建项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| . ├── Dockerfile ├── docker-compose.yml ├── docker-config │ ├── mysql │ │ ├── init │ │ │ └── 1_init.sql │ │ └── my.cnf │ ├── pwd.txt │ └── redis │ └── redis.conf ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ └── cn │ │ │ └── z201 │ │ │ └── docker │ │ │ ├── AppApplication.java │ │ │ └── AppApplicationController.java │ │ └── resources │ │ ├── application-dev.yml │ │ ├── application-test.yml │ │ ├── application.yml │ │ └── logback.xml │ └── test │ └── java └── target ├── Docker-Compose-SpringBoot-Mysql-Redis-1.0.0-SNAPSHOT.jar
|
spring boot 项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package cn.z201.docker;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap; import java.util.Map;
@RestController public class AppApplicationController {
@Autowired private JdbcTemplate jdbcTemplate;
@Autowired private RedisTemplate redisTemplate;
@RequestMapping(value = "") public Object index() { List<String> dataBasesList = jdbcTemplate.queryForList("SHOW DATABASES", String.class); Properties info = redisTemplate.getConnectionFactory().getConnection().info(); Map<String, Object> data = new HashMap<>(); data.put("code", "200"); data.put("db", dataBasesList.toString()); data.put("cache", info); return data; } }
package cn.z201.docker;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext;
@SpringBootApplication public class AppApplication {
public static void main(String[] args) { ApplicationContext applicationContext = SpringApplication.run(AppApplication.class, args); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| spring: application: name: spring-boot-mysql-redis profiles: active: dev mvc: throw-exception-if-no-handler-found: true web: resources: add-mappings: false server: port: 9000 servlet: context-path: /docker
logging: config: classpath:logback.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| spring: datasource: url: jdbc:mysql://mysql:3306/docker_app_1?useSSL=false&useUnicode=true&autoReconnect=true&failOverReadOnly=false&characterEncoding=utf-8 username: root password: root driver-class-name: com.mysql.jdbc.Driver type: com.zaxxer.hikari.HikariDataSource hikari: maximumPoolSize: 20 minimumIdle: 5 idleTimeout: 600000 connectionTimeout: 30000 maxLifetime: 1800000 redis: host: redis port: 6379 password: root database: 0 lettuce: pool: max-active: 8 max-wait: 2000 min-idle: 0 max-idle: 8 shutdown-timeout: 100 timeout: 60s
|
docket-compose配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| version : '3'
networks: network-docker-app: driver: bridge
services: web: container_name: cn.z201.docker-app build: context: . dockerfile: . image: cn.z201.docker-app-3 networks: - network-docker-app expose: - '9000' ports: - '9000:9000' depends_on: - mysql - redis links: - mysql - redis mysql: image: mysql:5.7 container_name: mysql5.7-dev-3 networks: - network-docker-app expose: - '3306' ports: - '3306:3306' volumes: - ./docker-config/mysql/my.cnf:/etc/mysql/my.cnf - ./docker-config/mysql/init:/docker-entrypoint-initdb.d command: [ '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci', '--lower_case_table_names=1', '--default-time-zone=+8:00'] environment: - MYSQL_ROOT_PASSWORD=root healthcheck: test: "/bin/netstat -anpt|grep 3306" interval: 30s timeout: 3s retries: 1 redis: image: redis:5.0.5 container_name: redis5.0.6-dev-3 networks: - network-docker-app expose: - '6379' ports: - '6379:6379' volumes: - ./docker-config/redis/redis.conf:/etc/redis.conf command: redis-server /etc/redis.conf healthcheck: test: "/bin/netstat -anpt|grep 6379" interval: 30s timeout: 3s retries: 1
|
END