前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker自制镜像打包推送

Docker自制镜像打包推送

作者头像
乐心湖
发布2021-01-18 15:02:26
1.6K0
发布2021-01-18 15:02:26
举报
文章被收录于专栏:MyTechnologyMyTechnology

基于 Ubuntu 镜像打包

手动打包

拉取 Ubuntu 系统并启动实例,不指定版本号则默认最新版本,目前为:Ubuntu 20.04.1 LTS

docker run -it ubuntu

容器启动后,会进入 ubuntu 容器内部 Bash 里面。

我们在这个 Ubuntu 中安装一些软件,例如:Node.js AND 写好一个输出 Hello World 的程序,然后再将容器再次打包,打包成一个新的镜像,这就意识着你或其他人下次 可以通过 docker run 这个已经装好 Node.js 的镜像拉取回来并运行成为新容器。

在安装 Node.js 之前,我们顺便把该容器的镜像地址更换为国人使用更快的阿里云。

apt install vim -y
vim /etc/apt/sources.list

gg 跳转到内容首行,然后按 dG 清空文件内容,按 i ,粘贴下面内容

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

更新一下

 sudo apt-get update

在 Ubuntu 中安装 Node.js

apt update && apt install nodejs -y

安装完之后,打开一个新终端,通过 docker ps 可以查看正在运行的容器

输入 docker diff 容器id ,可以看到一个长长的文件列表

  • A代表新增的文件
  • C代表更新的文件
  • D代表移除的文件

刚刚我们所作的操作,就可以通过这个命令来对比 Ubuntu 镜像改动了哪些文件

接下来我们去定义镜像的名称

docker commit 容器id 你的用户名/新镜像名称:版本号

docker commit 5ce lexinhu/ubuntu_node:0.1

通过 docker images 可以看到刚刚打包的镜像

运行我们新打包的镜像

docker run -it lexinhu/ubuntu_node:0.1

进入容器,输入 node -v 查看 nodejs

写一个输出 Hello Wold 程序

# node -v
v10.19.0
# mkdir /app
# cd /app
# pwd
/app
# echo "console.log('Hello World')" > hello-world.js
# ll
total 4
drwxr-xr-x. 2 root root 28 Dec 22 08:54 ./
drwxr-xr-x. 1 root root 29 Dec 22 08:53 ../
-rw-r--r--. 1 root root 27 Dec 22 08:54 hello-world.js
# node hello-world.js 
Hello World

退出容器,此时容器终止,原因是我们启动容器的时候没有用守护进程的方式,通过docker ps -a 找到它,通过 docker diff 容器id 查看文件修改

重新打包

docker commit df3 lexinhu/ubuntu_node:0.2

尝试运行

docker run lexinhu/ubuntu_node:0.2 node /app/hello-world.js

以上就是一步步手动通过 docker commit 打包的镜像

自动打包(主要)

主要是通过 Dockerfile 文件生成

在桌面创建 ubuntu_node 文件夹,写一个 hello-world.js

依旧在此文件夹,创建 Dockerfile 文件

FROM ubuntu

RUN apt update && apt install nodejs -y

WORKDIR /app

COPY . .

CMD ["node","/app/hello-world.js"]
  • FROM ubuntu 以 ubuntu 镜像为基础
  • RUN apt update && apt install nodejs -y 执行指令
  • WORKDIR /app 定义工作目录
  • COPY . . 将当前文件夹的文件复制到工作目录
  • CMD ["node","/app/hello-world.js"] 容器运行时执行的命令

由于我本机 win10 没有安装 docker,我将它上传到 Linux 上打包

进入 Dockerfile 所在的目录

docker build -t lexinhu/ubuntu_node:0.3 .

Docker 会根据 Dockerfile 的设定去建立新的镜像。

docker run lexinhu/ubuntu_node:0.3

推送镜像

这里我选择使用阿里云的镜像服务

登录,会提示输入用户名密码。

sudo docker login registry.cn-shanghai.aliyuncs.com

推送

sudo docker tag [ImageId] registry.cn-shanghai.aliyuncs.com/xn2001/study_xn2001:[镜像版本号]
sudo docker push registry.cn-shanghai.aliyuncs.com/xn2001/study_xn2001:[镜像版本号]

示例:

sudo docker tag d86fdef8356e registry.cn-shanghai.aliyuncs.com/lexinhu/ubuntu_node:0.3
sudo docker push registry.cn-shanghai.aliyuncs.com/lexinhu/ubuntu_node:0.3

拉取

sudo docker pull registry.cn-shanghai.aliyuncs.com/lexinhu/ubuntu_node:0.3

版权属于:乐心湖's Blog

本文链接:https://cloud.tencent.com/developer/article/1774970

声明:博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 手动打包
  • 自动打包(主要)
  • 推送镜像
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档