Docker 镜像使用

 docker images 来列出本地主机上的镜像。

PS C:\Users\Administrator> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0d120b6ccaa8        3 months ago        215.1 MB
ubuntu              15.10               9b9cb95443b5        4 years ago         137.2 MB
training/webapp     latest              6fae60ef3446        5 years ago         348.8 MB
PS C:\Users\Administrator>

各个选项说明:

  • REPOSITORY:表示镜像的仓库源

  • TAG:镜像的标签

  • IMAGE ID:镜像ID

  • CREATED:镜像创建时间

  • SIZE:镜像大小

同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如 ubuntu 仓库源里,有 15.10、14.04 等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。

所以,我们如果要使用版本为15.10的ubuntu系统镜像来运行容器时,命令如下:

docker run -t -i ubuntu:15.10 /bin/bash

参数说明:

  • -i: 交互式操作。
  • -t: 终端。
  • ubuntu:15.10: 这是指用 ubuntu 15.10 版本镜像为基础来启动容器。
  • /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。

不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像。


获取一个新的镜像

当我们在本地主机上使用一个不存在的镜像时 Docker 就会自动下载这个镜像。如果我们想预先下载这个镜像,我们可以使用 docker pull 命令来下载它。

 

查找镜像


我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/

我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个 httpd 的镜像来作为我们的 web 服务。我们可以通过 docker search 命令搜索 httpd 来寻找适合我们的镜像。

PS C:\Users\Administrator> docker search httpd
NAME                                    DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
httpd                                   The Apache HTTP Server Project                  3263      [OK]
centos/httpd-24-centos7                 Platform for running Apache httpd 2.4 or b...   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, Cen...   4                    [OK]
salim1983hoop/httpd24                   Dockerfile running apache config                2                    [OK]
lead4good/httpd-fpm                     httpd server which connects via fcgi proxy...   1                    [OK]
solsson/httpd-openidc                   mod_auth_openidc on official httpd image, ...   1                    [OK]
hypoport/httpd-cgi                      httpd-cgi                                       1                    [OK]
jonathanheilmann/httpd-alpine-rewrite   httpd:alpine with enabled mod_rewrite           1                    [OK]
publici/httpd                           httpd:latest                                    1                    [OK]
clearlinux/httpd                        httpd HyperText Transfer Protocol (HTTP) s...   1
dariko/httpd-rproxy-ldap                Apache httpd reverse proxy with LDAP authe...   1                    [OK]
interlutions/httpd                      httpd docker image with debian-based confi...   0                    [OK]
dockerpinata/httpd                                                                      0
manasip/httpd                                                                           0
amd64/httpd                             The Apache HTTP Server Project                  0
trollin/httpd                                                                           0
e2eteam/httpd                                                                           0
manageiq/httpd                          Container with httpd, built on CentOS for ...   0                    [OK]
manageiq/httpd_configmap_generator      Httpd Configmap Generator                       0                    [OK]
itsziget/httpd24                        Extended HTTPD Docker image based on the o...   0                    [OK]
ppc64le/httpd                           The Apache HTTP Server Project                  0
appertly/httpd                          Customized Apache HTTPD that uses a PHP-FP...   0                    [OK]
PS C:\Users\Administrator>

NAME: 镜像仓库源的名称

DESCRIPTION: 镜像的描述

OFFICIAL: 是否 docker 官方发布

stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。

AUTOMATED: 自动构建。

 

删除镜像

镜像删除使用 docker rmi 命令,比如我们删除 hello-world 镜像:

$ docker rmi hello-world


创建镜像

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

  • 1、从已经创建的容器中更新镜像,并且提交这个镜像
  • 2、使用 Dockerfile 指令来创建一个新的镜像

更新镜像

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

$ docker run -t -i ubuntu:15.10 /bin/bash

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

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

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

docker commit -m="has update" -a="runoob" e218edb10161 runoob/ubuntu:v2

各个参数说明:

  • -m: 提交的描述信息

  • -a: 指定镜像作者

  • e218edb10161:容器 ID

  • runoob/ubuntu:v2: 指定要创建的目标镜像名

我们可以使用 docker images 命令来查看我们的新镜像 runoob/ubuntu:v2

实践:

PS C:\Users\Administrator> docker commit -a="lm" -m="has a update" 99ab16c2155e centos8/liuming:v2
sha256:32c25f60d6d03407d01910f97137f9664bc588b45d77ce1fedf92adeec0b68d4
PS C:\Users\Administrator> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos8/liuming     v2                  32c25f60d6d0        11 seconds ago      284.4 MB
centos              latest              0d120b6ccaa8        3 months ago        215.1 MB
ubuntu              15.10               9b9cb95443b5        4 years ago         137.2 MB
training/webapp     latest              6fae60ef3446        5 years ago         348.8 MB
PS C:\Users\Administrator>


构建镜像

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

cat Dockerfile 
FROM    centos:6.7
MAINTAINER      Fisher "fisher@sudops.com"

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 命令来构建一个镜像。

$ 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 "fisher@sudops.com"
 ---> Using cache
 ---> 0c92299c6f03
Step 3 : RUN /bin/echo 'root:123456' |chpasswd
 ---> Using cache
 ---> 0397ce2fbd0a
Step 4 : RUN useradd runoob
......

参数说明:

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

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

使用docker images 查看创建的镜像已经在列表中存在

设置镜像标签

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

 docker tag 860c279d2fec runoob/centos:dev
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值