基于 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 ,可以看到一个长长的文件列表
刚刚我们所作的操作,就可以通过这个命令来对比 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 协议 ,转载请注明出处!