1: Enable remote access function of Docker

Main is configured to modify ExecStart ExecStart = / usr/bin/dockerd -h fd: / / — containerd = / run/containerd containerd. The sock – H TCP: / / 0.0.0.0:2375

[root@localhost ~]# cat /lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket containerd.service

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
# ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecStart=/usr/bin/dockerd -H fd:/ / -- containerd = / run/containerd containerd. The sock - H TCP: / / 0.0.0.0:2375
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target
Copy the code

Two: reload the configuration file and restart docker

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# 
[root@localhost ~]# systemctl restart docker.service
[root@localhost ~]# 
Copy the code

Confirm again whether the port 2375 of ExecStart we just modified is open or not, and find it is already open!

[root@localhost ~]# netstat -nlpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0. 01.:25            0.0. 0. 0:*               LISTEN      1106/master         
tcp        0      0 0.0. 0. 0:22              0.0. 0. 0:*               LISTEN      1019/sshd           
tcp6       0      0: :1:25                  :::*                    LISTEN      1106/master         
tcp6       0      0: : :2375                 :::*                    LISTEN      6292/dockerd        
tcp6       0      0: : :22                   :::*                    LISTEN      1019/sshd           
[root@localhost ~]# 
Copy the code

Three: IDEA uses Docker

3.1 Downloading the Docker plug-in

3.2 Testing the Connection

Preliminary guess is because the server firewall did not close the reason, boarded the server to see, as expected the firewall is open

3.3 Disabling the Firewall

 systemctl stop firewalld
Copy the code

If the server is restarted, the firewall will be automatically turned on again. Therefore, we need to permanently disable the firewall for testing convenience.

sudo systemctl disable firewalld
Copy the code

3.4 Retesting the Connection

It was found that the connection was successfulYou can now see all containers and images in the docker currently connectedThe basic operations on containers and images can be done here, such as pulling and pushing images, creating containers, stopping and deleting them. You don’t need to log in to the server to operate

Use Docker to deploy the Springboot project

4.1 Packaging the SpringBoot Project

4.2 Writing a DockerFile File

4.3 Uploading the JAR package and DockerFile File

4.4 Building an Image

[root@localhost idea]# docker build -f Dockerfile -t app.jar:1.0 .
Sending build context to Docker daemon  17.35MB
Step 1/5 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/5 : COPY *.jar /app.jar
 ---> 7453043a17c9
Step 3/5 : CMD ["--server.port=8080"]
 ---> Running in 7020002a89c3
Removing intermediate container 7020002a89c3
 ---> 0ace1426ab4e
Step 4/5 : EXPOSE 8080
 ---> Running in 80e97859a1de
Removing intermediate container 80e97859a1de
 ---> 3df752334d1a
Step 5/5 : ENTRYPOINT ["java"."-jar"."app.jar"]
 ---> Running in7794a2b1acc8 Removing intermediate container 7794a2b1acc8 ---> f116ab0e5883 Successfully built f116ab0e5883 Successfully  tagged app.jar:1.0
Copy the code

4.5 Starting a Container

[root@localhost idea]# docker run -d --name app app.jar:1.0
c64f1d1436e47b971cd3cd8b01af1d3168d5e2425612c09a7ab0c6da1cfd12e7
Copy the code

4.6 Access Tests

[root@localhost idea]# curl 172.17. 02.:8080
{"timestamp":"The 2021-09-15 T06: this. 020 + 00:00"."status":404."error":"Not Found"."path":"/"}[root@localhost idea]# 
[root@localhost idea]# curl 172.17. 02.:8080/hello Hello,Wolrd! [root@localhost idea]# lsCopy the code

Summary: At this point we deploy a Springboot project using Dockerfile with IDEA and the test is complete

Five: One-click Deployment of SpringBoot

5.1 Configuring poM Files

<properties>
    <java.version>1.8</java.version>
    <docker.image.prefix>jinlong</docker.image.prefix>
</properties>
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.0</version>
    <configuration>
        <imageName>${docker.image.prefix}/${project.artifactId}:${project.version}</imageName>
        <dockerHost>http://192.168.188.129:2375</dockerHost>
        <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>
Copy the code

5.2 Writing a DockerFile File

FROM java:8
VOLUME /tmp
ADD dockerspringboottest-0.01.-SNAPSHOT.jar app1.jar
ENTRYPOINT ["java"."-jar"."/app1.jar"]
Copy the code

5.3 Running the build command

mvn package docker:build
Copy the code

5.4 Viewing a Mirror

Found that the image was successfully built using IDEA