Docker Hub:镜像上传与拉取的完整流程
·
Docker Hub:镜像上传与拉取的完整流程
一、镜像拉取流程(从Docker Hub获取镜像)
-
搜索镜像
在终端使用docker search命令查找公共镜像:docker search nginx -
拉取镜像
使用docker pull下载镜像到本地:docker pull nginx:latest # 拉取最新版Nginx -
验证镜像
检查本地镜像列表:docker images输出示例:
REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest xxxxxxxxxxxx 2 weeks ago 133MB
二、镜像上传流程(推送镜像到Docker Hub)
-
登录Docker Hub
在终端输入账号密码:docker loginUsername: your_username Password: ******** Login Succeeded -
为本地镜像打标签
格式:docker tag <本地镜像> <DockerHub用户名>/<镜像名>:<标签>docker tag my-app:1.0 your_username/custom-app:v1 -
推送镜像
使用docker push上传:docker push your_username/custom-app:v1成功输出:
v1: digest: sha256:xxxxxxxx size: 1234 -
在Docker Hub验证
登录 hub.docker.com → 个人仓库 → 查看custom-app的v1标签。
三、关键注意事项
-
权限控制
- 公共仓库:所有用户可拉取
- 私有仓库:需
docker pull your_username/private-repo前先登录
-
镜像命名规范
- 必须包含Docker Hub用户名:
your_username/repo-name - 标签建议语义化:
v1.2,prod,debug
- 必须包含Docker Hub用户名:
-
存储空间限制
- 免费账户:1个私有仓库 + 无限制公共仓库
- 镜像层优化:使用
.dockerignore减少体积
四、完整操作示例
# 拉取官方Redis镜像
docker pull redis:alpine
# 构建自定义镜像(Dockerfile略)
docker build -t my-redis .
# 打标签并推送
docker tag my-redis your_username/redis-custom:prod
docker push your_username/redis-custom:prod
# 其他机器拉取
docker pull your_username/redis-custom:prod
提示:首次推送可能较慢,建议使用国内镜像加速器(如阿里云、腾讯云镜像服务)。
更多推荐
所有评论(0)