牛刀小试之docker容器系列(五)

牛刀小试之docker镜像相关配置

牛刀小试之docker容器系列(五)

牛刀小试之docker镜像相关配置

获取镜像

[root@docker ~]# docker pull ubuntu
Pulling repository ubuntu
6d4946999d4f: Pulling dependent layers
6d4946999d4f: Download complete
428b411c28f0: Download complete
435050075b3f: Download complete
9fd3c8c9af32: Download complete
Status: Downloaded newer image for ubuntu:latest
[root@docker ~]#

可以使用如下方法下载一个特定的镜像

[root@docker ~]# docker pull ubuntu:12.10

如上命令相当于docker pull registry.hub.docker.com/ubuntu:latest.是从docker默认的服务器下载latest镜像

也可以从dockerpool地方下载镜像

下载完成之后就可以使用镜像:

[root@docker ~]# docker run -t -i ubuntu /bin/bash
root@b89ee17998ec:/#
root@b89ee17998ec:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@b89ee17998ec:/# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:02
          inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
          UP BROADCAST RUNNING  MTU:1500  Metric:1
          RX packets:3 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:238 (238.0 B)  TX bytes:238 (238.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

root@b89ee17998ec:/#

查看镜像:

[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED                  VIRTUAL SIZE
ubuntu              trusty-20150612     6d4946999d4f        Less than a second ago   188.3 MB
ubuntu              14.04               6d4946999d4f        Less than a second ago   188.3 MB
ubuntu              14.04.2             6d4946999d4f        Less than a second ago   188.3 MB
ubuntu              latest              6d4946999d4f        Less than a second ago   188.3 MB
ubuntu              trusty              6d4946999d4f        Less than a second ago   188.3 MB
registry            latest              a90be6e49f98        3 months ago             405.2 MB
centos              centos7             acc1b23376ec        3 months ago             224 MB
centos              latest              acc1b23376ec        3 months ago             224 MB
<none>              <none>              3039b31f25fe        3 months ago             222 MB
ubuntu              12.04               28a945b4333c        3 months ago             130.3 MB
ubuntu              12.04.5             28a945b4333c        3 months ago             130.3 MB
<none>              <none>              04c5d3b7b065        3 months ago             192.7 MB
jpetazzo/nsenter    latest              6ed3da1d7fa6        4 months ago             367.7 MB
training/webapp     latest              31fa814ba25a        10 months ago            278.6 MB
[root@docker ~]#

#参数分析

REPOSITORY 来自哪个仓库
TAG  标签
 IMAGE ID  镜像的ID 这个是唯一的
CREATED  镜像的创建时间
VIRTUAL SIZE 镜像的大小

#注意点:

TAG 的很多镜像 其实IMAGE ID是一样的,为什么尼?
是因为他们都是同时指向一个镜像文件

查看镜像的详细信息

[root@docker ~]# docker inspect 6d4946999d4f
[{
    "Architecture": "amd64",
    "Author": "",
    "Comment": "",
    "Config": {
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "/bin/bash"
        ],
        "CpuShares": 0,
        "Cpuset": "",
        "Domainname": "",
        "Entrypoint": null,
        "Env": null,
        "ExposedPorts": null,
        "Hostname": "d3659c5e113e",
        "Image": "9fd3c8c9af32dddb1793ccb5f6535e12d735eacae16f8f8c4214f42f33fe3d29",
        "Memory": 0,
        "MemorySwap": 0,
        "NetworkDisabled": false,
        "OnBuild": null,
        "OpenStdin": false,
        "PortSpecs": null,
        "StdinOnce": false,
        "Tty": false,
        "User": "",
        "Volumes": null,
        "WorkingDir": ""
    },
    "Container": "9201d6220b01338011f2f21c28429d2155625625392e6654fe378c2772cc46bd",
    "ContainerConfig": {
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "/bin/sh",
            "-c",
            "#(nop) CMD [\"/bin/bash\"]"
        ],
        "CpuShares": 0,
        "Cpuset": "",
        "Domainname": "",
        "Entrypoint": null,
        "Env": null,
        "ExposedPorts": null,
        "Hostname": "d3659c5e113e",
        "Image": "9fd3c8c9af32dddb1793ccb5f6535e12d735eacae16f8f8c4214f42f33fe3d29",
        "Memory": 0,
        "MemorySwap": 0,
        "NetworkDisabled": false,
        "OnBuild": null,
        "OpenStdin": false,
        "PortSpecs": null,
        "StdinOnce": false,
        "Tty": false,
        "User": "",
        "Volumes": null,
        "WorkingDir": ""
    },
    "Created": "2015-06-12T15:32:30.680894574Z",
    "DockerVersion": "1.6.0",
    "Id": "6d4946999d4fb403f40e151ecbd13cb866da125431eb1df0cdfd4dc72674e3c6",
    "Os": "linux",
    "Parent": "9fd3c8c9af32dddb1793ccb5f6535e12d735eacae16f8f8c4214f42f33fe3d29",
    "Size": 0,
    "VirtualSize": 188310849
}
][root@docker ~]#

查找镜像:

[root@docker ~]# docker search mysql
NAME                  DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                 MySQL is a widely used, open-source relati...   795       [OK]
tutum/mysql           MySQL Server image - listens in port 3306....   123                  [OK]
mysql/mysql-server    Optimized MySQL Server Docker images. Crea...   24                   [OK]
centurylink/mysql     Image containing mysql. Optimized to be li...   23                   [OK]
sameersbn/mysql                                                       13                   [OK]
appcontainers/mysql   CentOS 6.6 based Customizable MySQL 5.5 Co...   3                    [OK]
azukiapp/mysql        Docker image to run MySQL by Azuki - http:...   2                    [OK]
frodenas/mysql        A Docker Image for MySQL                        1                    [OK]
phpmentors/mysql      MySQL server image                              1                    [OK]
marvambass/mysql      MySQL Server based on Ubuntu 14.04              1                    [OK]
[root@docker ~]#

可以看到很多关键字的镜像 包括名字 描述 和镜像的使用欢迎度 是否官方创建,感觉是不是有点像github
--automated=false 只显示自动创建的镜像
--no-trunc=false 输出信息不截断显示
-s, --stars=0 显示指定星级的镜像

删除镜像:

1.查看镜像
[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED                  VIRTUAL SIZE
ubuntu              trusty-20150612     6d4946999d4f        Less than a second ago   188.3 MB
ubuntu              14.04               6d4946999d4f        Less than a second ago   188.3 MB
ubuntu              14.04.2             6d4946999d4f        Less than a second ago   188.3 MB
ubuntu              latest              6d4946999d4f        Less than a second ago   188.3 MB
ubuntu              trusty              6d4946999d4f        Less than a second ago   188.3 MB
registry            latest              a90be6e49f98        3 months ago             405.2 MB
centos              centos7             acc1b23376ec        3 months ago             224 MB
centos              latest              acc1b23376ec        3 months ago             224 MB
<none>              <none>              3039b31f25fe        3 months ago             222 MB
ubuntu              12.04               28a945b4333c        3 months ago             130.3 MB
ubuntu              12.04.5             28a945b4333c        3 months ago             130.3 MB
<none>              <none>              04c5d3b7b065        3 months ago             192.7 MB
jpetazzo/nsenter    latest              6ed3da1d7fa6        4 months ago             367.7 MB
training/webapp     latest              31fa814ba25a        10 months ago            278.6 MB
2.删除镜像
[root@docker ~]# docker rmi 28a945b4333c 
Untagged: ubuntu:12.04
Untagged: ubuntu:12.04.5
[root@docker ~]# 
3.查看镜像 是否删除成功
[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED                  VIRTUAL SIZE
ubuntu              14.04               6d4946999d4f        Less than a second ago   188.3 MB
ubuntu              14.04.2             6d4946999d4f        Less than a second ago   188.3 MB
ubuntu              latest              6d4946999d4f        Less than a second ago   188.3 MB
ubuntu              trusty              6d4946999d4f        Less than a second ago   188.3 MB
ubuntu              trusty-20150612     6d4946999d4f        Less than a second ago   188.3 MB
registry            latest              a90be6e49f98        3 months ago             405.2 MB
centos              centos7             acc1b23376ec        3 months ago             224 MB
centos              latest              acc1b23376ec        3 months ago             224 MB
<none>              <none>              3039b31f25fe        3 months ago             222 MB
<none>              <none>              04c5d3b7b065        3 months ago             192.7 MB
jpetazzo/nsenter    latest              6ed3da1d7fa6        4 months ago             367.7 MB
training/webapp     latest              31fa814ba25a        10 months ago            278.6 MB
[root@docker ~]#
4.查看正在运行的状态
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE                    COMMAND                CREATED              STATUS                          PORTS                     NAMES
8703418b2dec        training/webapp:latest   "echo 'hello world'"   About a minute ago   Exited (0) About a minute ago                             pensive_curie
7f3491707dd4        ubuntu:14.04             "/bin/bash"            17 minutes ago       Exited (0) 17 minutes ago                                 ecstatic_poincare
b89ee17998ec        ubuntu:14.04             "/bin/bash"            18 minutes ago       Exited (0) 17 minutes ago                                 tender_babbage
0aced9db36e2        training/webapp:latest   "python app.py"        3 months ago         Exited (0) 9 weeks ago          0.0.0.0:49153->5000/tcp   web
436efb7b9933        28a945b4333c             "/bin/bash"            3 months ago         Exited (0) 9 weeks ago                                    berserk_thompson
7582b0eaf376        04c5d3b7b065             "/bin/bash"            3 months ago         Exited (0) 3 months ago                                   desperate_feynman

5.使用 -f 可以强制删除,但是需要注意的是,强制删除会造成一些相关遗留的问题。

创建镜像

1.基于已有镜像的方式创建
相关参数
docker commit -a “作者信息” -m ”提交信息“ -p ”提交时暂停容器运行“

操作如下, 需要记住镜像ID
1.1.修改本地镜像文件,创建一个文件夹
[root@docker ~]# docker run -ti ubuntu:14.04 /bin/bash
root@c72ad6c5adcd:/# touch test
root@c72ad6c5adcd:/# exit
exit
[root@docker ~]#
1.2.使用镜像来打包
[root@docker ~]# docker commit -m 'added a new file' -a 'docker Steven' c72ad6c5adcd test
e72d99a75afcda1e8836018ce5ea33ffe8cfc2161f81b82a70748812de56f385
[root@docker ~]#
1.3.查看是否创建成功
[root@docker ~]# docker images test
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
test                latest              e72d99a75afc        About a minute ago   188.3 MB
[root@docker ~]#
docker 

See also