SpringBoot项目容器化实战

目录

1. IDEA插件方式

1.1 Docker服务开启远程连接访问

1.2 IDEA安装插件Docker

1.3 IDEA连接docker

1.4 项目配置

2. Dockerfile方式

3. Docker Compose 方式


此文档仅仅是初级的docker实战,可以快速上手,具体深入内容暂不涉及

在服务器上面部署Docker 略

1. IDEA插件方式

1.1 Docker服务开启远程连接访问

默认的dokcer是不支持远程访问的,需要加点配置,开启Docker的远程访问

如果docker上面已经有运行中的容器,请关闭:

docker container stop

修改Docker服务文件,需要先切换到root用户

打开文件:vim /lib/systemd/system/docker.service 找到ExecStart 在后面添加 -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375

保存关闭

重启服务:

守护进程重启:sudo systemctl daemon-reload 重启docker服务:sudo systemctl restart docker

检测端口2375是否启用:

lsof -i:2375

 

说明开启了远程连接访问。

1.2 IDEA安装插件Docker

1.3 IDEA连接docker

安装如下图配置,显示连接成功,就说明连接成功,否则,说明第一步失败了。

SpringBoot项目容器化实战_第1张图片

 

1.4 项目配置

在maven.xml中配置插件使得项目在install的同时自动生成镜像,如下:

 
        
        ${project.artifactId}
​
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
​
            
            
                com.spotify
                docker-maven-plugin
                1.0.0
                
                
                    
                        build-image
                        
                        package
                        
                            build
                        
                    
                
​
                
                    
                    wolf/${project.artifactId}
​
                    
                    
                        latest
                    
​
                    
                    java
                    
                    
                    /ROOT
​
                    
                    ["java", "-version"]
​
                    
                    ["java", "-jar", "/${project.build.finalName}.jar"]
​
                    
                    http://172.31.186.236:2375
​
                    
                    
                        
                            /
                            
                            ${project.build.directory}/
                            
                            ${project.build.finalName}.jar
                        
                    
​
                
            
​
        
    

install成功以后,会得到镜像,如下图:

右键镜像,创建容器启动即可。

参考博客:

idea 使用docker插件,实现一键自动化部署。_起止洺的博客-CSDN博客

菜鸟的IDEA使用Docker插件_最强菜鸟-CSDN博客

 

2. Dockerfile方式

这个就稍微麻烦点了,需要自己写一个Dockerfile文件,比如:

FROM java
​
LABEL maintainer="xxxx"
​
LABEL version=latest
​
EXPOSE 1234 8080
​
CMD "nuhop java -jar dockerDemo2-0.0.1-SNAPSHOT.jar &"

项目生成一个jar 包

如下图,放到服务器中,Dockerfile和jar放到同一级

SpringBoot项目容器化实战_第2张图片

执行命令:docker image build -t demo-docker2:latest .

查看下服务器里面的当前镜像有没有:docker images

SpringBoot项目容器化实战_第3张图片

 

可以看到镜像生成好了,创建一个容器,运行一下:

[root@172 dockerDemo2]# docker container run -it c887eceeff6a /bin/bash
root@44cb500b86a7:/# ls -l
total 4
drwxr-xr-x   1 root root   19 Jan 17  2017 bin
drwxr-xr-x   2 root root    6 Dec 28  2016 boot
......
root@44cb500b86a7:/#

可以看到已经进入容器里面了

在不关闭容器的情况下退出去:执行ctrl +p 和ctrl + q,然后再次查看当前服务器中的容器数量

[root@172 dockerDemo2]# docker container ls
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                  PORTS                           NAMES
44cb500b86a7   c887eceeff6a   "/bin/bash"              3 minutes ago   Up 3 minutes            1234/tcp, 8080/tcp              sweet_feistel
f5baf401d405   e5aed97adf10   "gunicorn superset.a…"   24 hours ago    Up 24 hours (healthy)   172.31.186.236:9088->8088/tcp   superset01
9be2f4a4094e   redis          "docker-entrypoint.s…"   2 weeks ago     Up 22 minutes           6379/tcp                        always
[root@172 dockerDemo2]#

3. Docker Compose 方式

Compose 是用于定义和运行多容器 Docker 应用程序的工具。通过 Compose,您可以使用 YML 文件来配置应用程序需要的所有服务。然后,使用一个命令,就可以从 YML 文件配置中创建并启动所有服务。

也就是说 docker-compose是为了多个容器服务配置而准备的,当多个容器启动的时候,一个一个的Dockerfile会很繁琐。

举个例子:

新建一个项目:

 

package com.docker.compose;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@Slf4j
public class DockerComposeDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DockerComposeDemoApplication.class, args);
        log.info("----------------------- DockerComposeDemoApplication started -----------------------");
        try {
            while(true){
                log.info("starting..........");
                Thread.sleep(3_000);
            }
        }catch (Exception e){

        }
    }

}

然后配置Dockerfile

FROM java:8
MAINTAINER xxx

LABEL version=latest

ADD docker-compose-demo-1.0.jar /docker-compose-demo-1.0.jar


EXPOSE 1234 8033

#容器运营的时候启动jar
CMD ["nohup","java","-jar","/docker-compose-demo-1.0.jar","&"]
#ENTRYPOINT ["java","-jar","/docker-compose-demo-1.0.jar","&"]

然后编排docker-compose.yml,这里我们为了测试,额外指定了一个redis镜像:

version: "3.9"

services:
  docker-compose-demo:
    container_name: docker-compose-demo-container
    build:
      context: .
      dockerfile: Dockerfile
    image: docker-compose-demo:1.0
    ports:
    - "1234:8033"
    restart: "always"
    environment:
      TZ: Asia/Shanghai
  redisDemo:
    container_name: redis-container
    image: "redis:latest"

在服务器中的目录:

SpringBoot项目容器化实战_第4张图片

 

查看当前服务器中镜像情况: docker images

[root@172 docker-compose-demo]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED        SIZE
demo-docker2          latest    c887eceeff6a   10 days ago    643MB
wolf/docker-demo      latest    583273d16193   10 days ago    661MB
openjdk               8         08121337b7a4   2 weeks ago    526MB
netdata/netdata       latest    c21cfd733b21   2 weeks ago    336MB
python                3.9.5     9b0d330dfd02   2 months ago   886MB
redis                 latest    08502081bff6   2 months ago   105MB
node                  12        42bff2591ccb   2 months ago   918MB
amancevice/superset   latest    e5aed97adf10   5 months ago   2.24GB
node                  10        28dca6642db8   5 months ago   910MB
python                3.7.9     65d5b6c539fd   7 months ago   877MB
centos                latest    300e315adb2f   9 months ago   209MB
java                  8         d23bdf5b1b1b   4 years ago    643MB
java                  latest    d23bdf5b1b1b   4 years ago    643MB

当前运行的容器有哪些: docker container ls 

 [root@172 ~]# docker container ls
CONTAINER ID   IMAGE          COMMAND                  CREATED       STATUS                 PORTS                           NAMES
f5baf401d405   e5aed97adf10   "gunicorn superset.a…"   11 days ago   Up 11 days (healthy)   172.31.186.236:9088->8088/tcp   superset01
[root@172 ~]#

在当前文件夹中执行启动命令:docker-compose up &

[root@172 docker-compose-demo]# docker-compose up &
[1] 8818
[root@172 docker-compose-demo]# Creating network "docker-compose-demo_default" with the default driver
Building docker-compose-demo
Sending build context to Docker daemon  17.35MB
Step 1/6 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/6 : MAINTAINER lxw
 ---> Running in 625155cd19f2
Removing intermediate container 625155cd19f2
 ---> b3ec7ce6d62f
Step 3/6 : LABEL version=latest
 ---> Running in 87654edff22c
Removing intermediate container 87654edff22c
 ---> 1b33331cdb0e
Step 4/6 : ADD docker-compose-demo-1.0.jar /docker-compose-demo-1.0.jar
 ---> cc83eeddaaad
Step 5/6 : EXPOSE 1234 8033
 ---> Running in 4a3fa9248419
Removing intermediate container 4a3fa9248419
 ---> 632b9d59d665
Step 6/6 : CMD ["nohup","java","-jar","/docker-compose-demo-1.0.jar","&"]
 ---> Running in e4b2bbe03883
Removing intermediate container e4b2bbe03883
 ---> 46080ef283e8
Successfully built 46080ef283e8
Successfully tagged docker-compose-demo:1.0
WARNING: Image for service docker-compose-demo was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating redis-container               ... done
Creating docker-compose-demo-container ... done
Attaching to redis-container, docker-compose-demo-container
redis-container        | 1:C 18 Sep 2021 08:04:08.861 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-container        | 1:C 18 Sep 2021 08:04:08.861 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=1, just started
redis-container        | 1:C 18 Sep 2021 08:04:08.861 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis-container        | 1:M 18 Sep 2021 08:04:08.862 * monotonic clock: POSIX clock_gettime
redis-container        | 1:M 18 Sep 2021 08:04:08.862 * Running mode=standalone, port=6379.
redis-container        | 1:M 18 Sep 2021 08:04:08.862 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis-container        | 1:M 18 Sep 2021 08:04:08.862 # Server initialized
redis-container        | 1:M 18 Sep 2021 08:04:08.862 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis-container        | 1:M 18 Sep 2021 08:04:08.863 * Ready to accept connections
docker-compose-demo-container |
docker-compose-demo-container |   .   ____          _            __ _ _
docker-compose-demo-container |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
docker-compose-demo-container | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
docker-compose-demo-container |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
docker-compose-demo-container |   '  |____| .__|_| |_|_| |_\__, | / / / /
docker-compose-demo-container |  =========|_|==============|___/=/_/_/_/
docker-compose-demo-container |  :: Spring Boot ::                (v2.5.4)
docker-compose-demo-container |
docker-compose-demo-container | 2021-09-18 16:04:09.958  INFO 1 --- [           main] c.d.c.DockerComposeDemoApplication       : Starting DockerComposeDemoApplication v1.0 using Java 1.8.0_111 on 6213c531ccfd with PID 1 (/docker-compose-demo-1.0.jar started by root in /)
docker-compose-demo-container | 2021-09-18 16:04:09.961  INFO 1 --- [           main] c.d.c.DockerComposeDemoApplication       : No active profile set, falling back to default profiles: default
docker-compose-demo-container | 2021-09-18 16:04:11.002  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8033 (http)
docker-compose-demo-container | 2021-09-18 16:04:11.015  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
docker-compose-demo-container | 2021-09-18 16:04:11.015  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.52]
docker-compose-demo-container | 2021-09-18 16:04:11.064  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
docker-compose-demo-container | 2021-09-18 16:04:11.065  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1044 ms
docker-compose-demo-container | 2021-09-18 16:04:11.440  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8033 (http) with context path ''
docker-compose-demo-container | 2021-09-18 16:04:11.452  INFO 1 --- [           main] c.d.c.DockerComposeDemoApplication       : Started DockerComposeDemoApplication in 1.914 seconds (JVM running for 2.318)
docker-compose-demo-container | 2021-09-18 16:04:11.455  INFO 1 --- [           main] c.d.c.DockerComposeDemoApplication       : ----------------------- DockerComposeDemoApplication started -----------------------
docker-compose-demo-container | 2021-09-18 16:04:11.455  INFO 1 --- [           main] c.d.c.DockerComposeDemoApplication       : starting..........
docker-compose-demo-container | 2021-09-18 16:04:14.455  INFO 1 --- [           main] c.d.c.DockerComposeDemoApplication       : starting..........
docker-compose-demo-container | 2021-09-18 16:04:17.456  INFO 1 --- [           main] c.d.c.DockerComposeDemoApplication       : starting..........

发现项目启动了,但是没有在后台执行,一直是当前页面,ctrl+c只能短暂退出,幸亏我及时的 复制了命令:docker-compose down 才关闭了,为什么一直是前台执行,还需要我这个小白继续研究,

无论如何,项目是启动的,启动的时候,重新打开一个客户端查看下:

[root@172 ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
docker-compose-demo   1.0       46080ef283e8   41 seconds ago   661MB
demo-docker2          latest    c887eceeff6a   10 days ago      643MB
wolf/docker-demo      latest    583273d16193   10 days ago      661MB
openjdk               8         08121337b7a4   2 weeks ago      526MB
netdata/netdata       latest    c21cfd733b21   2 weeks ago      336MB
python                3.9.5     9b0d330dfd02   2 months ago     886MB
redis                 latest    08502081bff6   2 months ago     105MB
node                  12        42bff2591ccb   2 months ago     918MB
amancevice/superset   latest    e5aed97adf10   5 months ago     2.24GB
node                  10        28dca6642db8   5 months ago     910MB
python                3.7.9     65d5b6c539fd   7 months ago     877MB
centos                latest    300e315adb2f   9 months ago     209MB
java                  8         d23bdf5b1b1b   4 years ago      643MB
java                  latest    d23bdf5b1b1b   4 years ago      643MB
[root@172 ~]#
[root@172 ~]#
[root@172 ~]#
[root@172 ~]# docker container ls
CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS                 PORTS                                                 NAMES
6213c531ccfd   docker-compose-demo:1.0   "nohup java -jar /do…"   49 seconds ago   Up 45 seconds          1234/tcp, 0.0.0.0:1234->8033/tcp, :::1234->8033/tcp   docker-compose-demo-container
50590113d86a   redis:latest              "docker-entrypoint.s…"   49 seconds ago   Up 45 seconds          6379/tcp                                              redis-container
f5baf401d405   e5aed97adf10              "gunicorn superset.a…"   11 days ago      Up 11 days (healthy)   172.31.186.236:9088->8088/tcp                         superset01
[root@172 ~]#

可以看到,docker-compose基于Dockerfile生成了镜像docker-compose-demo,并且,运行中的容器,从一个变成了三个。

总结:

docker自学基本实战到这里了,相关原理还是需要继续深入理解,后续待补充吧。

参考博客:docker-compose使用部署jar项目_weixin_33862041的博客-CSDN博客

docker-compose部署springboot项目_xiaoguaishou_520的博客-CSDN博客

Docker-compose 部署jar包服务_小葫芦的博客-CSDN博客

你可能感兴趣的:(docker,docker,java,spring,boot)