如何通过Dockerfile创建镜像

77次阅读
没有评论

共计 3410 个字符,预计需要花费 9 分钟才能阅读完成。

这篇文章主要讲解了“如何通过 Dockerfile 创建镜像”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着丸趣 TV 小编的思路慢慢深入,一起来研究和学习“如何通过 Dockerfile 创建镜像”吧!

// 列出本地所有容器镜像

$ 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
. . .
// 然后可以下载相应的容器镜像 


// 创建自定义的容器镜像
$ docker run -t -i training/sinatra /bin/bash
root@0b2616b0e5a8:/# gem install json

//- a 标识创建作者
$ docker commit -m Added json gem -a Kate Smith 0b2616b0e5a8 ouruser/sinatra:v2
4f177bd27a9ff0f6dc2a830403925b5360bfe0b93d476f7fc3231110e7f71b1c

// 查看创建的容器镜像
$ 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 创建镜像
$ 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 命令创建一个镜像
$ 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 镜像设置 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 创建镜像”的内容了,经过本文的学习后,相信大家对如何通过 Dockerfile 创建镜像这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是丸趣 TV,丸趣 TV 小编将为大家推送更多相关知识点的文章,欢迎关注!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-16发表,共计3410字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)