idea one-click encryption to deploy springboot to docker container

1. Environmental preparation

  1. One centos server to install docker (for the installation process, refer to linux to install docker in ssl mode )
  2. idea
  3. docker link certificate

2. idea configure docker link

  1. Open the plugin in settings
  2. Select docker , then fill in our docker configuration information, and select the certificate directory
  3. The connection is successful
    insert image description here
  4. Check out our containers here
    insert image description here

3. Configure the mysql container

Because our springboot project needs to use mysql, we need to install mysql and deploy it on docker together.

Execute the following command on the linux server with docker installed

  1. Pull the latest version of mysql mirror
docker pull mysql

insert image description here

  1. Check whether the mysql image is pulled successfully
docker images

insert image description here

  1. start the mysql container
mkdir -p /home/project/mysql/{
    
    data,conf}
docker run -di -p 3306:3306 --restart=always --privileged=true -v /home/project/mysql/conf:/etc/mysql/conf.d -v /home/project/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=yinfeng --name=mysql  mysql

-e MYSQL_ROOT_PASSWORD Set the login password of the mysql root account yinfeng.
-di d means run in the background as a daemon process, i means run the container

3.1 Check whether the container is started successfully

docker ps

insert image description here

3.2 Alibaba Cloud security group opens port 3306

insert image description here

3.3 Use the navicat link to test, the connection is successful

insert image description here
insert image description here

4. springboot pom file configuration

  1. Create a simple springboot project
    this is my whole project structure
    insert image description here
  2. Configure docker-maven-plugin, this file is very important and must be configured carefully
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.2</version>
    <!--将插件绑定在某个phase执行-->
    <executions>
        <execution>
            <id>build-image</id>
            <!--用户只需执行mvn package ,就会自动执行mvn docker:build-->
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>

    <configuration>
        <!--指定生成的镜像名,这里是我们的作者名+项目名-->
        <imageName>yinfeng/${project.artifactId}</imageName>

        <!--指定标签 这里指定的是镜像的版本,我们默认版本是latest-->
        <imageTags>
            <imageTag>latest</imageTag>
        </imageTags>
        <rm>true</rm>

        <!--指定基础镜像jdk1.8-->
        <baseImage>java:8</baseImage>

        <!--切换到logs目录-->
        <workdir>/logs</workdir>

        <!--查看我们的java版本-->
        <cmd>["java", "-version"]</cmd>

        <!--${project.build.finalName}.jar是打包后生成的jar包的名字 加入UTF-8编码避免中文乱码-->
        <entryPoint>["java", "-Dfile.encoding=UTF-8", "-jar", "/${project.build.finalName}.jar"]</entryPoint>

        <!--指定远程 docker api地址-->
        <dockerHost>https://服务器ip:2375</dockerHost>

        <!--指定ca证书文件路径地址 -->
        <dockerCertPath>${project.basedir}/src/main/resources/ca</dockerCertPath>

        <!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <!--jar 包所在的路径  此处配置的 即对应 target 目录-->
                <directory>${project.build.directory}</directory>
                <!--用于指定需要复制的文件 需要包含的 jar包 ,这里对应的是 Dockerfile中添加的文件名 -->
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>

    </configuration>
</plugin>

insert image description here
3. Test and package the springboot image
and follow the steps in the figure. The
insert image description here
packaged image is successful log, the first packaging speed is slow, wait patiently for a
insert image description here
while. View the image we just packaged in the docker service
insert image description here
4. Deploy the packaged image into the container

4.1. Select our image to create a container

insert image description here

4.2 Configure the container parameters, because I use the https protocol, so the 443 port mapping is opened, you can map the corresponding port to your own container according to your needs

--net=host -p 443:443  -v /logs:/logs

-p is port mapping
-v is data volume mapping, /logs is our log directory, which is convenient for viewing logs on the cloud host
-net=host is the container directly using the server's local network without being isolated by the docker network, which is convenient for our project Connect directly to the mysql database in the container

insert image description here

4.3 Start the container

insert image description here

4.4 Startup success log

insert image description here

4.5 Test it, you can directly access it successfully, and the background log is also available

insert image description here
insert image description here

5. Configure linux timed tasks

Why configure scheduled tasks?
Because every time we package, we will invalidate the last image, but docker will not delete it automatically, which takes up a lot of space. Therefore, we need to configure a scheduled task to delete these garbage images.

insert image description here
First create a sh script to clear the mirror on our linux server

vi /logs/deleteImg.sh

Then paste the following code and save and exit

#清空none镜像
docker rmi `docker images -q -f dangling=true`

insert image description here
Then go to configure the scheduled task

crontab -e

Then press the a key to enter the input mode, paste the following code, and finally save and exit. The script means to execute the deleteImg.sh script to clear the mirror every day at 2:00 a.m.

00 02 * * * /bin/bash /logs/deleteImg.sh

insert image description here

6. Summary

6.1 Now the deployment method of Internet companies is generally carried out by docker and k8s, so it is necessary to learn docker-related deployment knowledge;
6.2 This blog can only be regarded as an entry-level docker, but it also perfectly cooperates with idea and docker, but docker-maven The official documentation of -plugin is not very clear, so I can only configure it to this point, I hope the old irons will support it for three consecutive waves.

Guess you like

Origin blog.csdn.net/a1774381324/article/details/121961051