1. 拉取镜像
假设从 Docker Hub 拉取一个官方的 Ubuntu 20.04 镜像,执行命令:
docker pull ubuntu:20.04
2. 基于镜像运行容器
拉取镜像后,基于该镜像运行一个容器:
docker run -it --name my_container ubuntu:20.04 /bin/bash
- -it:表示以交互模式运行容器并分配一个伪终端。
- –name my_container:给容器起名为my_container。
- ubuntu:20.04:指定使用刚拉取的镜像。
- /bin/bash:容器启动后执行的命令,这里进入一个bash shell。
3. 在容器中安装软件包
在容器内部进行操作,比如安装curl软件包:
apt-get update && apt-get install -y curl
4. 退出容器
安装完成后,退出容器:
exit
5. 提交容器为新镜像
将修改后的容器提交为新镜像:
docker commit my_container_id my_custom_ubuntu:v1
- my_container_id:容器id
- my_custom_ubuntu:v1:新的镜像名称:tag
6. 查看新镜像
查看所有镜像,确认新镜像已经生成:
docker images
7. 保存新镜像为文件(可选)
可以将新镜像保存为一个tar文件:
docker save -o my_custom_ubuntu.tar my_custom_ubuntu:v1
8. 加载镜像(如果是从文件加载)
如果在其他环境需要使用该镜像,可以从保存的文件中加载:
docker load -i my_custom_ubuntu.tar
9. 推送镜像到私有库(如果需要)
假设要推送到一个私有库,先登录私有库:
docker login your_private_registry_ip:port(输入用户名和密码)
给镜像打标签:
docker tag my_custom_ubuntu:v1 your_private_registry_ip:port/my_custom_ubuntu:v1
推送镜像:
docker push your_private_registry_ip:port/my_custom_ubuntu:v1
或者
docker push your_private_registry_host/my_custom_ubuntu:v1
比如:docker push dockeropen.x/openjdk8-chromedriver:v1.3