共計 3410 個字符,預計需要花費 9 分鐘才能閱讀完成。
這篇文章主要講解了“如何通過 Dockerfile 創(chuàng)建鏡像”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“如何通過 Dockerfile 創(chuàng)建鏡像”吧!
// 列出本地所有容器鏡像
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
training/webapp latest fc77f57ad303 3 weeks ago 280.5 MB
ubuntu 14.04 5e019ab7bf6d 4 weeks ago 180 MB
ubuntu latest 5e019ab7bf6d 4 weeks ago 180 MB
ubuntu 12.04 74fe38d11401 4 weeks ago 209.6 MB
ubuntu precise 74fe38d11401 4 weeks ago 209.6 MB
// 一個鏡像可能有多個 TAG
// 通過 docker run -t -i REPOSITORY:TAG /bin/bash 運行一個容器,譬如 ubuntu:14.04 和 ubuntu:latest 是運行同一個鏡像
// 獲取一個新的容器鏡像
$ docker pull centos
Pulling repository centos
b7de3133ff98: Pulling dependent layers
5cc9e91966f7: Pulling fs layer
511136ea3c5a: Download complete
ef52fb1fe610: Download complete
. . .
Status: Downloaded newer image for centos
$ docker run -t -i centos /bin/bash
bash-4.1#
// 從 Docker Hub 上搜索想要的容器鏡像
$ sudo docker search sinatra
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
training/sinatra Sinatra training image 0 [OK]
marceldegraaf/sinatra Sinatra test app 0
mattwarren/docker-sinatra-demo 0 [OK]
luisbebop/docker-sinatra-hello-world 0 [OK]
bmorearty/handson-sinatra handson-ruby + Sinatra for Hands on with D... 0
subwiz/sinatra 0
bmorearty/sinatra 0
. . .
// 然后可以下載相應的容器鏡像
// 創(chuàng)建自定義的容器鏡像
$ docker run -t -i training/sinatra /bin/bash
root@0b2616b0e5a8:/# gem install json
//- a 標識創(chuàng)建作者
$ docker commit -m Added json gem -a Kate Smith 0b2616b0e5a8 ouruser/sinatra:v2
4f177bd27a9ff0f6dc2a830403925b5360bfe0b93d476f7fc3231110e7f71b1c
// 查看創(chuàng)建的容器鏡像
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
training/sinatra latest 5bc342fa0b91 10 hours ago 446.7 MB
ouruser/sinatra v2 3c59e02ddd1a 10 hours ago 446.7 MB
ouruser/sinatra latest 5db5f8471261 10 hours ago 446.7 MB
// 通過 Dockerfile 創(chuàng)建鏡像
$ mkdir sinatra
$ cd sinatra
$ touch Dockerfile
# This is a comment
FROM ubuntu:14.04
MAINTAINER Kate Smith ksmith@example.com
RUN apt-get update apt-get install -y ruby ruby-dev
RUN gem install sinatra
// 利用 Dockerfile 和 docker build 命令創(chuàng)建一個鏡像
$ docker build -t ouruser/sinatra:v2 .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:14.04
--- e54ca5efa2e9
Step 1 : MAINTAINER Kate Smith ksmith@example.com
--- Using cache
--- 851baf55332b
Step 2 : RUN apt-get update apt-get install -y ruby ruby-dev
--- Running in 3a2558904e9b
Selecting previously unselected package libasan0:amd64.
...
Installing RDoc documentation for rack-protection-1.5.3...
Installing RDoc documentation for sinatra-1.4.5...
--- 97feabe5d2ed
Removing intermediate container 6b81cb6313e5
Successfully built 97feabe5d2ed
// 利用 - t 來識別新鏡像屬于 ouruser,v2 是 TAG
// 給 ouruser/sinatra 鏡像設(shè)置 TAG
$ docker tag 5db5f8471261 ouruser/sinatra:devel
// 查看 ouruser/sinatra 的 TAG
$ docker images ouruser/sinatra
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ouruser/sinatra latest 5db5f8471261 11 hours ago 446.7 MB
ouruser/sinatra devel 5db5f8471261 11 hours ago 446.7 MB
ouruser/sinatra v2 5db5f8471261 11 hours ago 446.7 MB
// 將鏡像 PUSH 到 Docker Hub
$ docker push ouruser/sinatra
The push refers to a repository [ouruser/sinatra] (len: 1)
Sending image list
Pushing repository ouruser/sinatra (3 tags)
. . .
// 從本地刪除鏡像
$ docker rmi training/sinatra
Untagged: training/sinatra:latest
Deleted: 5bc342fa0b91cabf65246837015197eecfa24b2213ed6a51a8974ae250fedd8d
Deleted: ed0fffdcdae5eb2c3a55549857a8be7fc8bc4241fb19ad714364cbfd7a56b22f
Deleted: 5c58979d73ae448df5af1d8142436d81116187a7633082650549c52c3a2418f0
感謝各位的閱讀,以上就是“如何通過 Dockerfile 創(chuàng)建鏡像”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對如何通過 Dockerfile 創(chuàng)建鏡像這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
正文完