通常项目上容器后,通过docker的方式来启动系统,需要经过一系列相关操作,例如:编译、打jar包、打镜像、发布、部署及启动等阶段。在各种自动化工具的出现,对打包、部署等工作带来了便利,一般交给git+Jenkins或者gitlab方式进行自动化部署。
然而,在项目开发、调试阶段,需要借助开发平台进行相关操作。本文主要讲述:如何在IDEA中通过插件来部署docker项目,以方便开发阶段的调试部署工作。
环境配置
1、Docker平台配置
为了确保本地IDEA能够访问服务器上的docker环境,则需要开放对docker平台的2375端口的访问权限。具体如下:
[administrator@JavaLangOutOfMemory luga % ]# vim /lib/systemd/system/docker.service # ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
[administrator@JavaLangOutOfMemory luga % ]# systemctl daemon-reload [administrator@JavaLangOutOfMemory luga % ]# systemctl restart docker.service [administrator@JavaLangOutOfMemory luga % ]# netstat -nlpt Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp6 0 0 :::2375 :::* LISTEN 2334/dockerd ... tcp6 0 0 :::22 :::* LISTEN 1007/sshd [administrator@JavaLangOutOfMemory luga % ]# curl http://127.0.0.1:2375/info {"ID":"JR7J:4SFC:IBDG:GS7G:4EDK:JXBX:TTYO:YIO3:KONX:ZEI7:JHEE:2YSM","Containers":0,"ContainersRunning":0,"ContainersPaused":0,"ContainersStopped":0,"Images":15,"Driver":"overlay2","DriverStatus":[["Backing Filesystem","<unknown>"],["Supports d_type","true"],["Native Overlay Diff","true"]],"SystemStatus":null,"Plugins":{"Volume":["local"],"Network":["bridge","host","ipvlan","macvlan","null","overlay"],"Authorization":null,"Log":["awslogs","fluentd","gcplogs","gelf","journald","json-file","local","logentries","splunk","syslog"]},"MemoryLimit":true,"SwapLimit":true,"KernelMemory":true,"KernelMemoryTCP":true,"CpuCfsPeriod":true,"CpuCfsQuota":true,"CPUShares":true,"CPUSet":true,"PidsLimit":true,"IPv4Forwarding":true,"BridgeNfIptables":true,"BridgeNfIp6tables":true,"Debug":false,"NFd":23,"OomKillDisable":true,"NGoroutines":36,"SystemTime":"2020-07-24T11:14:38.270098039+08:00","LoggingDriver":"json-file","CgroupDriver":"cgroupfs","NEventsListener":0,"KernelVersion":"3.10.0-1062.9.1.el7.x86_64","OperatingSystem":"CentOS Linux 7 (Core)","OSType":"linux","Architecture":"x86_64","IndexServerAddress":"https://index.docker.io/v1/","RegistryConfig":{"AllowNondistributableArtifactsCIDRs":[],"AllowNondistributableArtifactsHostnames":[],"InsecureRegistryCIDRs":["127.0.0.0/8"],"IndexConfigs":{"docker.io":{"Name":"docker.io","Mirrors":["https://registry.docker-cn.com/","http://hub-mirror.c.163.com/"],"Secure":true,"Official":true}},"Mirrors":["https://registry.docker-cn.com/","http://hub-mirror.c.163.com/"]},"NCPU":2,"MemTotal":1907802112,"GenericResources":null,"DockerRootDir":"/var/lib/docker","HttpProxy":"","HttpsProxy":"","NoProxy":"","Name":"k8s-master","Labels":[],"ExperimentalBuild":false,"ServerVersion":"19.03.8","ClusterStore":"","ClusterAdvertise":"","Runtimes":{"runc":{"path":"runc"}},"DefaultRuntime":"runc","Swarm":{"NodeID":"","NodeAddr":"","LocalNodeState":"inactive","ControlAvailable":false,"Error":"","RemoteManagers":null},"LiveRestoreEnabled":false,"Isolation":"","InitBinary":"docker-init","ContainerdCommit":{"ID":"7ad184331fa3e55e52b890ea95e65ba581ae3429","Expected":"7ad184331fa3e55e52b890ea95e65ba581ae3429"},"RuncCommit":{"ID":"dc9208a3303feef5b3839f4323d9beb36df0a9dd","Expected":"dc9208a3303feef5b3839f4323d9beb36df0a9dd"},"InitCommit":{"ID":"fec3683","Expected":"fec3683"},"SecurityOptions":["name=seccomp,profile=default"],"Warnings":["WARNING: API is accessible on http://0.0.0.0:2375 without encryption.\n Access to the remote API is equivalent to root access on the host. Refer\n to the 'Docker daemon attack surface' section in the documentation for\n more information: https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface"]}
2、IDEA Docker Plug配置
进入IDEA,File->Settings->Plugins->Install JetBrains plugin进入插件安装界面,在搜索框中输入docker,可以看到Docker integration,点击右边的Install按钮进行安装,完成后自动重启IDEA。
File->Settings->Build,Execution,Deployment->Docker 配置:
容器化部署
1、以echo-admin project为例,进行Spring Boot项目工程创建
2、更新pom文件,添加properties及plugin参数信息
3、配置DockerFile文件
4、创建Docker image及发布
5、Spring Boot容器镜像包构建完成,可进入对应的服务器查看容器的运行状况。