若是對Docker還不是很瞭解的同窗能夠先看一下我以前的文章Docker基礎教程(1)java
容器和鏡像之間是能夠互相轉變的,鏡像能夠運行成容器,容器也能夠打包成鏡像,能夠將全部數據打包起來,原封不動的變成一個鏡像spring
首先了解一下打包的命令docker commit docker commit :從容器建立一個新的鏡像。docker
咱們利用docker commit
命令 來嘗試打包一個鏡像shell
[root@iZbp1cwfb0i1aften1twtvZ ~]# docker commit -a "zhai" -m "mytestcentos" mycentos mytestcentos:1.0
sha256:f336e3692c24837df716f1cb9379e29672f6baa5126df5e6d2ee4dda296beb22
複製代碼
輸入docker images
查看已經打包好的鏡像centos
[root@iZbp1cwfb0i1aften1twtvZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytestcentos 1.0 f336e3692c24 7 seconds ago 220 MB
複製代碼
這樣就把一個正在運行的容器打包成鏡像拉bash
Dockerfile是一個包含用於組合映像的命令的文本文檔。可使用在命令行中調用任何命令。 Docker經過讀取Dockerfile中的指令自動生成鏡像。微信
首先熟悉一下Dockerfile中的幾個關鍵字 包括FROM、MAINTAINER、RUN、CMD、EXPOSE、ENV、ADD、COPY、ENTRYPOINT、VOLUME、USER、WORKDIR、ONBUILD等13個指令,我會選一些經常使用的關鍵字逐個解析網絡
ENV word hello
RUN echo $word
複製代碼
相同點:app
不一樣點:測試
注: Docker容器須要一直掛載一個命令,因此做爲ENTRYPOINT或者CMD或者command都須要一直保持在前臺運行,不然容器一啓動就會馬上退出
接下來咱們動手寫一個本身的Dockerfile
首先找到一個文件夾建立一個叫Dockerfile的文件
[root@iZbp1cwfb0i1aften1twtvZ /]# cd /tmp/centos/
[root@iZbp1cwfb0i1aften1twtvZ centos]# ls
test.txt
[root@iZbp1cwfb0i1aften1twtvZ centos]# touch Dockerfile
複製代碼
能夠看到這個文件夾還有咱們上次測試留下的test.txt文件,咱們就先不扔了,能夠測試一下添加文件的效果.
接下來咱們在Dockerfile中加入如下內容
FROM centos #根鏡像爲centos
ADD test.txt /tmp/ # 添加test.txt文件進入tmp文件夾
ADD http://images.xiaoazhai.com/qiaoben.jpg /tmp/qiaoben.jpg# 添加網絡資源
ENV profile=test #指定環境變量
RUN echo mytest > /tmp/test.txt #測試RUN命令
RUN echo $profile # 測試環境變量是否成功
ENTRYPOINT top # 執行一個一直在前臺運行的命令
EXPOSE 8999# 暴露端口
複製代碼
保存並退出 運行命令docker build -t mycentos:1.0 .
來打包成鏡像,該命令中-t 指定打包出來的鏡像名稱 "."表示Dockerfile所在的文件夾
[root@iZbp1cwfb0i1aften1twtvZ centos]# docker build -t mycentos:1.0 .
Sending build context to Docker daemon 3.072 kB
Step 1/8 : FROM centos
---> 0f3e07c0138f
Step 2/8 : ADD test.txt /tmp/
---> e81cb238f74a
Removing intermediate container 90009ab9814e
Step 3/8 : ADD http://images.xiaoazhai.com/qiaoben.jpg /tmp/qiaoben.jpg
Downloading [==================================================>] 14.46 kB/14.46 kB
---> 0d4678989698
Removing intermediate container ea16c9215ed4
Step 4/8 : ENV profile test
---> Running in ec23237b9c5c
---> c6b43c68a91e
Removing intermediate container ec23237b9c5c
Step 5/8 : RUN echo mytest > /tmp/test.txt
---> Running in 6f276d45a876
---> 9a4094e0cc21
Removing intermediate container 6f276d45a876
Step 6/8 : RUN echo $profile
---> Running in 14c1b307c7bc
test
---> 4bb58acd03d4
Removing intermediate container 14c1b307c7bc
Step 7/8 : ENTRYPOINT top
---> Running in 528c78987148
---> 245601488490
Removing intermediate container 528c78987148
Step 8/8 : EXPOSE 8999
---> Running in 72433b0025e1
---> 238c588a72cf
Removing intermediate container 72433b0025e1
Successfully built 238c588a72cf
複製代碼
運行docker images
查看本身的鏡像
root@iZbp1cwfb0i1aften1twtvZ centos]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.0 238c588a72cf 5 minutes ago 220 MB
複製代碼
這裏我就不跑起來了,有興趣的同窗能夠自行跑通鏡像,查看結果.
首先須要註冊一個DockerHub的帳號點此註冊
註冊好之後運行docker login
輸入帳號密碼登錄
[root@iZbp1cwfb0i1aften1twtvZ centos]# 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: yourname Password: Login Succeeded 複製代碼
接下來給咱們的鏡像改個名字 改爲你的id+名稱 運行docker tag docker tag mycentos:1.0 yourname/mycentos:1.0
[root@iZbp1cwfb0i1aften1twtvZ centos]# docker tag mycentos:1.0 602122993/mycentos:1.0
[root@iZbp1cwfb0i1aften1twtvZ centos]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
602122993/mycentos 1.0 238c588a72cf 16 minutes ago 220 MB
mycentos 1.0 238c588a72cf 16 minutes ago 220 MB
複製代碼
運行docker push 602122993/mycentos:1.0
將鏡像推送到咱們的dockerhub倉庫(推送可能比較慢)
[root@iZbp1cwfb0i1aften1twtvZ centos]# docker push 602122993/mycentos:1.0
The push refers to a repository [docker.io/602122993/mycentos]
62bea31d0469: Pushed
3120fc785846: Pushed
8e2a2d912053: Pushed
9e607bb861a7: Mounted from library/centos
1.0: digest: sha256:58dd5bb4570b17e1730ee0ef3d1e2f88f7b84c114bb719dd580b2230abe2312b size: 1152
複製代碼
接下來咱們刪除本地的鏡像而且拉取線上的鏡像
[root@iZbp1cwfb0i1aften1twtvZ centos]# docker rmi mycentos:1.0
Untagged: mycentos:1.0
Untagged: 602122993/mycentos@sha256:58dd5bb4570b17e1730ee0ef3d1e2f88f7b84c114bb719dd580b2230abe2312b
[root@iZbp1cwfb0i1aften1twtvZ centos]# docker rmi 602122993/mycentos:1.0
Untagged: 602122993/mycentos:1.0
Deleted: sha256:238c588a72cf697c57aa39c670e5317d7fd732f7d18090933f8b747f746e3ebd
Deleted: sha256:245601488490345ca2c112f4f6e07b7e213da5029db5eba7f0a926c0a78e2d15
Deleted: sha256:4bb58acd03d44324bebea0b0ba56201b630a5eb59465b5ec668b433408cc3ef0
Deleted: sha256:9a4094e0cc217b1470f43d5b111c39e2a620b044a8122e819a34db226e6fb296
Deleted: sha256:5acf5ffaf5d260d38ccd8a4d97f601bd410ccb5ede7dc6f99ba6d587bed72be2
Deleted: sha256:c6b43c68a91e71295c3dc0b9fd4d9e439be36f8d25ddf0e73d5e7ab96ab41a3b
Deleted: sha256:0d4678989698e022d0001d53d608c98edccb401ea9c6fb7452116438cb03483e
Deleted: sha256:cd5d444402cdbfd1cab3e723353f066bb31b369712006eb9ec5e0fc0a711f97b
Deleted: sha256:e81cb238f74acdf9d31fc5e7945c64d0557b7fd2fb871bd9cf385c5d84d39f2b
Deleted: sha256:74e061523d05623c8021d35edd58c1e2484daed9b50252720f2a26fe91e3ed71
[root@iZbp1cwfb0i1aften1twtvZ centos]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 0f3e07c0138f 2 weeks ago 220 MB
[root@iZbp1cwfb0i1aften1twtvZ centos]# docker pull 602122993/mycentos:1.0
Trying to pull repository docker.io/602122993/mycentos ...
1.0: Pulling from docker.io/602122993/mycentos
729ec3a6ada3: Already exists
3d9879b8dfbb: Pull complete
f4c8ced9c934: Pull complete
4a9350bbacbe: Pull complete
Digest: sha256:58dd5bb4570b17e1730ee0ef3d1e2f88f7b84c114bb719dd580b2230abe2312b
Status: Downloaded newer image for docker.io/602122993/mycentos:1.0
複製代碼
這樣咱們就將咱們本身建立的鏡像上傳到dockerhub上面啦~
感謝閱讀
有興趣能夠關注個人我的微信公衆號,會按期推送關於Java的技術文章,並且目前不恰飯都是乾貨哈哈哈哈