本文章主要講述如何創建鏡像並上傳到自己的dockerhub中。
如果還沒有安裝docker,請參考docker安裝
第一步
在dockerhub https://hub.docker.com/上創建帳號。
docker是和github類似的倉庫,用於存放自己的鏡像。可以創建私有和公開的倉庫。私有的倉庫免費的有限制,貌似只能創建一個倉庫,公開的沒有限制。
第二步
拉取一個鏡像
本例拉取ubuntu鏡像。會自動拉取最終版本。
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:latest
查看安裝的鏡像
可以看到ubuntu:latest鏡像已經存在
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
第三步
運行鏡像
使用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
完結!!!!!!!!