【3】Working with Docker Images

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

$ 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 <[email protected]>
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 <[email protected]>
 ---> 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


你可能感兴趣的:(【3】Working with Docker Images)