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

zxbandzby
2
2025-12-19

一、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:2375

添加完成后的ExecStart行:

ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375 --containerd=/run/containerd/containerd.sock

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.安装Docker插件

  • 进入idea

  • 进入目录: File → Settings → Plugins

  • 搜索: docker, 并安装

2.配置Docker连接

  • 进入目录: File → Settings → Build, Execution, Deployment → Docker

  • 点击"+"添加新连接

  • Engine API URL: tcp://ip:2375

  • 最后点击Apply, 再点击ok

3.使用Docker配置

在配置docker部署时就可以选择以上添加的docker服务器

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

项目工程结构

复制项目根目录/
├── src/
│   └── main/
│       ├── java/com/hcl/helloworld/
│       │   ├── controller/HelloWorldController.java
│       │   └── HelloWorldApplication.java
│       └── resources/application.yaml
├── target/
│   └── hello-world-0.0.1-SNAPSHOT.jar
└── 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-bak

ADD 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.进入运行配置

  • 点击IDEA右上角的运行配置下拉菜单

  • 选择"Edit Configurations..."

2.添加Docker配置

  • 点击"+"添加新配置

  • 选择Docker → Dockerfile

  • 选择项目中的Dockerfile文件

  • 设置镜像标签和容器名称

  • 配置端口映射: 8084:8084

3.注意事项

  • dockerfile中对外暴露的端口是8084

  • 服务器防火墙需要开放8084端口

  • 如果是阿里云等云服务器,安全组也需要配置开放8084端口

4.打包项目

在IDEA的Maven面板中:

  • 先执行clean

  • 再执行package

    确保生成最新的jar包

5.启动部署

点击运行按钮,IDEA将:

  • 构建Docker镜像

  • 推送镜像到远程Docker服务器

  • 创建并启动容器

6.访问应用

在浏览器中访问: http://ip:8084/hello/world

动物装饰