Docker系列(6) Docker镜像使用

当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。
本节主要包括:
1、管理和使用本地 Docker 主机镜像
2、创建镜像

1. 列出镜像列表

我们可以使用 docker images 来列出本地主机上的镜像。

dd@ubuntu04:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dd/ubuntu           v1                  63cf4e1dfe43        19 minutes ago      73.9MB
ubuntu              latest              1d622ef86b13        13 days ago         73.9MB
ubuntu              18.04               c3c304cb4f22        13 days ago         64.2MB
hello-world         latest              bf756fb1ae65        4 months ago        13.3kB

各个选项说明:

  • REPOSITORY:表示镜像的仓库源
  • TAG:镜像的标签
  • IMAGE ID:镜像ID
  • CREATED:镜像创建时间
  • SIZE:镜像大小

同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如 ubuntu 仓库源里,有 18.04、16.04、15.10、14.04 等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。
所以,我们如果要使用版本为18.04的ubuntu系统镜像来运行容器时,命令如下

dd@ubuntu04:~$ docker run -t -i ubuntu:18.04 /bin/bash
root@d167b30fbf52:/# 

参数说明:

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

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

dd@ubuntu04:~$ docker run -t -i ubuntu:14.04 /bin/bash
Unable to find image 'ubuntu:14.04' locally
14.04: Pulling from library/ubuntu
2e6e20c8e2e6: Pull complete 
30bb187ac3fc: Pull complete 
b7a5bcc4a58a: Pull complete 
Digest: sha256:ffc76f71dd8be8c9e222d420dc96901a07b61616689a44c7b3ef6a10b7213de4
Status: Downloaded newer image for ubuntu:14.04
root@0f076266e3e8:/# 

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

2. 获取一个新的镜像

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

dd@ubuntu04:~$ docker pull ubuntu:13.10
13.10: Pulling from library/ubuntu
Image docker.io/library/ubuntu:13.10 uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
a3ed95caeb02: Pull complete 
0d8710fc57fd: Pull complete 
5037c5cd623d: Pull complete 
83b53423b49f: Pull complete 
e9e8bd3b94ab: Pull complete 
7db00e6b6e5e: Pull complete 
Digest: sha256:403105e61e2d540187da20d837b6a6e92efc3eb4337da9c04c191fb5e28c44dc
Status: Downloaded newer image for ubuntu:13.10
docker.io/library/ubuntu:13.10

下载完成后,我们可以直接使用这个镜像来运行容器。

3. 查找镜像

我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/
我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个 httpd 的镜像来作为我们的 web 服务。我们可以通过 docker search 命令搜索 httpd 来寻找适合我们的镜像。

dd@ubuntu04:~$ docker search httpd
NAME                                    DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
httpd                                   The Apache HTTP Server Project                  2992                [OK]                
centos/httpd-24-centos7                 Platform for running Apache httpd 2.4 or bui…   31                                      
centos/httpd                                                                            29                                      [OK]
arm32v7/httpd                           The Apache HTTP Server Project                  9                                       
polinux/httpd-php                       Apache with PHP in Docker (Supervisor, CentO…   4                                       [OK]
salim1983hoop/httpd24                   Dockerfile running apache config                2                                       [OK]
publici/httpd                           httpd:latest                                    1                                       [OK]
solsson/httpd-openidc                   mod_auth_openidc on official httpd image, ve…   1                                       [OK]
dariko/httpd-rproxy-ldap                Apache httpd reverse proxy with LDAP authent…   1                                       [OK]
lead4good/httpd-fpm                     httpd server which connects via fcgi proxy h…   1                                       [OK]
jonathanheilmann/httpd-alpine-rewrite   httpd:alpine with enabled mod_rewrite           1                                       [OK]
clearlinux/httpd                        httpd HyperText Transfer Protocol (HTTP) ser…   1                                       
itsziget/httpd24                        Extended HTTPD Docker image based on the off…   0                                       [OK]
appertly/httpd                          Customized Apache HTTPD that uses a PHP-FPM …   0                                       [OK]
hypoport/httpd-cgi                      httpd-cgi                                       0                                       [OK]
dockerpinata/httpd                                                                      0                                       
izdock/httpd                            Production ready Apache HTTPD Web Server + m…   0                                       
manasip/httpd                                                                           0                                       
amd64/httpd                             The Apache HTTP Server Project                  0                                       
trollin/httpd                                                                           0                                       
e2eteam/httpd                                                                           0                                       
manageiq/httpd_configmap_generator      Httpd Configmap Generator                       0                                       [OK]
alvistack/httpd                         Docker Image Packaging for Apache               0                                       [OK]
manageiq/httpd                          Container with httpd, built on CentOS for Ma…   0                                       [OK]
interlutions/httpd                      httpd docker image with debian-based config …   0                                       [OK]

参数说明:

  • NAME: 镜像仓库源的名称
  • DESCRIPTION: 镜像的描述
  • OFFICIAL: 是否 docker 官方发布
  • stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
  • AUTOMATED: 自动构建。

4. 拖取镜像

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

dd@ubuntu04:~$ docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
54fec2fa59d0: Pull complete 
8219e18ac429: Pull complete 
3ae1b816f5e1: Pull complete 
a5aa59ad8b5e: Pull complete 
4f6febfae8db: Pull complete 
Digest: sha256:c9e4386ebcdf0583204e7a54d7a827577b5ff98b932c498e9ee603f7050db1c1
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest

下载完成后,我们就可以使用这个镜像了。

dd@ubuntu04:~$ docker run httpd

5. 删除镜像

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

docker rmi hello-world
dd@ubuntu04:~$ docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 831320a55d0c is using its referenced image bf756fb1ae65

dd@ubuntu04:~$ docker rmi -f hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b

TIPS
当有容器使用某镜像时,删除镜像会报错,可以采用强制删除,也可以先删除容器,再删除镜像

6. 创建镜像

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

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

6.1 更新镜像

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

dd@ubuntu04:~$ docker run -t -i ubuntu:15.10 /bin/bash

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

root@4ff460a9b14b:/#  apt-get update

在完成操作之后,输入 exit 命令来退出这个容器。
此时 ID 为 4ff460a9b14b的容器,是按我们的需求更改的容器。我们可以通过命令 docker commit 来提交容器副本。

dd@ubuntu04:~$ docker commit -m="has update" -a="dd" 4ff460a9b14b dd/ubuntu:v2
sha256:5b51178018cd647042d1c1cc80e776511e72a0709ba3e9bae1fe4deb3d4c1438

dd@ubuntu04:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dd/ubuntu           v2                  5b51178018cd        26 seconds ago      137MB
dd/ubuntu           v1                  63cf4e1dfe43        38 minutes ago      73.9MB
ubuntu              latest              1d622ef86b13        13 days ago         73.9MB
ubuntu              18.04               c3c304cb4f22        13 days ago         64.2MB
httpd               latest              b2c2ab6dcf2e        13 days ago         166MB
ubuntu              14.04               6e4f1fe62ff1        4 months ago        197MB
ubuntu              15.10               9b9cb95443b5        3 years ago         137MB
ubuntu              13.10               7f020f7bf345        5 years ago         185MB

参数说明:

  • -m: 提交的描述信息
  • -a: 指定镜像作者
  • 4ff460a9b14b :容器 ID
  • dd/ubuntu:v2: 指定要创建的目标镜像名

使用我们的新镜像 runoob/ubuntu 来启动一个容器

dd@ubuntu04:~$ docker run -t -i dd/ubuntu:v2 /bin/bash
root@067e33a2976a:/# 

6.2 构建镜像

我们使用命令 docker build , 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像(可以先了解,具体内容可以在Dockerfile章节查看)。

在一个空文件夹内,新建一个名字为Dockerfile的文件,Dockerfile文件内容如下:

FROM    centos:6.7
MAINTAINER      Fisher "dd@sudops.com"

RUN     /bin/echo 'root:123456' |chpasswd
RUN     useradd dd
RUN     /bin/echo 'dd: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 命令来构建一个镜像。

dd@ubuntu04:~/test/01$ pwd
/home/dd/test/01

dd@ubuntu04:~/test/01$ cat Dockerfile 
FROM    centos:6.7
MAINTAINER      Fisher "dd@sudops.com"

RUN     /bin/echo 'root:123456' |chpasswd
RUN     useradd dd
RUN     /bin/echo 'dd:123456' |chpasswd
RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE  22
EXPOSE  80
CMD     /usr/sbin/sshd -D

dd@ubuntu04:~/test/01$ docker build -t dd/centos:6.7 .
Sending build context to Docker daemon  2.048kB
Step 1/9 : FROM    centos:6.7
 ---> 9f1de3c6ad53
Step 2/9 : MAINTAINER      Fisher "dd@sudops.com"
 ---> Running in c6121cbebed7
Removing intermediate container c6121cbebed7
 ---> 9f7facb16445
Step 3/9 : RUN     /bin/echo 'root:123456' |chpasswd
 ---> Running in fea5641ca324
Removing intermediate container fea5641ca324
 ---> 055505a38bba
Step 4/9 : RUN     useradd dd
 ---> Running in 3bb0a189fe69
Removing intermediate container 3bb0a189fe69
 ---> 4e27b7ab6a81
Step 5/9 : RUN     /bin/echo 'dd:123456' |chpasswd
 ---> Running in 5a556ca9e268
Removing intermediate container 5a556ca9e268
 ---> 12929abe1dd5
Step 6/9 : RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
 ---> Running in 69347bb267cd
Removing intermediate container 69347bb267cd
 ---> 4b417ab3406c
Step 7/9 : EXPOSE  22
 ---> Running in 85c0beb033a4
Removing intermediate container 85c0beb033a4
 ---> c4c2c4f73325
Step 8/9 : EXPOSE  80
 ---> Running in 932ec279f858
Removing intermediate container 932ec279f858
 ---> e4435dbe2ce8
Step 9/9 : CMD     /usr/sbin/sshd -D
 ---> Running in a29c0a5552a0
Removing intermediate container a29c0a5552a0
 ---> c6519f975a25
Successfully built c6519f975a25
Successfully tagged dd/centos:6.7

参数说明:

  • -t :指定要创建的目标镜像名
  • . :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径

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

REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
dd/centos           6.7                 c6519f975a25        About a minute ago   191MB
dd/ubuntu           v2                  5b51178018cd        21 minutes ago       137MB
dd/ubuntu           v1                  63cf4e1dfe43        59 minutes ago       73.9MB
ubuntu              latest              1d622ef86b13        13 days ago          73.9MB
ubuntu              18.04               c3c304cb4f22        13 days ago          64.2MB
httpd               latest              b2c2ab6dcf2e        13 days ago          166MB
ubuntu              14.04               6e4f1fe62ff1        4 months ago         197MB
centos              6.7                 9f1de3c6ad53        13 months ago        191MB
ubuntu              15.10               9b9cb95443b5        3 years ago          137MB
ubuntu              13.10               7f020f7bf345        5 years ago          185MB

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

dd@ubuntu04:~/test/01$ docker run -t -i dd/centos:6.7  /bin/bash
[root@8f9814a7a172 /]# id dd
uid=500(dd) gid=500(dd) groups=500(dd)
[root@8f9814a7a172 /]# 

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

6.3 设置镜像标签

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

在这里插入代码片

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

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

dd@ubuntu04:~/test/01$ docker tag c6519f975a25 dd/centos:dev

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

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

dd@ubuntu04:~/test/01$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dd/centos           6.7                 c6519f975a25        3 minutes ago       191MB
dd/centos           dev                 c6519f975a25        3 minutes ago       191MB
dd/ubuntu           v2                  5b51178018cd        23 minutes ago      137MB
dd/ubuntu           v1                  63cf4e1dfe43        About an hour ago   73.9MB
ubuntu              latest              1d622ef86b13        13 days ago         73.9MB
ubuntu              18.04               c3c304cb4f22        13 days ago         64.2MB
httpd               latest              b2c2ab6dcf2e        13 days ago         166MB
ubuntu              14.04               6e4f1fe62ff1        4 months ago        197MB
centos              6.7                 9f1de3c6ad53        13 months ago       191MB
ubuntu              15.10               9b9cb95443b5        3 years ago         137MB
ubuntu              13.10               7f020f7bf345        5 years ago         185MB

7. 参考文献

[1] Docker 镜像使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值