Docker Compose rapid deployment of multi Container Services

1 what is Docker Compose

When we define the container, we use the command of Docker and build Docker, and then use the command of Docker. However, the application system of microservice architecture generally includes several microservices, and each microservice will generally deploy multiple instances. If each microservice needs to be started and stopped manually, it can be imagined that the efficiency is low and the amount of maintenance is large.

Docker Compose can easily and efficiently manage containers. It is an application tool for defining and running multi container dockers

2. Install Docker Compose

Installation command:

[root@iZ2ze4m2ri7irkf6h6n8zoZ ~]# curl -L$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
[root@iZ2ze4m2ri7irkf6h6n8zoZ ~]# chmod +x /usr/local/bin/docker-compose

Check for successful installation:

[root@iZ2ze4m2ri7irkf6h6n8zoZ ~]# docker-compose -v

3. Brief introduction to docker compose file format

Docker Compose files are generally named Docker Compose YML, and the Docker Compose command is executed in the directory where the file is located.

  • Docker Compose is divided into three layers: project, service / reference tag and container

For example:

docker-compose.yml   # A file represents a project
 serveices:          # service
   container-name:   # container
      - xxx:xxx
 network:            # Reference label

The following is a standard docker compose YML file

version: "3"  # Specified version
services:     # services
  proxy:      # Custom container name
    build: ./proxy  # The directory of Dockerfile, which is used to build containers
    networks: # Custom container network
      - frontend
    build: ./app
      - frontend
      - backend
    image: postgres
      - backend
    driver: custom-driver-1
    driver: custom-driver-2
      foo: "1"
      bar: "2"

4. Common commands of docker compose

  • ps: list all running containers
docker-compose ps
  • logs: view the service log output
docker-compose logs
  • Port: print the bound public port. The following command can output the public port bound by eureka service port 8761
docker-compose port eureka 8761
  • Build: build or rebuild services
docker-compose build
  • start: starts the container where the specified service already exists
docker-compose start eureka
  • stop: the container that stops the running service
docker-compose stop eureka
  • rm: deletes the container for the specified service
docker-compose rm eureka
  • up: build and start containers
docker-compose up
  • kill: a container that stops a specified service by sending a SIGKILL signal
docker-compose kill eureka
  • pull: download service image
docker-compose pull eureka
  • scale: sets the number of service containers specified in the form of service=num
docker-compose scale user=3 movie=3
  • run: execute a command on a service
docker-compose run web bash

5. Using Docker Compose to deploy Spring Boot+Redis in one click

Application construction

5.1.1 Spring Boot project




    host: ymx.redis
    port: 6379
        max-active: 8
        max-wait: -1
        max-idle: 500
        min-idle: 0
      shutdown-timeout: 0

controller code:

public class HelloController {

    private RedisTemplate<String, String> redisTemplate;

    public String hello(@PathVariable("id") Integer id) {
        return redisTemplate.opsForValue().get(String.valueOf(id));

    public String save(@PathVariable("id") Integer id, @PathVariable("name") String name) {
        try {
            redisTemplate.opsForValue().set(String.valueOf(id), "Hello " + name + "!");
        } catch (Exception e) {
            return "false";
        return "success";
5.1.2 Redis configuration file

Just redis that comes with redis Conf made some changes

#Comment out bind
# bind -::1
#Modify protected mode yes - > no
protected-mode no

5.2 package applications and build directories

5.2.1 packaging Spring Boot project

5.2.2 upload redis Conf configuration file
5.2.3 directory structure
- mycompose
  - docker-compose.yml 
  - rd  
    - Dockerfile  
    - redis.conf
  - sp
    - Dockerfile  
    - sp_redis-0.0.1-SNAPSHOT.jar

5.3 writing Dockerfile

5.3.1 Dockerfile of spring boot container
FROM java:8


COPY sp_redis-0.0.1-SNAPSHOT.jar /root/sp_redis-0.0.1-SNAPSHOT.jar


ENTRYPOINT ["java", "-jar","/root/sp_redis-0.0.1-SNAPSHOT.jar"]
5.3.2 Dockerfile of redis container
FROM redis

COPY redis.conf /usr/local/etc/redis/redis.conf


CMD ["redis-server","/usr/local/etc/redis/redis.conf" ]

5.4 write docker compose yml

version: "2.8" # Indicates that the docker compose file uses Version 2 file
  sp-demo:       # Specify service name
    build: ./sp  # Specify the path of Dockerfile
    ports:       # Specify port mapping
      - "9001:8080"
      - re-demo:ymx.redis # Make container link
    build: ./rd

5.5 run and test deployment results


[root@iZ2ze4m2ri7i mycompose]# docker-compose up
Creating network "mycompose_default" with the default driver
Building re-demo
Sending build context to Docker daemon  96.77kB
Step 1/5 : FROM redis
latest: Pulling from library/redis


[root@iZ2ze4m2ri7i mycompose]# curl http://localhost:9001/save/2/Ymx
[root@iZ2ze4m2ri7i mycompose]# curl http://localhost:9001/hello/2
Hello Ymx!

6 Summary

In the Spring Boot configuration file, the host of redis does not use localhost or, but uses the domain name YMX Redis, the domain name in docker compose The YML file is mapped, and then the container of Spring Boot can be linked to the redis container, but this situation depends on a default condition, that is, the docker network defaults to the bridge mode, and the two containers are in the same subnet, so they can access each other.

Therefore, links is not the only container network solution. When there are many containers, networks needs to be used for network management.

Reference article:

Keywords: Operation & Maintenance Docker Container cloud computing IDE

Added by codeman on Sun, 13 Feb 2022 15:55:07 +0200