How to deploy a project with Docker Compose?

Posted by tlchung on Thu, 17 Feb 2022 02:10:25 +0100

  • 💻 Sword finger offer & leetcode question brushing warehouse:
  • 📒 Programming knowledge base:
    • Document address:
  • preface
  • Docker Compose
    • 1. What is docker compose?
    • 2. Specific steps of docker compose
    • 3. How to use Docker Compose in an IDEA project


Previously, we deployed springboot, redis and mysql projects with docker, but they are deployed in three different containers. We also need to know the ip addresses of redis and mysql first and manually configure them into the springboot application container. I just want to test them locally quickly. This cost is too high. Is there any way to centrally manage them? For example, build it into a mirror image.

There is always a way. That is Docker Compose.

Previous project address:

Docker Compose

1. What is docker compose?

Docker Compose is actually a docker tool used to define and run complex applications. What is a complex application? For example, the previously written springboot+redis+mysql has three containers. This kind of multiple containers is managed by one tool. Isn't it fragrant?

Docker compose manages multiple docker containers through a configuration file. In the configuration file, all containers are defined through services, and then docker compose script is used to start, stop and restart the application, as well as the services and dependent containers in the application.

2. Specific steps of docker compose

There are generally three steps:

  • Use Dockerfile to define the environment of the application
  • At docker compose YML defines the services that make up the application so that they can run together in an isolated environment.
  • Execute the docker compose up command to start and run the entire application.

I use Mac OS. Docker Compose is also installed when docker is installed. There is no need to install it separately.

3. How to use Docker Compose in an IDEA project

First, POM In the XML file, you should pay attention to the configuration of lowercase artifactId:


In addition, you need to configure plug-ins:

                <!-- Bind a plug-in to a phase implement -->
                        <!-- Users only need to execute mvn package ,It will be executed automatically mvn docker:build -->

In addition, dockerfile is necessary. The above plug-in has been configured. We need to put dockerfile in < dockerdirectory > Src / main / docker < / dockerdirectory >, and the configuration in dockerfile is as follows:

FROM openjdk:8-jdk-alpine
# Rewrite and name it app jar
ADD dockerdemo-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Theoretically, by this time, we will generate the corresponding jar package by using mvn clean package:


The most important thing for docker compose is to configure docker compose YML, we can put this file in the root directory of the project, and POM XML level:

version: "3"

    image: redis:latest
    restart: always
      - "6389:6379"
      - /tmp/redis.conf:/etc/redis/redis.conf
    command: redis-server /etc/redis/redis.conf

    image: mysql:latest
    restart: always
      MYSQL_ROOT_PASSWORD: "123456"
      MYSQL_USER: 'root'
      MYSQL_PASS: '123456'
      - "3306:3306"
      - "./db:/var/lib/mysql"
      - "./conf/my.cnf:/etc/my.cnf"
      - "./init:/docker-entrypoint-initdb.d/"
  # Specify service name
    # Specifies the image used by the service
    image: aphysia/dockerdemo
    # Specify container name
    container_name: dockerdemo
    # Specifies the port on which the service runs
      - 8081:8081
    # Specify the files that need to be mounted in the container
      - /etc/localtime:/etc/localtime
      - /tmp/dockerdemo/logs:/var/logs

Notable points:

service is the image we configured, including redis, MySQL and webapp. Webapp is actually our application.

In "6389:6379", 6389 is actually the port of our host, that is, my Mac needs to use 6389 to connect to the redis container, while the connection between containers needs to use 6379, which is the port of the container.

/tmp/redis.conf:/etc/redis/redis.conf / TMP / redis Conf is the directory of the host, and this directory needs to be configured in docker, otherwise an error will be reported (remember to add administrator permission when executing):

docker ERROR: * start service *: Mounts denied


mysql 8.0 may report an error Java sql. Sqllontransientconnectionexception: public key retrieval is not allowed because the url link is missing a parameter: allowPublicKeyRetrieval=true

Start possible pit points

After startup, mysql or redis may not be linked, but the container operation is normal:

DockerCompose % docker container ps
CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS         PORTS                                                  NAMES
32fd6ce598ba   aphysia/dockerdemo   "java -jar /app.jar"     7 minutes ago    Up 7 minutes>8081/tcp, :::8081->8081/tcp              dockerdemo
585b9b6bd71d   redis:latest         "docker-entrypoint.s..."   10 minutes ago   Up 7 minutes>6379/tcp, :::6379->6379/tcp              dockercompose_redis_1
d96ba57941d9   mysql:latest         "docker-entrypoint.s..."   16 minutes ago   Up 7 minutes>3306/tcp, :::3306->3306/tcp, 33060/tcp   dockercompose_mysql_1

No error is reported when docker compose up is executed. An error is reported when requesting:$AnnotatedConnectException: Connection refused: / 6379

This is because requests between containers cannot use, but must use MySQL and redis to represent the network of containers, such as JDBC: mysql://mysql:3306/test?characterEncoding=utf -8&useSSL=false&allowPublicKeyRetrieval=true

Complete application yml:

  port: 8081
  #Database connection configuration
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://mysql:3306/test?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: 123456
    host: redis     ## Server IP of redis
    port: 6379
    ##I don't have a password here, so I don't fill it in
    ## Set the maximum number of connections. 0 is infinite
      max-active: 8
      min-idle: 0
      max-idle: 8
      max-wait: -1
#Related configurations of mybatis
  #mapper profile
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.aphysia.springdocker.model
  #Hump naming
    map-underscore-to-camel-case: true
    root: debug

Another problem is docker compose The image name configured in YML must be correct, or it will appear when docker compose up is executed:

Pulling xxxx...
ERROR: The image for the service you're trying to recreate has been removed. If you continue, volume data could be lost. Consider backing up your data before continuing.

Continue with the new image? [yN]y
Pulling xxxx...
ERROR: pull access denied for postgresql, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

I thought it was the reason for logging in. It was originally a local image. You should directly create instead of pull. If you don't know the name, you can check it through the following command. REPOSITORY is the name:

DockerCompose % docker images
REPOSITORY           TAG            IMAGE ID       CREATED             SIZE
aphysia/dockerdemo   latest         1429aa26790a   54 minutes ago      137MB
<none>               <none>         ceb493583d7c   57 minutes ago      137MB
<none>               <none>         dffcc47602a2   About an hour ago   137MB
<none>               <none>         a695cf2cd2df   About an hour ago   137MB
<none>               <none>         209ce4f96d34   2 hours ago         137MB
redis                latest         40c68ed3a4d2   10 days ago         113MB
mysql                latest         e1d7dc9731da   14 months ago       544MB
openjdk              8-jdk-alpine   a3562aa0b991   2 years ago         105MB

Last start command:

sudo docker-compose up

Successfully started:


Remember to initialize the database data table after startup!!!

drop database IF EXISTS test;
use test;
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT "",
  `age` int(11) DEFAULT 0,
  PRIMARY KEY (`id`)

INSERT INTO `user` VALUES (1, 'Li Si', 11);
INSERT INTO `user` VALUES (2, 'Wang Wu', 11);


So far, it has been completed. There are still many pits in the seemingly simple command.