[开源]制作docker镜像不依赖linux和Docker环境
阅读原文时间:2021年04月21日阅读:1

背景

最近群友们经常反馈docker镜像制作起来有点麻烦,我开源的antdeploy工具虽然可以制作镜像但是必须有一个提前:有一台安装好docker的linux服务器。因为大家开发环境基本上都是windows。为了制作镜像专门搞一台linux服务器有点浪费,也很麻烦。需要一种技术能制作docker镜像不依赖linux环境且脱离于docker环境

技术调研

谷歌有一个开源项目 https://github.com/GoogleContainerTools/jib

这个应该就是我想要的了,但是这个开源项目主要是提供 maven插件 和 gradle插件来 快速制作镜像

如何让它脱离maven或者gradle环境的束缚来提供服务呢。

只能参考谷歌的java代码自己实现一个能够跨平台的解决方案

技术成果

这里我采用了微软的asp5.0技术一比一仿写了google的jib项目。

目前我已经集成到我的开源项目AntDeploy(一键部署发布工具)里面

支持功能如下:

  • 一键制作镜像并发布到dockerhub仓库

  • 一键制作镜像并发布到阿里云镜像仓库

  • 一键制作镜像并发布到腾讯云镜像仓库

注意:采用标准的auth协议的镜像仓库都支持一键推送

如何使用

支持下面2种方式使用

  • vs插件方式(可以直接项目右键,一键搞定)

  • 独立软件方式(可以发布java等其他语言)

这里以vs插件方式示例

下面演示创建一个aspnet5.0的web项目并采用antdeploy插件一键创建docker镜像发布到阿里云镜像仓库,然后在linux服务器上跑起来这个web项目

使用的环境:

  • Visualstuido 2019

  • 已安装AntDeploy插件最新版

创建工程(testimagepush)如下图:步骤(略)

在工程文件点击鼠标右键,找到AntDeploy

打开AntDeploy如下图:

配置参数
From里面要填参数的意思:

名称

含义

基础镜像

等同于DockerFile里面的from

UserName

如果你用的基础镜像是私有的话就需要,阿里云或腾讯云也需要

Pwd

如果你用的基础镜像是私有的话就需要,阿里云或腾讯云也需要

Http代理

在拉取这个基础镜像的时候如果是需要科学访问(特别是dockerhub)的话就需要用这个了

To里面要填参数的意思:

名称

含义

目标镜像

这个是你要推送到的镜像仓库地址(下面会举例阿里云,腾讯云,dockerhub,详细看下面解说的例子)

UserName

如果你推送的镜像仓库是私有的话就需要,阿里云或腾讯云也需要

Pwd

如果你推送的镜像仓库是私有的话就需要,阿里云或腾讯云也需要

Http代理

在推送生创建好的镜像的时候如果是需要科学访问(特别是dockerhub)的话就需要用这个了

EntryPoint

你的镜像的运行入口点

CMD

入口点要带的一些参数 多个用->隔开

玩过docker的人上面的参数应该比较熟悉,因为创建一个docker镜像至少需要这么些配置

如上图:

  • 因为我是5.0项目所以我用的是官方的5.0镜像

  • 因为基础镜像在国外我配置了代理加速pull

  • 目标镜像是配置的阿里云镜像仓库的地址(你开通了阿里云镜像仓库功能注意得开启自动创建的功能)

  • 因为阿里云的镜像仓库需要账户名和密码所以得配置上

  • EntryPoint程序入口点为dotnet

  • 参数写的是当前项目dll(这么配的意思就是如果你跑这个镜像生成的容器就会默认启动你的应用)

如果你需要将某些文件排除打包的话可以在【其他配置】进行配置

点击发布按钮

可以查看详细日志:

16:26:52|INFO|-----------------Start publish[Ver:7.30]-----------------  16:26:52|INFO|Visual Studio Version : 16.0  16:26:52|INFO|CurrentProjectFolder:  file://C:\Users\Administrator\source\repos\testimagepush\testimagepush#link316:26:52|INFO|current project Path:C:\Users\Administrator\source\repos\testimagepush\testimagepush\testimagepush.csproj  16:26:52|INFO|dotnet publish "C:\Users\Administrator\source\repos\testimagepush\testimagepush\testimagepush.csproj" -c Release -o "C:\Users\Administrator\source\repos\testimagepush\testimagepush\bin\Release\dockerImage\publish\"16:26:53|INFO|版权所有(C)MicrosoftCorporation。保留所有权利。16:26:53|INFO|正在确定要还原的项目…16:26:53|INFO|正在确定要还原的项目…16:26:53|INFO|所有项目均是最新的,无法还原。16:26:53|INFO|所有项目均是最新的,无法还原。16:26:55|INFO|testimagepush->C:\Users\Administrator\source\repos\testimagepush\testimagepush\bin\Release\net5.0\testimagepush.dll16:26:55|INFO|testimagepush->C:\Users\Administrator\source\repos\testimagepush\testimagepush\bin\Release\net5.0\testimagepush.Views.dll16:26:55|INFO|testimagepush->C:\Users\Administrator\source\repos\testimagepush\testimagepush\bin\Release\net5.0\testimagepush.Views.dll16:26:55|INFO|testimagepush->C:\Users\Administrator\source\repos\testimagepush\testimagepush\bin\Release\dockerImage\publish\16:26:55|INFO|testimagepush->C:\Users\Administrator\source\repos\testimagepush\testimagepush\bin\Release\dockerImage\publish\16:26:55|INFO|publishtarget==>file://C:\Users\Administrator\source\repos\testimagepush\testimagepush\bin\Release\dockerImage\publish#link1616:26:55|INFO|-----------------Startpublishdockerimage-----------------16:26:55|INFO|-----------------DeployStart-----------------16:26:55|INFO|jib.exepush--configfile="C:\Users\Administrator\AppData\Roaming\020198643be8650696268b110a25104d_param.json"16:26:56|INFO|Start:Pullingmanifestofbaseimagemcr.microsoft.com/dotnet/aspnet:5.016:26:56|INFO|callhttp:https://registry.cn-hangzhou.aliyuncs.com/v2/useproxy:127.0.0.1:108016:26:56|INFO|Start:[childprogress]settinguptopushlayers16:26:57|INFO|callhttp:https://mcr.microsoft.com/v2/dotnet/aspnet/blobs/sha256:ab15cbada1ec431e6222fbf5544eee38f1ac0bc0ada4b37831a25ac991a6e522useproxy:127.0.0.1:108016:27:00|INFO|callhttp:https://registry.cn-hangzhou.aliyuncs.com/v2/yuzd/testimagepush/blobs/sha256:9127a318a8536208e7041347951bfd4f95257db52ee748e1d586cda0f029380b16:27:00|INFO|callhttp:https://registry.cn-hangzhou.aliyuncs.com/v2/yuzd/testimagepush/blobs/uploads/16:27:00|INFO|callhttp:https://registry.cn-hangzhou.aliyuncs.com/v2/yuzd/testimagepush/blobs/uploads/fd39d922-c22a-4871-b439-13a6d7c67eff?_state=3gR0LUGRZV1wpHAxrlWtg9CZt8rBXnShN4B-EYxD0g57Ik5hbWUiOiJ5dXpkL3Rlc3RpbWFnZXB1c2giLCJVVUlEIjoiZmQzOWQ5MjItYzIyYS00ODcxLWI0MzktMTNhNmQ3YzY3ZWZmIiwiT2Zmc2V0IjowLCJTdGFydGVkQXQiOiIyMDIxLTA0LTExVDA4OjI3OjAzLjA1MTA3NTgzMloifQ%3D%3D16:27:00|INFO|Start:pushingblobsha256:9127a318a8536208e7041347951bfd4f95257db52ee748e1d586cda0f029380b[1052129]16:27:00|INFO|Start:pullcontainerconfigurationsha256:ab15cbada1ec431e6222fbf5544eee38f1ac0bc0ada4b37831a25ac991a6e52216:27:00|INFO|callhttp:https://registry.cn-hangzhou.aliyuncs.com/v2/yuzd/testimagepush/blobs/sha256:ff2f4c814a04afba84f03a2fd077348a964bd525c4e253e3fedb23d035f9c45a16:27:00|INFO|End:pushingblobsha256:f7ec5a41d630a33a2d1db59b95d89d93de7ae5a619a3a8571b78457e48266eba[27139373]16:27:00|INFO|callhttp:https://registry.cn-hangzhou.aliyuncs.com/v2/yuzd/testimagepush/blobs/uploads/fd39d922-c22a-4871-b439-13a6d7c67eff?_state=LSGtNCpNE7aaQCoZDLvXL6DJRn1PN85OFbLaPPFouRZ7Ik5hbWUiOiJ5dXpkL3Rlc3RpbWFnZXB1c2giLCJVVUlEIjoiZmQzOWQ5MjItYzIyYS00ODcxLWI0MzktMTNhNmQ3YzY3ZWZmIiwiT2Zmc2V0IjoxMDUyMTI5LCJTdGFydGVkQXQiOiIyMDIxLTA0LTExVDA4OjI3OjAzWiJ9&digest=sha256:9127a318a8536208e7041347951bfd4f95257db52ee748e1d586cda0f029380b16:27:01|INFO|End:pushingblobsha256:ff2f4c814a04afba84f03a2fd077348a964bd525c4e253e3fedb23d035f9c45a[2719]16:27:01|INFO|callhttp:https://registry.cn-hangzhou.aliyuncs.com/v2/yuzd/testimagepush/blobs/uploads/dc2051f5-7d7b-4622-9979-3f028fc124df?_state=ZSRwEW96U7Pu7blVT_rsRjY_UsBe02Mbjer7jCkkPz17Ik5hbWUiOiJ5dXpkL3Rlc3RpbWFnZXB1c2giLCJVVUlEIjoiZGMyMDUxZjUtN2Q3Yi00NjIyLTk5NzktM2YwMjhmYzEyNGRmIiwiT2Zmc2V0IjoyNzE5LCJTdGFydGVkQXQiOiIyMDIxLTA0LTExVDA4OjI3OjAzWiJ9&digest=sha256:ff2f4c814a04afba84f03a2fd077348a964bd525c4e253e3fedb23d035f9c45a16:27:01|INFO|End:settinguptopushlayers16:27:01|INFO|End:taggingwith1.0.316:27:03|INFO|globalpullimagecachefolder==>file://%LOCALAPPDATA%/fibdotnet#link3616:27:03|INFO|localbuildimagecachefolder==>file://C:\Users\Administrator\source\repos\testimagepush\testimagepush\bin\Release\dockerImage\dockerImage_cache#link3716:27:03|INFO|publishtarget==>file://C:\Users\Administrator\source\repos\testimagepush\testimagepush\bin\Release\dockerImage\publish#link3816:27:03|INFO|-----------------DeployEnd-----------------16:27:03|INFO|【Deploylog】==>file://C:\Users\Administrator\source\repos\testimagepush\testimagepush\bin\Release\dockerImage\publish_deploy_logs\20210411162703.log#link40

发布成功后检查阿里云镜像仓库是否成功推送

镜像已有,下面在服务器上使用该镜像

先在服务器上拉取镜像:

docker pull registry.cn-hangzhou.aliyuncs.com/yuzd/testimagepush:1.0.3

然后基于该镜像跑一个容器

docker run -p 5000:80 --name testimagepush -d ff2

其他注意事项

插件下载地址:

https://marketplace.visualstudio.com/items?itemName=nainaigu.AntDeployVsix

项目开源地址:

https://github.com/yuzd

dockerhub镜像仓库作为基础镜像构建并上传到dockerhub镜像仓库

  • 考虑到网络环境你需要配置拉取和推送的代理

如果你基础镜像用的是阿里云或者是腾讯云的话,是需要配置账户名和密码的。

  • 固定密码是在开通容器镜像服务时候可以配置的

基础镜像会在本地缓存吗?

  • 会的。在日志里面的最后几行有记录

  • global pull image cache folder 代表的是拉取镜像缓存,如果你删除的话会重新拉

  • local build image cache folder 代表的是制作本地制作镜像的分层缓存

还有其他问题可以提issue或加群反馈


我是正东,学的越多不知道也越多。这个公众号是我的实验小天地,我会分享一些我开源的工具(欢迎你来提意见),好玩好用的新技术。如果你也和我一样喜欢折腾技术请关注 !

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章