springboot project entry level docker packaging mirror publishing and installation

The springboot project database is mysql, the cache is redis, both MySQL and redis run in the docker environment, and th...

The springboot project database is mysql, the cache is redis, both MySQL and redis run in the docker environment, and the boot project relies on jdk8 to publish and run in separate containers, so there are many scenarios. This paper uses the simplest way to package and run in jar (note the code in the red section below).

First, the pom configuration is as follows

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.shuogesha</groupId>
<artifactId>base2020</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>base2020</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>

Secondly, if the project uses application.properties, application.yml to configure multiple environments, we use yml's configuration

application.yml

#Configure the reading environment
spring:
profiles:
active: docker

application-docker.yml modification reason, because instead of exposing the container's mysql, redis ports to connect out, it uses the alias of docker--link, so modify the alias to modify the link address host, column

#==========redis configuration information===========#
redis:
host: shuogesha-redis
port: 6379
password:
pool:
max-active: 8
max-wait: 1
max-idle: 8
min-idle: 0
timeout: 0

Configure the steps above to package the project pom file directly into jar

clean package -Dmaven.test.skip=true

A Dockerfile needs to be written to produce a mirror of the docker

# Docker image for base2020 run
# VERSION 0.0.1
# Author: shuogesha
# Basic mirror using java
FROM java:8
# author
MAINTAINER shuogesha <[email protected]>
# VOLUME specifies the temporary file directory as/data.
ARG workdir=/data
VOLUME $
WORKDIR $
# Add the jar package to the container and rename it app.jar
ADD target/base2020-0.0.1-SNAPSHOT.jar /app.jar
# Specify port 8080
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Then command under the project's root directory, cd to the project's root directory

You can specify the version number by executing the mirror generation command, but now the test just ignores it and pays attention to the following.Number is indispensable

Package the project and set a version number, then see if the comparison was successful
docker build -t shuogesha/base2020 .

After successful packaging, you can run docker images to see if they are successful

Step by step, no flaws, then start running the command, of course you can expose port 8080, or map the local directory to the container, for example, some picture uploads need to be saved all the time to avoid data loss after the container restarts;

docker run -d --name base2020 --link redis5.0.5:shuogesha-redis --link mysql5.6:shuogesha-mysql -v /Users/zhaohaiyuan/Downloads/docker/base2020:/data -p 8080:8080 shuogesha/base2020

You can then see if the container is running

docker container ls -a

If the container fails to start or succeeds, you can view the container's log through the container's id

docker logs -f -t container id

Radix Isatidis Millennium Five original articles published, 0% praised, 358 visits Private letter follow

5 February 2020, 22:05 | Views: 3590

Add new comment

For adding a comment, please log in
or create account

0 comments