Deploy the springboot project in idea to the remote docker container

Keywords: Programming Docker Java Maven Tomcat

1. Create the project in idea, as shown below

2. Configure docker maven plugin

<!-- Docker maven plugin -->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.0.0</version>
    <configuration>
        <forceTags>true</forceTags>
        <!-- docker Address of private server -->
        <dockerHost>http://192.168.132.129:2375</dockerHost>
        <!--Dependent base image-->
        <imageName>${project.groupId}/${project.build.finalName}</imageName>
        <workdir>/</workdir>
        <baseImage>java</baseImage>
        <cmd>["java", "-version"]</cmd>
        <entryPoint>["java", "-jar", "./${project.build.finalName}.jar"]</entryPoint>
        <dockerDirectory>${project.basedir}/</dockerDirectory>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
        <imageTags>
            <imageTag>${project.version}</imageTag>
            <imageTag>latest</imageTag>
        </imageTags>
    </configuration>
</plugin>

3. Start the server and start docker (server address 192.168.132.129)

4. idea connects to docker, as shown below

If the connection is unsuccessful, check whether the Docker has enabled remote access. If not, the configuration is as follows

On the server: vim /usr/lib/systemd/system/docker.service
#Modify ExecStart line
ExecStart=/usr/bin/dockerd  -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

#Reload profile
systemctl daemon-reload    
#Restart service
systemctl restart docker.service 
#Check whether the port is on
netstat -nlpt 
#Directly curl to see if it is effective
curl http://127.0.0.1:2375/info

5. Create the DockerFile file as follows

Dockerfile content

#Indicates using the Jdk8 environment as the base image. If the image is not local, it will be downloaded from the DockerHub
FROM java:8

#VOLUME points to a directory of / tmp. Because Spring Boot uses the built-in Tomcat container, Tomcat uses / tmp as the working directory by default.
#The effect of this command is to create a temporary file in the host's / var/lib/docker directory and link it to the / tmp directory in the container
VOLUME /tmp

#Copy file and rename
ADD ./target/springboot-docker-demo-1.0.0.jar /docker-demo-1.0.0.jar

RUN bash -c 'touch /docker-demo-1.0.0.jar'
EXPOSE 7001
#In order to shorten the startup time of Tomcat, add the system attribute of java.security.egd to point to / dev/urandom as ENTRYPOINT
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/docker-demo-1.0.0.jar"]

6. Edit the idea Dockerfile configuration, as shown below

7. To create a docker image, click "Build Image for Dockerfile" in the dockerfile

The deployment log prompt of the console is as follows

View image in server docker

8. To run the project, click "Run Dockerfile" in the dockerfile

The results of console operation are as follows

Check the operation of the project in the server docker, as follows

9. To access the project in the browser, enter the address in the browser address bar: http://192.168.132.129:7001/test, and the output is as follows

Posted by lou28 on Sun, 26 Jan 2020 07:00:26 -0800