使用IDEA官方docker插件结合Dockerfile远程构建镜像

本文介绍如何使用JetBrains IDEA结合官方Docker插件和Dockerfile快速构建并推送Docker镜像,包括IDEA配置、Docker远程访问设置及具体操作步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jetbrains系列2024.1版本开始,有更新,看后边的 #2024-7-2 节

此方法同样适用于jetbrains系列的其他开发软件

0 简介

在IDEA中,如果是maven项目,可以使用插件

<plugin>
	<groupId>com.spotify</groupId>
	<artifactId>docker-maven-plugin</artifactId>
	<version>1.2.2</version>
</plugin>

或者

<plugin>
	<groupId>com.spotify</groupId>
	<artifactId>dockerfile-maven-plugin</artifactId>
	<version>1.4.13</version>
</plugin>

来构建docker镜像,不过还是有些繁琐,可以结合IDEA官方的docker插件,直接快速构建镜像

1 准备

安装docker,Windows可以安装Docker Desktop,CentOS可以参考我之前的文章:CentOS安装docker,安装完成后,需要开启远程访问

1.1 CentOS

vi /usr/lib/systemd/system/docker.service

# 在 ExecStart 后加上参数
 -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

完整信息如下

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket containerd.service

[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 -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=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

1.2 Windows

在这里插入图片描述

1.3 Mac

mac上安装docker,推荐使用OrbStack,个人版本免费使用

2 IDEA配置

在idea中,默认自带了docker插件
在这里插入图片描述
在这里插入图片描述
显示连接成功即可

3 打包项目

这里使用spring boot项目演示,如果是多模块项目,可以结合maven-mvnd这个工具,实现快速打包maven项目

mvnd clean package -DskipTests=true

4 编写dockerfile

点击文件左侧的运行
在这里插入图片描述
在这里插入图片描述
点击 run 以后,则会开始执行打包操作
在这里插入图片描述
切换到服务器也可以查看到
在这里插入图片描述
如果需要推送到远程仓库,右键镜像,push
在这里插入图片描述
然后选择相应的仓库即可

2024-7-2更新

升级jetbrains系列2024版本以后,配置docker远程构建,会提示无法找到docker.exe
在这里插入图片描述
升级后,需要自行配置docker.exe路径
在这里插入图片描述

下载docker.exe

地址:https://download.docker.com/win/static/stable/x86_64/

下载最新版本,解压后,idea中配置即可

在这里插入图片描述

buildx工具找不到

可能还会出现 buildx工具找不到 到不到的情况

在这里插入图片描述
在用户文件夹下(或在文件管理地址栏输入 %USERPROFILE% 定位)

新建 .docker 文件夹(注意有个“.”),下面再新建一个cli-plugins文件夹,访问如下 链接 ,下载下来,然后重命名为 docker-buildx.exe,然后放入该文件夹中

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值