镜像使用

Docker 镜像使用

列出镜像列表

[root@web1 ~]# docker images

REPOSITORY          TAG                IMAGE ID            CREATED            SIZE

test/ubuntu        v1                  74bee83de2c9        3 weeks ago        72.9MB

ubuntu              latest              d70eaf7277ea        3 weeks ago        72.9MB

training/webapp    latest              6fae60ef3446        5 years ago        349MB

[root@web1 ~]# docker run -t -i test/ubuntu:v1 /bin/bash

root@0f4c95bf5d6f:/#

参数说明:

-i: 交互式操作。

-t: 终端。

ubuntu:15.10: 这是指用 ubuntu 15.10 版本镜像为基础来启动容器。

/bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。

获取一个新的镜像

[root@web1 ~]# docker pull ubuntu:14.04

14.04: Pulling from library/ubuntu

2e6e20c8e2e6: Pull complete

95201152d9ff: Pull complete

5f63a3b65493: Pull complete

Digest: sha256:63fce984528cec8714c365919882f8fb64c8a3edf23fdfa0b218a2756125456f

Status: Downloaded newer image for ubuntu:14.04

docker.io/library/ubuntu:14.04

查找镜像

[root@web1 ~]# docker search httpd

NAME                                    DESCRIPTION                                    STARS              OFFICIAL            AUTOMATED

httpd                                  The Apache HTTP Server Project                  3255                [OK]               

centos/httpd-24-centos7                Platform for running Apache httpd 2.4 or bui…  36                                     

centos/httpd                                                                            33                                      [OK]

arm32v7/httpd                          The Apache HTTP Server Project                  9                                     

arm64v8/httpd                          The Apache HTTP Server Project                  6                                     

polinux/httpd-php                      Apache with PHP in Docker (Supervisor, CentO…  4                                      [OK]

salim1983hoop/httpd24                  Dockerfile running apache config                2                                      [OK]

jonathanheilmann/httpd-alpine-rewrite  httpd:alpine with enabled mod_rewrite          1                                      [OK]

拖取镜像

我们决定使用上图中的 httpd 官方版本的镜像,使用命令 docker pull 来下载镜像

[root@web1 ~]# docker pull httpd

Using default tag: latest

latest: Pulling from library/httpd

852e50cd189d: Pull complete

67d51c33d390: Pull complete

b0ad2a3b9567: Pull complete

136f1f71f30c: Pull complete

01f8ace29294: Pull complete

Digest: sha256:fddc534b7f6bb6197855be559244adb11907d569aae1283db8e6ce8bb8f6f456

Status: Downloaded newer image for httpd:latest

docker.io/library/httpd:latest

删除镜像

如果有容器在使用这个镜像,则会报错,必须先停止并删除容器后才可以删除镜像

[root@web1 ~]# docker rmi test/ubuntu

Error: No such image: test/ubuntu

[root@web1 ~]# docker rmi test/ubuntu:v1

Error response from daemon: conflict: unable to remove repository reference "test/ubuntu:v1" (must force) - container 0f4c95bf5d6f is using its referenced image 74bee83de2c9

[root@web1 ~]# docker ps -a

CONTAINER ID        IMAGE              COMMAND            CREATED            STATUS                        PORTS                    NAMES

0f4c95bf5d6f        test/ubuntu:v1      "/bin/bash"        18 minutes ago      Exited (127) 10 minutes ago                            heuristic_murdock

ccc5f08a6edf        training/webapp    "python app.py"    22 hours ago        Up 22 hours                  0.0.0.0:32768->5000/tcp  eager_joliot

f3519be18778        ubuntu              "/bin/bash"        3 weeks ago        Up 3 weeks                                              ubuntu-test

[root@web1 ~]# docker rm 0f4c95bf5d6f

0f4c95bf5d6f

[root@web1 ~]# docker ps -a

CONTAINER ID        IMAGE              COMMAND            CREATED            STATUS              PORTS                    NAMES

ccc5f08a6edf        training/webapp    "python app.py"    22 hours ago        Up 22 hours        0.0.0.0:32768->5000/tcp  eager_joliot

f3519be18778        ubuntu              "/bin/bash"        3 weeks ago        Up 3 weeks                                    ubuntu-test

[root@web1 ~]# docker rmi test/ubuntu:v1

Untagged: test/ubuntu:v1

Deleted: sha256:74bee83de2c94d6f7f70c71d6d9abc2a85257423b5b25ea02ba62bca094d913c

Deleted: sha256:b40e6b9446a191d9a8902775516265e5edb1aa56ffa9f26f7a4514c823ede5de

[root@web1 ~]# docker images

REPOSITORY          TAG                IMAGE ID            CREATED            SIZE

httpd              latest              0a30f4c29d25        22 hours ago        138MB

ubuntu              latest              d70eaf7277ea        3 weeks ago        72.9MB

ubuntu              14.04              df043b4f0cf1        2 months ago        197MB

training/webapp    latest              6fae60ef3446        5 years ago        349MB

创建镜像

创建镜像

当我们从 docker 镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。

1、从已经创建的容器中更新镜像,并且提交这个镜像

2、使用 Dockerfile 指令来创建一个新的镜像

更新镜像

更新镜像之前,我们需要使用镜像来创建一个容器。

runoob@ru[root@web1 ~]# docker run -i -t ubuntu:14.04 /bin/bash

root@03e7ed62dc2d:/# apt-get update

在运行的容器内使用 apt-get update 命令进行更新。

在完成操作之后,输入 exit 命令来退出这个容器。

此时 ID 为 e218edb10161 的容器,是按我们的需求更改的容器。我们可以通过命令 docker commit 来提交容器副本。

构建镜像

我们使用命令 docker build , 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像

我们使用命令 docker build , 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。

runoob@runoob:~$ cat Dockerfile

FROM    centos:6.7

MAINTAINER      Fisher "[email protected]"

RUN    /bin/echo 'root:123456' |chpasswd

RUN    useradd runoob

RUN    /bin/echo 'runoob:123456' |chpasswd

RUN    /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local

EXPOSE  22

EXPOSE  80

CMD    /usr/sbin/sshd -D

每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的。

第一条FROM,指定使用哪个镜像源

RUN 指令告诉docker 在镜像内执行命令,安装了什么。。。

然后,我们使用 Dockerfile 文件,通过 docker build 命令来构建一个镜像。

runoob@runoob:~$ docker build -t runoob/centos:6.7 .

Sending build context to Docker daemon 17.92 kB

Step 1 : FROM centos:6.7

---> d95b5ca17cc3

Step 2 : MAINTAINER Fisher "[email protected]"

---> Using cache

---> 0c92299c6f03

Step 3 : RUN /bin/echo 'root:123456' |chpasswd

---> Using cache

---> 0397ce2fbd0a

Step 4 : RUN useradd runoob

......

参数说明:

-t :指定要创建的目标镜像名

. :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径

使用docker images 查看创建的镜像已经在列表中存在,镜像ID为860c279d2fec

runoob@runoob:~$ docker images

REPOSITORY          TAG                IMAGE ID            CREATED              SIZE

runoob/centos      6.7                860c279d2fec        About a minute ago  190.6 MB

runoob/ubuntu      v2                  70bf1840fd7c        17 hours ago        158.5 MB

ubuntu              14.04              90d5884b1ee0        6 days ago          188 MB

php                5.6                f40e9e0f10c8        10 days ago          444.8 MB

nginx              latest              6f8d099c3adc        12 days ago          182.7 MB

mysql              5.6                f2e8d6c772c0        3 weeks ago          324.6 MB

httpd              latest              02ef73cf1bc0        3 weeks ago          194.4 MB

ubuntu              15.10              4e3b13c8a266        5 weeks ago          136.3 MB

hello-world        latest              690ed74de00f        6 months ago        960 B

centos              6.7                d95b5ca17cc3        6 months ago        190.6 MB

training/webapp    latest              6fae60ef3446        12 months ago        348.8 MB

我们可以使用新的镜像来创建容器

runoob@runoob:~$ docker run -t -i runoob/centos:6.7  /bin/bash

[root@41c28d18b5fb /]# id runoob

uid=500(runoob) gid=500(runoob) groups=500(runoob)

从上面看到新镜像已经包含我们创建的用户 runoob。

设置镜像标签

我们可以使用 docker tag 命令,为镜像添加一个新的标签。

runoob@runoob:~$ docker tag 860c279d2fec runoob/centos:dev

docker tag 镜像ID,这里是 860c279d2fec ,用户名称、镜像源名(repository name)和新的标签名(tag)。

使用 docker images 命令可以看到,ID为860c279d2fec的镜像多一个标签。

runoob@runoob:~$ docker images

REPOSITORY          TAG                IMAGE ID            CREATED            SIZE

runoob/centos      6.7                860c279d2fec        5 hours ago        190.6 MB

runoob/centos      dev                860c279d2fec        5 hours ago        190.6 MB

runoob/ubuntu      v2                  70bf1840fd7c        22 hours ago        158.5 MB

ubuntu              14.04              90d5884b1ee0        6 days ago          188 MB

php                5.6                f40e9e0f10c8        10 days ago        444.8 MB

nginx              latest              6f8d099c3adc        13 days ago        182.7 MB

mysql              5.6                f2e8d6c772c0        3 weeks ago        324.6 MB

httpd              latest              02ef73cf1bc0        3 weeks ago        194.4 MB

ubuntu              15.10              4e3b13c8a266        5 weeks ago        136.3 MB

hello-world        latest              690ed74de00f        6 months ago        960 B

centos              6.7                d95b5ca17cc3        6 months ago        190.6 MB

training/webapp    latest              6fae60ef3446        12 months ago      348.8 MB

你可能感兴趣的:(镜像使用)