本文章主要讲述如何建立镜像并上传到本身的dockerhub中。html
若是尚未安装docker,请参考docker安装git
在dockerhub https://hub.docker.com/上建立账号。github
docker是和github相似的仓库,用于存放本身的镜像。能够建立私有和公开的仓库。私有的仓库免费的有限制,貌似只能建立一个仓库,公开的没有限制。web
拉取一个镜像docker
本例拉取ubuntu镜像。会自动拉取最终版本。json
lgj@lgj-Lenovo-G470:~$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
6abc03819f3e: Pull complete
05731e63f211: Pull complete
0bd67c50d6be: Pull complete
Digest: sha256:f08638ec7ddc90065187e7eabdfac3c96e5ff0f6b2f1762cf31a4f49b53000a5
Status: Downloaded newer image for ubuntu:latestubuntu
查看安装的镜像vim
能够看到ubuntu:latest镜像已经存在bash
lgj@lgj-Lenovo-G470:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE lgj/webdemo 1.0 10e0d3d344d6 3 days ago 504MB <none> <none> 67b2eee0f0b6 3 days ago 504MB <none> <none> 88b1d5b63784 3 days ago 504MB <none> <none> e60203504301 3 days ago 504MB <none> <none> 11f593fd9537 3 days ago 504MB openjdk 8 4a0a42e87cf3 6 days ago 488MB ubuntu latest 7698f282e524 2 weeks ago 69.9MB
运行镜像this
使用docker run运行,并打开bash,出现第二行则表示运行成功。
lgj@lgj-Lenovo-G470:~$ docker run -it --name ubuntu-vim ubuntu:latest /bin/bash
root@d1d7b31bfd46:/#
这里给它安装上vim.
root@d1d7b31bfd46:/# apt update
root@d1d7b31bfd46:/# apt install vim
安装成功后执行vim。能打开则说明安装成功。
退出容器
root@d1d7b31bfd46:/# exit
基于上述容器从新建立镜像,建立的镜像为 lgj/ubuntu:vim
镜像的完整名称是 用户名称/仓库名称:TAG
docker commit d1d7b31bfd46 lgj/ubuntu:vim
lgj@lgj-Lenovo-G470:~$ docker commit d1d7b31bfd46 lgj/ubuntu:vim sha256:3d0ffc87f06d9c2e2ada45da86333a037b84292d7ab5fd2eeee8e6e2f4686ffc lgj@lgj-Lenovo-G470:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE lgj/ubuntu vim 3d0ffc87f06d 8 seconds ago 155MB lgj/webdemo 1.0 10e0d3d344d6 3 days ago 504MB openjdk 8 4a0a42e87cf3 6 days ago 488MB ubuntu latest 7698f282e524 2 weeks ago 69.9MB
还能够对镜像进行重命名
这里重命名为lgjlife/ubuntu:vim。由于要与dockerhub上的仓库对应
lgj@lgj-Lenovo-G470:~$ docker tag lgj/ubuntu:vim lgjlife/ubuntu:vim
建立仓库
这里的仓库是ubuntu。lgjlife是个人dockerhub的用户名。
登陆
使用docker login登陆
lgj@lgj-Lenovo-G470:~$ docker login 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. Username: lgjlife Password: WARNING! Your password will be stored unencrypted in /home/lgj/snap/docker/384/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
上传
注意这里的镜像名称必须和hub中的仓库名称一致,不然将会抛出错误
denied: requested access to the resource is denied
lgj@lgj-Lenovo-G470:~$ docker push lgjlife/ubuntu:vim The push refers to repository [docker.io/lgjlife/ubuntu] 6b4d25968145: Pushing [==========================================> ] 72.93MB/85.24MB 8d267010480f: Pushed 270f934787ed: Pushed 02571d034293: Pushing [============================================> ] 62.72MB/69.86MB
等待上传成功。
上传成功
vim: digest: sha256:b5a9bb84f41f0da2930695acafbae2fe7484bf65c5b0c8461d7cedc3c6075e13 size: 1155
查看dockerhub
仓库lgjlife/ubuntu已经存在tag为vim的镜像
能够docker pull 下载
docker pull lgjlife/ubuntu:vim
完结!!!!!!!!