基于容器制作镜像
1. 语法知识
- Create a new image from a container's changes
- Usage
- docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAGI]]
Name, shorthand | Defaul | Description |
---|---|---|
-author, -a | Author (e.g. "Jjohn Hannibal Smith hannibal@a-teamcom") | |
--change, -o | Apply Dockerfile instruction to the created image | |
-message, -m | Commit message | |
-pause, -p | true | Pause container during commit |
2. 基于容器制作镜像
# 运行b1容器
docker run --name b1 -it busybox
# 基于容器制作镜像,此时没有指明docker镜像的tag均为none
docker commit -p b1
# 给刚创建的镜像指定tag
docker tag f93e983a803e john/httpd:v0.1-1
# 给有tag的镜像再次添加一个tag
docker tag john/httpd:v0.1-1 john/httpd:latest
# docker 删除标签
docker image rm john/httpd:latest
# 运行自定义的镜像
docker run --name t1 -it john/httpd:v0.1-1
# 再次制作一个更完善的镜像(-a 指明作者信息,-c 配置httpd阿帕奇的配置信息,-p 表示暂停状态,b1是制作镜像的容器名称,john/httpd:v0.2是指明镜像及标签)
docker commit -a "john <508110504@qq.com>" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p b1 john/httpd:v0.2
# 运行新自定义的容器
docker run --name t2 john/httpd:v0.2
查看容器状态
通过docker inspect t2
获取容器ip地址,访问容器服务
3. 将制作的自定义镜像发布到dockerhub
# 将制作的自定义镜像发布到dockerhub上面
# 1. 先注册dockerhub账号,创建一个repository
https://cloud.docker.com/repository/create
# 需要确保自己制作的镜像名称和dockerhub中一致
docker commit -a "john <qnyt1993@gmail.com>" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p b1 john1993/httpd:v0.2
# 将自定义的镜像推送到dockerhub上去(再次之前先进行登录)
docker login
docker push john1993/httpd
4. 将镜像推到阿里云容器镜像服务
# 将镜像推到阿里云容器镜像服务
# 地址 https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
# 配置镜像加速器(官方文档)
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://ostfibsg.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
# 1.注册账号,配置镜像加速,创建镜像仓库
# 阿里云的镜像仓库创建完毕,开始将本地的自定义镜像推送到阿里云上
# 1. 先重新打标签
docker tag john1993/httpd:v0.2 registry.cn-beijing.aliyuncs.com/john1993/httpd:v0.2
# 2. 先退出dockerhub,在登录阿里云docker
docker logout
docker login --username=qnytytqn registry.cn-beijing.aliyuncs.com
# 3. 开始推送
docker push registry.cn-beijing.aliyuncs.com/john1993/httpd:[镜像版本号]
docker push registry.cn-beijing.aliyuncs.com/john1993/httpd:v0.2
5. 镜像的导入和导出
# 1. docker 导出(myimages.gz是导出的压缩包,可指定多个镜像)
docker save -o myimages.gz john1993/httpd:v0.2 john/httpd:v0.1-1
# 2. docker的导入(在需要导入镜像的docker服务器中执行)
docker load -i myimages.gz