在上一篇文章中,介绍了使用docker快速搭建nginx+php环境,本文介绍如何把修改过的镜像上传至镜像仓库。我们需要先在 Docker Hub上注册一个账号。

  • 查看镜像
  1. [root@localhost ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx latest f09fe80eb0e7 7 days ago 109MB
  4. php 7.1-fpm 7c5ccac5d47f 7 days ago 358MB
  • 查看容器
  1. [root@localhost ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 51adb2df6004 php:7.1-fpm "docker-php-entrypoif" 6 hours ago Up 6 hours 0.0.0.0:9000->9000/tcp myphp-fpm
  4. 3218b3ad4e47 f09fe80eb0e7 "nginx -g 'daemon off" 7 hours ago Up 7 hours 0.0.0.0:80->80/tcp mynginx

从容器创建一个新的镜像

  1. [root@localhost ~]# docker commit -p -a "codehui" -m "test" 3218b3ad4e47 codehi/nginx:v1
  2. sha256:1d8fca63675ac57bccb50f0dbdb6030e384c22ee673eecdcb04c915236778109

-a :提交的镜像作者;
-m :提交时的说明文字;
-p :在commit时,将容器暂停。

再次查看镜像,发现了本地新提交的codehi/nginx:v1的镜像

  1. [root@localhost ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. codehi/nginx v1 1d8fca63675a 21 minutes ago 109MB
  4. nginx latest f09fe80eb0e7 7 days ago 109MB
  5. php 7.1-fpm 7c5ccac5d47f 7 days ago 358MB
  • 然后登陆到 docker hub
  1. [root@localhost ~]# docker login
  2. Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
  3. Username: codehi
  4. Password:
  5. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  6. Configure a credential helper to remove this warning. See
  7. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  8. Login Succeeded
  • codehi/nginx:v1镜像推送到docker hub仓库中
  1. [root@localhost ~]# docker push codehi/nginx:v1
  2. The push refers to repository [docker.io/codehi/nginx]
  3. 67e805da8eae: Pushed
  4. 6b5e2ed60418: Mounted from library/nginx
  5. 92c15149e23b: Mounted from library/nginx
  6. 0a07e81f5da3: Mounted from library/nginx
  7. v1: digest: sha256:729b836319d3f8bd501db5e7bfadc31d1a88f996b0e756f6fb6cd6abd4408962 size: 1156

登陆仓库后台查看上传成功

然后就可以在另一台服务器使用pull方法下载这个镜像

  1. [root@localhost ~]# docker pull codehi/nginx:v1
  2. v1: Pulling from codehi/nginx
  3. 6ae821421a7d: Already exists
  4. da4474e5966c: Already exists
  5. eb2aec2b9c9f: Already exists
  6. 96e30fa1efc1: Pull complete
  7. Digest: sha256:422535f95685c43406e378b2c59a3a6564362a683ceac4b783b9d5cee347ae17
  8. Status: Downloaded newer image for codehi/nginx:v1

push 时报错 “denied: requested access to the resource is denied” 的解决方法

报这个错说明tag需要改名字,由于之前commit的时候没有填tag,导致这块上传不上去,解决办法:

  1. [root@localhost ~]# docker tag 1d8fca63675a codehi/nginx:v1