Cloud Native 7 min read

Deploy SpringBoot, MySQL, and Redis with Docker Compose Custom Networks

This guide walks through creating a Docker Compose file that defines a custom network, assigns static IPs to Redis, MySQL, and a Java container, and shows how to configure SpringBoot to connect to these services using the assigned addresses.

The Dominant Programmer
The Dominant Programmer
The Dominant Programmer
Deploy SpringBoot, MySQL, and Redis with Docker Compose Custom Networks

Scenario

The article introduces Docker networking, contrasting the default docker0 bridge with custom networks, and explains why a custom network is useful when orchestrating a SpringBoot application together with MySQL and Redis via Docker Compose.

Implementation

1. Prepare the project directory as shown in the diagram (the diagram is omitted for brevity). Ensure the Redis version is 5.0.9 and download the matching redis.conf file. Edit the configuration to comment out bind 127.0.0.1 and set a password, then grant the file read/write permissions with chmod 777 redis.conf. Create an empty data directory for MySQL and another empty data directory under the Redis folder.

2. Create docker-compose.yml with the following content (pay attention to two‑space indentation and avoid tabs):

version: "3.8"

services:

  redis:
    image: redis:5.0.9-alpine3.11
    command: redis-server /etc/redis/redis.conf
    ports:
      - "379:6379"
    volumes:
      - ./redis/redis.conf:/etc/redis/redis.conf
      - ./redis/data:/data
    networks:
      badaonet:
        ipv4_address: 192.168.0.11

  mysql:
    image: mysql:8.0
    command: --lower_case_table_names=1
    environment:
      MYSQL_DATABASE: test
      MYSQL_ROOT_PASSWORD: ABC@123!
      MYSQL_ROOT_HOST: '%'
      TZ: Asia/Shanghai
    ports:
      - "306:3306"
    volumes:
      - ./mysql/data:/var/lib/mysql
    networks:
      badaonet:
        ipv4_address: 192.168.0.12

  java:
    image: openjdk:8u342-oracle
    command: [
      'java',
      '-jar',
      '/home/badao.jar'
    ]
    environment:
      TZ: Asia/Shanghai
    ports:
      - "996:996"
    volumes:
      - ./java:/home
    working_dir: /home/
    networks:
      badaonet:
        ipv4_address: 192.168.0.13
    depends_on:
      - redis
      - mysql

networks:
  badaonet:
    ipam:
      config:
        - subnet: 192.168.0.0/24

3. Start the stack and verify there are no errors: docker compose up Running without -d shows the logs directly. After the containers start, confirm they are running with docker ps. The screenshot (omitted) shows all three containers up.

Inspect the custom network to see its details: docker network inspect badao_badaonet The inspection output (image omitted) confirms that the network has been created with the specified subnet and that each container has the assigned IP address.

4. Configure SpringBoot to connect to MySQL and Redis using the static IPs defined in the Compose file. In application.yml (or application.properties) set the MySQL URL to:

url: jdbc:mysql://192.168.0.12:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8

and the Redis connection to:

redis:
  host: 192.168.0.11
  port: 6379
  password: ABC@123!

Build the Java project into badao.jar, place it in the java directory, and run docker compose up again. After the containers are up, test the connections by creating a simple controller that inserts a record into MySQL and stores a value in Redis:

@RequestMapping("student")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    RedisCache redisCache;

    @RequestMapping("save")
    public String save() {
        User user = new User();
        user.setUserId(new Random().nextInt(1000) + 1);
        user.setName("张三" + user.getUserId());
        user.setAge(new Random().nextInt(80) + 1);
        userService.insert(user);
        redisCache.setCacheObject("badao", "222", 100, TimeUnit.SECONDS);
        String aaa = redisCache.getCacheObject("badao");
        System.out.println(aaa);
        return user.getName() + "创建成功!";
    }
}

Invoke the endpoint with: curl 127.0.0.1:996/student/save The response shows that the MySQL insert succeeded (screenshot omitted) and that the Redis cache entry was stored (screenshot omitted).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerRedisMySQLSpringBootDocker Composecustom network
The Dominant Programmer
Written by

The Dominant Programmer

Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.