IDEA 一键 Docker 部署配置
一、Docker 开启远程访问
如果要部署到本地 Docker,则跳过这步
1. 修改配置文件
修改 docker.service 配置文件,开启远程访问
vim /usr/lib/systemd/system/docker.service
修改 [Service] 模块下 ExecStart 配置项,添加 -H tcp://0.0.0.0:2375
配置修改前配置:
[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
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
修改后配置:
[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 tcp://0.0.0.0:2375 -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
2. 重新加载配置文件
并重启Docker服务重新加载配置文件:
[root@centos-tools-105 ~]# systemctl daemon-reload
重启Docker服务:
[root@centos-tools-105 ~]# systemctl restart
3.检查配置是否生效:
[root@centos-tools-105 ~]# 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 12553/dockerd
4.配置安全组
公网IP,建议将 2375 端口设置为自己的IP可以访问,增加安全性。
5.访问测试
[root@centos-tools-105 ~]# curl http://127.0.0.1:2375/info
{"ID":"3PJA:ITZZ:GWMQ:G4U6:MMUZ:FZ6U:W7ER:3UA3:MRLI:6U2T:EZTF:RL6J","Containers":10,"ContainersRunning":8,"ContainersPaused":0,"ContainersStopped":2,"Images":30,"Driver":"overlay2","DriverStatus":[["Backing Filesystem","xfs"],["Supports d_type","true"],["Native Overlay Diff","true"],["userxattr","false"]],"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":89,"OomKillDisable":true,"NGoroutines":87,"SystemTime":"2021-11-12T17:38:35.616729553+08:00","LoggingDriver":"json-file","CgroupDriver":"cgroupfs","CgroupVersion":"1","NEventsListener":1,"KernelVersion":"3.10.0-1160.el7.x86_64","OperatingSystem":"CentOS Linux 7 (Core)","OSVersion":"7","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":[],"Secure":true,"Official":true}},"Mirrors":[]},"NCPU":16,"MemTotal":33565421568,"GenericResources":null,"DockerRootDir":"/var/lib/docker","HttpProxy":"","HttpsProxy":"","NoProxy":"","Name":"centos-tools-105","Labels":[],"ExperimentalBuild":false,"ServerVersion":"20.10.9","Runtimes":{"io.containerd.runc.v2":{"path":"runc"},"io.containerd.runtime.v1.linux":{"path":"runc"},"runc":{"path":"runc"}},"DefaultRuntime":"runc","Swarm":{"NodeID":"","NodeAddr":"","LocalNodeState":"inactive","ControlAvailable":false,"Error":"","RemoteManagers":null},"LiveRestoreEnabled":false,"Isolation":"","InitBinary":"docker-init","ContainerdCommit":{"ID":"5b46e404f6b9f661a205e28d59c982d3634148f8","Expected":"5b46e404f6b9f661a205e28d59c982d3634148f8"},"RuncCommit":{"ID":"v1.0.2-0-g52b36a2","Expected":"v1.0.2-0-g52b36a2"},"InitCommit":{"ID":"de40ad0","Expected":"de40ad0"},"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/go/attack-surface/"]}
二、连接Docker服务器
打开 Preference,搜索 Docker
1.连接本地 Docker
连接本地安装的 Docker 配置起来特别简单,选择 Docker for mac 点击确认就可以
2.服务器安装 Docker
三、Dockerfile编写
以 SpringBoot 程序为例,Dockerfile 一般是这样的
#Java 镜像
FROM openjdk:8-jdk-alpine
# 作者名称
MAINTAINER lhc <lhc@lihaocheng.cn>
VOLUME /temp
COPY target/项目名.jar 项目名.jar
# 设置启动参数
CMD ["--spring.profiles.active=local"]
ENTRYPOINT ["java","-Xms512M","-Xmx1024M","-jar","sale-third.jar"]
我个人比较喜欢在项目中加上 Arthas
FROM openjdk:8-jdk-alpine
MAINTAINER lhc <lhc@lihaocheng.cn>
VOLUME /temp
COPY target/项目名.jar 项目名.jar
COPY --from=hengyunabc/arthas:latest /opt/arthas /opt/arthas
EXPOSE 9091
CMD ["--spring.profiles.active=local"]
RUN apk add --no-cache tini
ENTRYPOINT ["/sbin/tini","--","java","-Xms512M","-Xmx1024M","-jar","sale-third.jar"]
四、镜像构建以及容器运行配置
点击 Dockerfile 文件上的运行按钮并选择 New Run Configuraion.
1.镜像以及容器运行相关配置
2.maven打包命令配置
选择配置项中最下面的 Before launch 选项,配置 maven 打包命令maven 打包命令配置
clean install -Dmaven.test.skip=true
3.构建镜像并启动容器
点击 Dockerfile 文件上的运行按钮并选择 Run 命令。
执行完毕即可在 Services 窗口中看到所生成的镜像以及运行的容器,当然也可以在Docker服务器上通过命令查看镜像以及容器信息。同时通过 IDEA 可以直接查看容器内服务日志
评论区