[TOC]
preface
We used Docker to deploy springboot, Redis,mysql projects, but deployed in three different containers, we need to first know the IP address of Redis and mysql, manually configure into the Springboot application container, I just want to quickly test locally. It’s too expensive. Is there any way to centralize them? Like building it as a mirror image.
There is always a solution, Docker Compose.
Previous project address: github.com/Damaer/Demo…
Docker Compose
1. What is Docker Compose?
Docker Compose is a Docker Compose tool that is used to define and run complex applications. For example, springboot+ Redis +mysql, which contains three containers, uses a tool to manage multiple containers.
Docker compose manages multiple Docker containers through a configuration file. In the configuration file, all containers are defined by service, and then the docker-compose script is used to start, stop, and restart the application, as well as the services in the application and the dependent containers.
2. Specific steps for Docker Compose
There are generally three steps:
- use
Dockerfile
To define the application’s environment - in
docker-compose.yml
Define the services that make up the application so that they can run together in an isolated environment. - perform
docker-compose up
Command to start and run the entire application.
I use Mac OS, Docker Compose has already been installed when INSTALLING Docker, there is no need to install Docker Compose separately.
3. How to use Docker Compose in IDEA project
First, you need to configure the artifactId in lower case in the POM.xml file:
<groupId>com.aphysia</groupId>
<artifactId>dockerdemo</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<name>dockerdemo</name>
<packaging>jar</packaging>
Copy the code
In addition, you need to configure the plug-in:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<! -- Bind the plug-in to a phase execution -->
<executions>
<execution>
<id>build-image</id>
<! Docker :build -->
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
Copy the code
SRC /main/docker
FROM OpenJDK :8-jdk-alpine EXPOSE 8081 VOLUME/TMP # ADD dockerdemo-0.0.1 -snapshot.jar app.jar ENTRYPOINT ["java","-jar","/app.jar"]Copy the code
In theory at this point, using the MVN Clean Package should generate the corresponding JAR package:
Docker-comemage. yml: docker-comemage. yml: docker-comemage. yml: docker-comemage. yml:
version: "3"
services:
redis:
image: redis:latest
restart: always
ports:
- "6389:6379"
volumes:
- /tmp/redis.conf:/etc/redis/redis.conf
command: redis-server /etc/redis/redis.conf
mysql:
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: "123456"
MYSQL_USER: 'root'
MYSQL_PASS: '123456'
ports:
- "3306:3306"
volumes:
- "./db:/var/lib/mysql"
- "./conf/my.cnf:/etc/my.cnf"
- "./init:/docker-entrypoint-initdb.d/"Image: aphysia/dockerdemo # Specifies the name of the container container_name: dockerdemo # Specifies the port on which the service is running. Ports: -8081:8081# need to mount the files in the specified container volumes: - / etc/localtime: / etc/localtime - / TMP/dockerdemo/logs: /var/logs
Copy the code
Points to note:
-
Service is the mirror of our configuration, including Redis,mysql, webApp, webApp is actually our application.
-
In “6389:6379”, 6389 is actually the port of our host, that is, I need to use 6389 to connect my Mac to redis containers, and I need to use 6379 to connect containers, which is the port of containers.
-
/ TMP/redis. Conf: / etc/redis/redis in the conf/TMP/redis. The conf directory is the host, and need inside the docker configuration to this directory, or an error (execution remember add administrator privileges) :
docker ERROR: * start service *: Mounts denied Copy the code
-
Mysql 8.0 May be an error in Java. SQL. SQLNonTransientConnectionException: AllowPublicKeyRetrieval =true PublicKeyRetrieval is not allowed
Start possible potholes
Mysql or Redis may not be connected after startup, but it is normal to see the container running:
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 0.0.0.0:8081->8081/ TCP, :::8081->8081/ TCP DockerDemo 585b9b6bd71D Redis :latest "docker-entrypoint.s..." 10 minutes ago Up 7 minutes 0.0.0.0:6379->6379/ TCP, :::6379->6379/ TCP Dockercompose_redis_1 d96ba57941d9 mysql:latest "docker-entrypoint.s..." 16 minutes ago Up 7 minutes 0.0.0.0:3306->3306/ TCP, ::3306->3306/ TCP, 33060/ TCP dockercompose_mysql_1Copy the code
Docker-compose up: docker-compose up: docker-compose up: docker-compose up: docker-compose up: docker-compose up: docker-compose up
Io.net ty. Channel. AbstractChannel $AnnotatedConnectException: Connection refused: / 127.0.0.1:6379Copy the code
This is because the container between the request cannot use 127.0.0.1, must use the mysql, redis represents the container’s network, such as: JDBC: mysql: / / mysql: 3306 / test? characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
The complete application. Yml:
server:
port: 8081
spring:
Database connection configuration
datasource:
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
redis:
host: redis The IP address of the server where redis resides
port: 6379
## Password, I do not set here, so do not fill in
password:
## Set the maximum number of connections. 0 is unlimited
pool:
max-active: 8
min-idle: 0
max-idle: 8
max-wait: - 1
# Mybatis configuration
mybatis:
Mapper configuration file
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.aphysia.springdocker.model
# Enable hump naming
configuration:
map-underscore-to-camel-case: true
logging:
level:
root: debug
Copy the code
Docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose:
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
Copy the code
A REPOSITORY is a local image that should be created instead of pulled. If you do not know the name, you can use the following command to query 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 105MBCopy the code
Final startup command:
sudo docker-compose up
Copy the code
Successful startup:
Remember to initialize the database table after startup!!
drop database IF EXISTS test;
CREATE DATABASE test;
use test;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT "",
`age` int(11) DEFAULT 0.PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES (1.'bill'.11);
INSERT INTO `user` VALUES (2.'Cathy'.11);
Copy the code
So far, done, seemingly simple command, in fact, there are many pits.
[Author profile] : Qin Huai, public number [Qin Huai Grocery store] author, the road of technology is not at that time, mountain high water long, even slow, chi and not stop. Personal Writing Direction: Java source code analysis, JDBC, Mybatis, Spring, Redis, distributed, sword Offer, LeetCode, etc., carefully write each article, do not like the title party, do not like the flowery, mostly write a series of articles, can not guarantee that I write are completely correct, But I guarantee that what I write is done through practice or research. We hope to correct any omissions or mistakes.
Offer all questions in PDF
What did I write about 2020?
Open Source Programming Notes