当前位置: 首页 > news >正文

Docker开启远程访问+idea配置docker+dockerfile发布java项目

一、docker开启远程访问

1.编辑docker服务文件
vim /usr/lib/systemd/system/docker.service

docker.service原文件如下:

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service time-set.target
Wants=network-online.target containerd.service
Requires=docker.socket[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
TimeoutStartSec=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

找到ExecStart头添加代码如图所示:

-H tcp://0.0.0.0

添加完成,如下图标记
在这里插入图片描述

2.重启docker的daemon服务
systemctl daemon-reload
3.重启docker服务
systemctl restart docker
4.开启防火墙指定端口:2375
firewall-cmd --zone=public --add-port=2375/tcp --permanent
5.重启防火墙
firewall-cmd --reload
6.进入浏览器中访问:http://ip:2375/version

如图所示
在这里插入图片描述
能够访问到该页面说明已经开启了远程访问。

二、idea配置docker

1.进入idea,进入目录:File-Settings-Plugins搜索:docker,并安装:如图:

在这里插入图片描述

2.配置docker,进入目录:File-Settings-Build,Execution,Deployment

如图:
在这里插入图片描述
Engine API URL:

tcp://ip:2375

最后点击Apply,再点击ok

3.在配置docker部署时就可以选择以上添加的docker了

如图:
在这里插入图片描述
在这里插入图片描述

三、编写一个java Demo,使用dockerfile部署

项目工程如下:
在这里插入图片描述
代码展示:HelloWorldController.java

package com.hcl.helloworld.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("hello")
public class HelloWorldController {@GetMapping("world")public String hello(){return "蜉蝣之羽,衣裳楚楚。<br/>"+"心之忧矣,於我归处?<br/>" +"蜉蝣之翼,采采衣服。<br/>" +"心之忧矣,於我归息?<br/>" +"蜉蝣掘阅,麻衣如雪。<br/>" +"心之忧矣,於我归说?";}
}

代码展示:HelloWorldApplication.java

package com.hcl.helloworld;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class HelloWorldApplication {public static void main(String[] args) {SpringApplication.run(HelloWorldApplication.class, args);}}

代码展示:application.yaml

server:port: 8081

代码展示:Dockerfile

# 指定环境镜像作为该容器的基础环境,如springboot应用最起码得有jdk环境
FROM openjdk:8# 执行维护者信息
MAINTAINER abliner# 创建一个存放工程的目录
RUN mkdir -p /data/project-bakADD target/hello-world-0.0.1-SNAPSHOT.jar /data/project-bak/hello-world-0.0.1-SNAPSHOT.jar# 对外暴露的端口
EXPOSE 8084# 执行启动
ENTRYPOINT ["/bin/sh","-c","java -jar /data/project-bak/hello-world-0.0.1-SNAPSHOT.jar"]

四、开始配置

1.进入配置,如图:

在这里插入图片描述

2.添加docker,选择Dockerfile如图:

在这里插入图片描述

3.如图

在这里插入图片描述
最后点击Apply再点击ok
注意:dockerfile中对外暴露的端口也就是上面配置的端口“8084”开启防火墙的一定要去开放端口,否则无法访问。如果是阿里云上面则也要配置打开该端口。

4.idea打包项目,先clear再package,保证是最新的包

如图:
在这里插入图片描述

5.启动

如图:
在这里插入图片描述

6.访问

在这里插入图片描述

http://www.lryc.cn/news/193124.html

相关文章:

  • 基于nodejs+vue教学辅助管理系统
  • Qt 子线程中无限递归的信号槽导致主线程槽失效的原因和解决办法
  • 实施 DevSecOps 最佳实践
  • 第56节——redux-toolkit中的createAction——了解
  • 【数据结构】排序--选择排序(堆排序)
  • C# 图解教程 第5版 —— 第2章 C# 和 .NET Core
  • 数据结构 | Huffman TreeCode
  • mysql拼接字符串函数
  • python基础(5):深入理解 python 中的赋值、引用、拷贝、作用域
  • 《动手学深度学习 Pytorch版》 8.6 循环神经网络的简洁实现
  • leetcode做题笔记173. 二叉搜索树迭代器
  • RPA流程自动化的优势和好处
  • 搭建 Hadoop 生态集群大数据监控告警平台
  • 课题学习(七)----粘滑运动的动态算法
  • python二次开发CATIA:测量曲线长度
  • 从零开始学习调用百度地图网页API:二、初始化地图,鼠标交互创建信息窗口
  • Yarn基础入门
  • element picker 时间控件,指定区间和指定月份置灰
  • thinkphp6
  • Android 13.0 USB鼠标右键改成返回键的功能实现
  • 超低延时 TCP/UDP IP核
  • Python与数据库存储
  • RN操作SQLite数据库的包(sqlite-helper.js)及其使用
  • 软件测试学习(四)自动测试和测试工具、缺陷轰炸、外包测试、计划测试工作、编写和跟踪测试用例
  • 【Rust日报】2023-10-12 论文:利用公共信息评估 Rust 代码库
  • 微信小程序入门
  • 【RocketMQ系列二】通过docker部署单机RocketMQ
  • 中缀表达式转后缀表达式
  • Zabbix 使用同一ODBC监控不同版本MySQL
  • Swagger3.0 与spring boot2.7x 整合避免swagger2.0与boot2.7冲突