tanger
发布于 2025-01-03 / 4 阅读 / 0 评论 / 0 点赞

远程容器开发配置

维护多个项目,每个项目的jdk版本,node版本不时,一般的解决方案是安装sdkman(java),nvm(node)的版本管理器进行版本切换。现在有了新的选择,容器开发(devcontainers),可以为每个项目配置自己的开发环境,方便在不同的设备上快速部署开发环境,免去安装环境以及解决环境冲突的麻烦。

1、根据自己的环境需要创建一个容器镜像

以下以jdk21+node18为例子

  • 创建一个DockerFile,名称为devcontainer(名称随意)

# 基于微软的 Ubuntu 基础镜像
FROM mcr.microsoft.com/devcontainers/base:ubuntu

# 安装 JDK 21
RUN apt-get update && apt-get install -y openjdk-21-jdk

# 安装 Node.js 18
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash -
RUN apt-get install -y nodejs

# 安装 Maven
RUN apt-get install -y maven

  • 构建环境镜像

docker build -f devcontainer -t my_dev_nv:latest .
  • 编写.devcontainer/devcontainer.json文件

{
	"name": "ocker_env",
    // 上面构建的镜像名称
	"image": "my_dev_nv:latest",

	// 👇 容器映射的端口
	"forwardPorts": [8080],

	// 👇 Configure tool-specific properties.
  "customizations" : {
    "jetbrains" : {
      "backend" : "IntelliJ"
    }
  },

	// 👇 Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
	// "remoteUser": "root"
}
  • idea / vscode / jetbrains gateway 选择这个devcontainer.json即可开启远程容器开发(每个ide的操作方式可以参考官方的说明或者百度一下)

ps:一些其他的配置,注释掉的

{
	"name": "dsb_docker_env",
	//"image": "mcr.microsoft.com/devcontainers/java:1-${templateOption:imageVariant}",
	//"image": "mcr.microsoft.com/devcontainers/java:1-21-bullseye",
	"image": "my_dev_nv:latest",
	//"image": "openjdk:21",
  // 👇 Features to add to the Dev Container. More info: https://containers.dev/implementors/features.
//	"features": {
//		"ghcr.io/devcontainers/features/docker-in-docker:2": {},
//		"ghcr.io/prulloac/devcontainer-features/bun:1": {},
//		"ghcr.io/devcontainers/features/node:1": {
//			"version": "18"
//		},
//		"ghcr.io/devcontainers/features/common-utils:2": {
//			"configureZshAsDefaultShell": true
//		},
//		"ghcr.io/devcontainers/features/java:1": {
//			"version": "none",
//			//"installMaven": "${templateOption:installMaven}",
//			"installMaven": true,
//			//"installGradle": "${templateOption:installGradle}"
//			"installGradle": false
//		},
//		"ghcr.io/devcontainers/features/github-cli:1": {}
//	},

	// 👇 Use 'forwardPorts' to make a list of ports inside the container available locally.
	"forwardPorts": [8080],

	// 👇 Configure tool-specific properties.
  "customizations" : {
    "jetbrains" : {
      "backend" : "IntelliJ"
    }
  },

	// 👇 Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
	// "remoteUser": "root"
}


评论