CentOS安装Docker和部署Springboot应用

 

目录

安装

配置安装包

设置镜像源

查看所有stable版本

安装

启动

查看运行状态

部署应用程序

Springboot配置

编写Dockerfile

配置pom文件

服务器配置

jar配置

创建镜像

运行容器

查看状态

容器运行状态

查看镜像

停止容器运行

查看所有的容器

查看容器内应用日志

删除容器

删除镜像


环境CentOS 7.2

 

安装

配置安装包

yum install -y yum-utils device-mapper-persistent-data lvm2

设置镜像源

yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

查看所有stable版本

yum list docker-ce --showduplicates | sort -r
[root@jiba02 ~]# yum list docker-ce --showduplicates | sort -r
 * updates: mirrors.cqu.edu.cn
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
 * extras: mirrors.163.com
docker-ce.x86_64            3:19.03.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.0-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.0-3.el7                     docker-ce-stable
docker-ce.x86_64            18.06.3.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.2.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.1.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.0.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.3.ce-1.el7                    docker-ce-stable
docker-ce.x86_64            17.03.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable
 * base: mirrors.cqu.edu.cn
Available Packages

安装

此处直接安装最新,或者指定版本安装

yum install docker-ce  #默认最新
yum install docker-ce-19.03.3-3.el7 #安装指定版本

启动

systemctl start docker

查看运行状态

systemctl status docker

部署应用程序

Springboot配置

编写Dockerfile

FROM java:8
VOLUME /tmp
ADD csdnranks-1.0.jar csdnranks.jar
EXPOSE 18200
ENTRYPOINT ["java","-jar","/csdnranks.jar"]

配置pom文件

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            

            
                com.spotify
                docker-maven-plugin
                1.2.1
                
                    ${docker.image.prefix}/${project.artifactId}
                    src/main/docker
                    
                        
                            /
                            ${project.build.directory}
                            ${project.build.finalName}.jar
                        
                    
                
            
        
    

服务器配置

jar配置

把编译好的jar包和dockerfile放在服务器的同一个目录下,如/home/files/

创建镜像

在目录种执行命令创建镜像

docker build -t csdn:1.0 .   #这个.代表当前目录,不能忽略

运行容器

把创建的镜像实例化到容器中,运行起来

可以配置不同的option,此处使用简单的命令

# -d 后台运行 
#csdn:1.0运行指定镜像名字和版本
#-p 8889主机端口号  映射 18200容器内应用端口号
#--name给运行的容器起个名字
#等等
docker run -d csdn:1.0 -p 8889:18200 --name=wdnmd

查看状态

容器运行状态

[root@jiba01 files]# docker container stats
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT    MEM %               NET I/O             BLOCK I/O           PIDS
be7072b9840e        wdnmd               0.06%               96.1MiB / 3.686GiB   2.55%               2.78kB / 704B       4.09MB / 0B         11

查看镜像

[root@jiba01 files]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
csdn                2.0                 d963a22b25e6        30 minutes ago      675MB
csdn                1.0                 fb4dcb6f38c9        2 hours ago         675MB
java                8                   d23bdf5b1b1b        2 years ago         643MB

停止容器运行

[root@jiba01 files]# docker stop wdnmd
wdnmd

查看所有的容器

(停止的正在运行的)

[root@jiba01 files]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS                     NAMES
c602fff078e6        csdn:1.0            "java -jar /csdnrank…"   3 seconds ago       Up 2 seconds                      0.0.0.0:8889->18200/tcp   wdnmd2
be7072b9840e        csdn:1.0            "java -jar /csdnrank…"   2 minutes ago       Exited (143) About a minute ago                             wdnmd
31be14c31c39        csdn:2.0            "java -jar /csdnrank…"   24 minutes ago      Exited (143) 3 minutes ago                                  great_mcnulty
d768426900f7        csdn:2.0            "java -jar /csdnrank…"   32 minutes ago      Exited (143) 30 minutes ago                                 wdnmw2

查看容器内应用日志

真正应用中可以通过docker安装elk组件进行微服务日志收集,此处是最简单查看容器的日志

[root@jiba01 files]# docker logs wdnmd

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.7.RELEASE)

2019-10-18 00:36:24.016  INFO 1 --- [           main] com.zzj.csdnranks.CsdnranksApplication   : Starting CsdnranksApplication v1.0 on b9c050c5354d with PID 1 (/csdnranks.jar started by root in /)
2019-10-18 00:36:24.019  INFO 1 --- [           main] com.zzj.csdnranks.CsdnranksApplication   : No active profile set, falling back to default profiles: default
2019-10-18 00:36:24.932  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-10-18 00:36:25.064  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 108ms. Found 1 repository interfaces.
2019-10-18 00:36:25.615  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'JPAConfiguration' of type [com.zzj.csdnranks.config.JPAConfiguration$$EnhancerBySpringCGLIB$$d5aa1b96] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-10-18 00:36:26.301  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-10-18 00:36:27.595  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-10-18 00:36:27.692  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
        name: default
        ...]
2019-10-18 00:36:27.828  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.10.Final}
2019-10-18 00:36:27.829  INFO 1 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-10-18 00:36:28.158  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-10-18 00:36:28.636  INFO 1 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-10-18 00:36:29.636  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-10-18 00:36:31.393  INFO 1 --- [           main] org.quartz.impl.StdSchedulerFactory      : Using default implementation for ThreadExecutor
2019-10-18 00:36:31.430  INFO 1 --- [           main] org.quartz.core.SchedulerSignalerImpl    : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2019-10-18 00:36:31.430  INFO 1 --- [           main] org.quartz.core.QuartzScheduler          : Quartz Scheduler v.2.3.1 created.
2019-10-18 00:36:31.431  INFO 1 --- [           main] org.quartz.simpl.RAMJobStore             : RAMJobStore initialized.
2019-10-18 00:36:31.432  INFO 1 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler meta-data: Quartz Scheduler (v2.3.1) 'quartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

2019-10-18 00:36:31.432  INFO 1 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.
2019-10-18 00:36:31.432  INFO 1 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler version: 2.3.1
2019-10-18 00:36:31.432  INFO 1 --- [           main] org.quartz.core.QuartzScheduler          : JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@3576ddc2
2019-10-18 00:36:31.520  INFO 1 --- [           main] o.s.s.quartz.SchedulerFactoryBean        : Starting Quartz Scheduler now
2019-10-18 00:36:31.522  INFO 1 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler quartzScheduler_$_NON_CLUSTERED started.
2019-10-18 00:36:31.534  INFO 1 --- [           main] com.zzj.csdnranks.CsdnranksApplication   : Started CsdnranksApplication in 8.55 seconds (JVM running for 9.373)

 

删除容器

如果正在运行,需要先执行stop命令停止容器运行后,再进行删除

[root@jiba01 files]# docker rm wdnmd
wdnmd
[root@jiba01 files]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                     NAMES
c602fff078e6        csdn:1.0            "java -jar /csdnrank…"   39 seconds ago      Up 38 seconds                 0.0.0.0:8889->18200/tcp   wdnmd2
31be14c31c39        csdn:2.0            "java -jar /csdnrank…"   25 minutes ago      Exited (143) 3 minutes ago                              great_mcnulty
d768426900f7        csdn:2.0            "java -jar /csdnrank…"   32 minutes ago      Exited (143) 31 minutes ago                             wdnmw2

删除镜像

[root@jiba01 files]# docker images  #删除前
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
csdn                2.0                 d963a22b25e6        34 minutes ago      675MB
csdn                1.0                 fb4dcb6f38c9        2 hours ago         675MB
java                8                   d23bdf5b1b1b        2 years ago         643MB
[root@jiba01 files]# docker rmi d963a22b25e6 #根据IMAGE ID,删除指定镜像
Untagged: csdn:2.0
Deleted: sha256:d963a22b25e61e764d65ab8507237f4a1bb8614a1a4f130ac7f31c1f48b9d8ee
Deleted: sha256:575f03d19cfc8dcd1abd9e59bcdcf3a8ee0930dfd43725a73c36ed2cad8e1e03
Deleted: sha256:11e4c45277da5e70ecfabd1a82fcc64e39d92567566b645e3112395ea3b423b9
Deleted: sha256:f6a1d8e4990c0a58f713e09e28e506214c026ebae34d7d825ee4f7448dcf4478
[root@jiba01 files]# docker images #删除后
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
csdn                1.0                 fb4dcb6f38c9        2 hours ago         675MB
java                8                   d23bdf5b1b1b        2 years ago         643MB

待续...docker-compose

 

参考 

https://www.cnblogs.com/yfalcon/p/9044246.html

https://www.cnblogs.com/yufeng218/p/8370670.html

http://www.ruanyifeng.com/blog/2018/02/docker-tutorial.html

 

你可能感兴趣的:(docker)