How to deploy the springboot project in the IDE to the docker on the server or virtual machine

1. Installation process:

1. Buy a server or install a virtual machine

  1. Install docker (can be installed using yum)

tips:安装好docker之后需要配置一下,输入下面这行代码,进入docker配置文件,在ExecStart=后添加配置,远程访问docker的端口为2375
sos:如果是服务器,这个修改后需要在服务器上面再配置一下这个端口号,不然ide会识别不出端口号,很重要(这个坑踩了很久,快陷进去了)
1.vim /lib/systemd/system/docker.service  #输入,进到这个配置文件
2.-H tcp://0.0.0.0:2375                   #复制粘贴这个
3.退出编辑界面:先按esc,然后":wq"退出
4.# 重启docker
systemctl daemon-reload
systemctl restart docker

3. At the third step, it means that we have configured the docker, and now we need to pull the MySQL image in the docker

1.docker pull mysql:8.0(本人用8.0,需要什么版本号在后面改就好)
2.docker run -itd --name mysql -p 3306:3306-e MYSQL_ROOT_PASSWORD=root mysql:8.0
tips: 这一步也有个坑,如果你在安装docker之前,本地就安装了mysql,并且端口号也是3306,那必须让它处于关闭状态,否则会造成端口号冲突,windows的navigate或者小海豚就会连接不上虚拟机的mysql

4. At this point, the work of the virtual machine is finished. Go back to the ide, first modify the connection address of mysql in the yml file, the ip and port number need to be changed to the virtual machine, and the password should also be changed.

Secondly, you need to add the docker-maven-plugin plugin to the pom file

<!-- docker-maven-plugin-->
    <plugin>
        <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>1.2.2</version>
            <configuration>
                <!-- Docker路径 -->
                <dockerHost>http://虚拟机的ip:2375(刚开始设置的docker端口号)</dockerHost>
                <!-- Dockerfile定义 -->
                <baseImage>openjdk:11</baseImage>
<!-- 作者 -->
                <maintainer>jackie</maintainer>
                <resources>
                    <resource>
                        <!-- 复制jar包到docker容器指定目录 -->
                        <targetPath>/</targetPath>
    <!-- 从哪个包拷贝文件,target包 -->
                        <directory>${project.build.directory}</directory>
    <!-- 拷贝哪个文件 -->
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
                <workdir>/</workdir>
                <entryPoint>
                   ["java", "-jar", "${project.build.finalName}.jar"]
                </entryPoint>
                <forceTags>true</forceTags>
    <!-- 镜像名 -->
                <imageName>${project.artifactId}</imageName>
    <!-- 镜像版本 -->
                <imageTags>
                    <imageTag>${project.version}</imageTag>
                </imageTags>
            </configuration>
     </plugin>

5. Packing, order.

6. At this point, the docker plug-in has been loaded. Open the plug-in and click build. If BUID SUCESS is displayed at the end of the run, it means that the package has been successfully transferred to the virtual machine. Check it in the virtual machine (if it fails here, the probability is The port number 2375 is not released on the server)

7. Go back to the virtual machine and enter the command

docker images

if you see

It is successful, now to run this project input

docker run -d -p 80:80 项目名:0.0.1-SNAPSHOT

-d is running in the background, -p: binding port number, the first 80 is the port exposed to the outside world (customized) when starting the mirror, and the second 80 is the port of the downloaded mirror (original project port).

Next, check whether your project is successful, enter the command, and your project name will appear, and then you can view the project in a local browser.

docker ps

Guess you like

Origin blog.csdn.net/zhangjiaming_zjm/article/details/128796747