Docker《三:idea使用docker》

462 阅读1分钟

一:开启docker远程访问功能

主要就是修改ExecStart的配置为ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.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.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

二:重新加载配置文件并重启docker

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

再次确认我们刚刚修改的ExecStart的2375端口有没有打开,发现已经打开了!

[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.0.1: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 ~]# 

三:idea使用docker

3.1下载docker插件

image.png

3.2测试连接

image.png 初步猜测是由于服务器防火墙未关闭的原因,登上服务器查看,果然防火墙是开着的 image.png

3.3关闭防火墙

 systemctl stop firewalld

上面的命令只是在当前会话关闭防火墙,如果服务器重启,那么防火墙又会自动打开,所以为了测试方便,我们需要永久关闭防火墙,参考命令如下:

sudo systemctl disable firewalld

image.png

3.4重新测试连接

发现已经连接成功了 image.png 可以看到当前连接的docker里面所有的容器和镜像了 image.png 关于容器和镜像的基本操作基本上都可以在这里完成,比如镜像的拉取,推送,容器的创建,停止以及删除等。就不需要再登陆服务器才能操作了 image.png

四:使用docker部署springboot项目

4.1打包springboot项目

image.png

4.2编写dockerfile文件

image.png

4.3将jar包和dockerfile文件上传

image.png

4.4构建镜像

[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 in 7794a2b1acc8
Removing intermediate container 7794a2b1acc8
 ---> f116ab0e5883
Successfully built f116ab0e5883
Successfully tagged app.jar:1.0

4.5启动容器

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

4.6访问测试

[root@localhost idea]# curl 172.17.0.2:8080
{"timestamp":"2021-09-15T06:42:32.020+00:00","status":404,"error":"Not Found","path":"/"}[root@localhost idea]# 
[root@localhost idea]# curl 172.17.0.2:8080/hello
Hello,Wolrd![root@localhost idea]# ls

总结:到此我们结合idea使用dockerfile部署一个springboot项目就测试完成了

五:一键部署springboot

5.1配置pom文件

<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>

5.2编写dockerfile文件

FROM java:8
VOLUME /tmp
ADD dockerspringboottest-0.0.1-SNAPSHOT.jar app1.jar
ENTRYPOINT ["java","-jar","/app1.jar"]

image.png

5.3执行build命令

image.png

mvn package docker:build

5.4查看镜像

image.png 发现使用idea已经成功build了一个镜像了