当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。
下面我们来学习:
- 1、管理和使用本地 Docker 主机镜像
- 2、创建镜像
列出镜像列表
我们可以使用 docker images 来列出本地主机上的镜像。
[root@test ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test/redis-test v2 22486b144c33 10 seconds ago 94.8MB test/redis-test v1 e1bd6ab53cd8 About an hour ago 94.8MB redis latest dcf9ec9265e0 3 weeks ago 98.2MB nginx latest 231d40e811cd 3 weeks ago 126MB ansible 2.0 38a4a7650067 3 months ago 481MB celery_env 2.0 1f642bd32d94 3 months ago 2.01GB zabbix/zabbix-agent centos-4.0-latest c5302a10cad2 3 months ago 228MB mysql 5.7 e1e1680ac726 4 months ago 373MB zabbix/zabbix-web-apache-mysql centos-4.0-latest 587c9f53bbbd 4 months ago 474MB zabbix/zabbix-server-mysql centos-4.0-latest 7fbda7c3fa2e 4 months ago 333MB training/webapp latest 6fae60ef3446 4 years ago 349MB [root@test ~]#
各个选项说明:
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如 test/redis-test仓库源里,有 v2、v1 等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。
所以,我们如果要使用版本为v1的est/redis-test系统镜像来运行容器时,命令如下:
[root@test ~]# docker run -t -i test/redis-test:v1 /bin/bash root@8d74da7e60a0:/# ls bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@8d74da7e60a0:/#
参数说明:
- -i: 交互式操作。
- -t: 终端。
- test/redis-test:v1: 这是指用 test/redis-test v1 版本镜像为基础来启动容器。
- /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。
如果要使用版本为 v2的 test/redis-test系统镜像来运行容器时,命令如下:
[root@test ~]# docker run -t -i test/redis-test:v2 /bin/bash root@23d9efad1ae1:/# ls bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@23d9efad1ae1:/# root@23d9efad1ae1:/#
如果你不指定一个镜像的版本标签,例如你只使用 test/redis-test,docker 将默认使用 test/redis-test:latest 镜像。
获取一个新的镜像
当我们在本地主机上使用一个不存在的镜像时 Docker 就会自动下载这个镜像。如果我们想预先下载这个镜像,我们可以使用 docker pull 命令来下载它。
[root@test ~]# docker pull httpd Using default tag: latest latest: Pulling from library/httpd 000eee12ec04: Already exists 32b8712d1f38: Pull complete f1ca037d6393: Pull complete c4bd3401259f: Pull complete 51c60bde4d46: Pull complete Digest: sha256:ac6594daaa934c4c6ba66c562e96f2fb12f871415a9b7117724c52687080d35d Status: Downloaded newer image for httpd:latest [root@test ~]#
下载完成后,我们可以直接使用这个镜像来运行容器。
查找镜像
我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/
例如:
我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个 httpd 的镜像来作为我们的 web 服务。我们可以通过 docker search 命令搜索 httpd 来寻找适合我们的镜像。
[root@test ~]# docker search httpd NAME DESCRIPTION STARS OFFICIAL AUTOMATED httpd The Apache HTTP Server Project 2781 [OK] centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 27 centos/httpd 26 [OK] armhf/httpd The Apache HTTP Server Project 8 salim1983hoop/httpd24 Dockerfile running apache config 2 [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] manasip/httpd 0 dockerpinata/httpd 0 itsziget/httpd24 Extended HTTPD Docker image based on the off… 0 [OK] interlutions/httpd httpd docker image with debian-based config … 0 [OK] manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK] appertly/httpd Customized Apache HTTPD that uses a PHP-FPM … 0 [OK] izdock/httpd Production ready Apache HTTPD Web Server + m… 0 trollin/httpd 0 amd64/httpd The Apache HTTP Server Project 0 publici/httpd httpd:latest 0 [OK] e2eteam/httpd 0 buzzardev/httpd Based on the official httpd image 0 [OK] hypoport/httpd-cgi httpd-cgi 0 [OK] manageiq/httpd Container with httpd, built on CentOS for Ma… 0 [OK] ppc64le/httpd The Apache HTTP Server Project 0 alvistack/httpd Docker Image Packaging for Apache 0 [OK] tugboatqa/httpd The Apache HTTP Server Project 0 [root@test ~]#
NAME: 镜像仓库源的名称
DESCRIPTION: 镜像的描述
OFFICIAL: 是否 docker 官方发布
stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
AUTOMATED: 自动构建。
拖取镜像
我们决定使用上图中的 httpd 官方版本的镜像,使用命令 docker pull 来下载镜像。
[root@test ~]# docker pull httpd Using default tag: latest latest: Pulling from library/httpd 000eee12ec04: Already exists 32b8712d1f38: Pull complete f1ca037d6393: Pull complete c4bd3401259f: Pull complete 51c60bde4d46: Pull complete Digest: sha256:ac6594daaa934c4c6ba66c562e96f2fb12f871415a9b7117724c52687080d35d Status: Downloaded newer image for httpd:latest
下载完成后,我们就可以使用这个镜像了。
[root@test ~]# docker run httpd [Thu Dec 19 06:38:37.165568 2019] [mpm_event:notice] [pid 1:tid 140487557534848] AH00489: Apache/2.4.41 (Unix) configured -- resuming normal operations [Thu Dec 19 06:38:37.165926 2019] [core:notice] [pid 1:tid 140487557534848] AH00094: Command line: 'httpd -D FOREGROUND'
删除镜像
镜像删除使用 docker rmi 命令,比如我们删除 httpd 镜像:
[root@test ~]# docker rmi httpd --有容器在引用,无法删除,需要先删掉容器 Error response from daemon: conflict: unable to remove repository reference "httpd" (must force) - container 91c3146a4e2d is using its referenced image 2ae34abc2ed0 [root@test ~]# [root@test ~]# [root@test ~]# [root@test ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e75e6fd17a7 training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp ecstatic_jennings cf27a69ff0d3 dcf9ec9265e0 "docker-entrypoint.s…" 4 hours ago Up 4 hours 6379/tcp heuristic_brattain 3aeb30c31b99 1f642bd32d94 "/bin/bash" 27 hours ago Up 27 hours lucid_heyrovsky 660644cb03d8 e1e1680ac726 "docker-entrypoint.s…" 27 hours ago Up 27 hours 3306/tcp, 33060/tcp friendly_mccarthy 49194cec6480 38a4a7650067 "tail -f /dev/null /…" 28 hours ago Up 28 hours jolly_tereshkova 5c0a4fb8dc05 231d40e811cd "/bin/bash" 28 hours ago Up 28 hours 80/tcp cocky_colden [root@test ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 91c3146a4e2d httpd "httpd-foreground" 4 minutes ago Exited (0) 2 minutes ago hungry_hypatia 23d9efad1ae1 test/redis-test:v2 "/bin/bash" 3 hours ago Exited (127) 13 minutes ago nifty_wright 8d74da7e60a0 test/redis-test:v1 "/bin/bash" 3 hours ago Exited (0) 3 hours ago agitated_knuth 8e75e6fd17a7 training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp ecstatic_jennings cf27a69ff0d3 dcf9ec9265e0 "docker-entrypoint.s…" 4 hours ago Up 4 hours 6379/tcp heuristic_brattain ae31ed600854 1f642bd32d94 "/bin/sh -c 'while t…" 5 hours ago Exited (137) 5 hours ago silly_shaw 50046987514f 1f642bd32d94 "/bin/bash" 5 hours ago Exited (0) 5 hours ago focused_lewin 3aeb30c31b99 1f642bd32d94 "/bin/bash" 27 hours ago Up 27 hours lucid_heyrovsky 326fcb6c6b17 1f642bd32d94 "/bin/echo 'Hello wo…" 27 hours ago Exited (0) 27 hours ago elated_panini 660644cb03d8 e1e1680ac726 "docker-entrypoint.s…" 27 hours ago Up 27 hours 3306/tcp, 33060/tcp friendly_mccarthy 3e391a12359d 1f642bd32d94 "/bin/bash" 28 hours ago Exited (137) 28 hours ago agitated_kalam 49194cec6480 38a4a7650067 "tail -f /dev/null /…" 28 hours ago Up 28 hours jolly_tereshkova 5c0a4fb8dc05 231d40e811cd "/bin/bash" 28 hours ago Up 28 hours 80/tcp cocky_colden [root@test ~]# [root@test ~]# docker rm 91c3146a4e2d 91c3146a4e2d [root@test ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 23d9efad1ae1 test/redis-test:v2 "/bin/bash" 3 hours ago Exited (127) 14 minutes ago nifty_wright 8d74da7e60a0 test/redis-test:v1 "/bin/bash" 3 hours ago Exited (0) 3 hours ago agitated_knuth 8e75e6fd17a7 training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp ecstatic_jennings cf27a69ff0d3 dcf9ec9265e0 "docker-entrypoint.s…" 4 hours ago Up 4 hours 6379/tcp heuristic_brattain ae31ed600854 1f642bd32d94 "/bin/sh -c 'while t…" 5 hours ago Exited (137) 5 hours ago silly_shaw 50046987514f 1f642bd32d94 "/bin/bash" 5 hours ago Exited (0) 5 hours ago focused_lewin 3aeb30c31b99 1f642bd32d94 "/bin/bash" 27 hours ago Up 27 hours lucid_heyrovsky 326fcb6c6b17 1f642bd32d94 "/bin/echo 'Hello wo…" 27 hours ago Exited (0) 27 hours ago elated_panini 660644cb03d8 e1e1680ac726 "docker-entrypoint.s…" 27 hours ago Up 27 hours 3306/tcp, 33060/tcp friendly_mccarthy 3e391a12359d 1f642bd32d94 "/bin/bash" 28 hours ago Exited (137) 28 hours ago agitated_kalam 49194cec6480 38a4a7650067 "tail -f /dev/null /…" 28 hours ago Up 28 hours jolly_tereshkova 5c0a4fb8dc05 231d40e811cd "/bin/bash" 28 hours ago Up 28 hours 80/tcp cocky_colden [root@test ~]# docker rmi httpd Untagged: httpd:latest Untagged: httpd@sha256:ac6594daaa934c4c6ba66c562e96f2fb12f871415a9b7117724c52687080d35d Deleted: sha256:2ae34abc2ed0a22e280d17e13f9c01aaf725688b09b7a1525d1a2750e2c0d1de Deleted: sha256:f142129afcde2a59210763515cd67ca09a90e62801a6f4af0ad9087c0a3c8782 Deleted: sha256:c8473253c52755c60810dc6ee62c4ae0856ab140fce263af29aec650e3bac7dc Deleted: sha256:96f1a9551370bb171a4c3fe161728a776e84e8e732ebdc3196b649544574ef8f Deleted: sha256:4abaca4f9812f8ac32380c94fe5b61f7111c028601161e1216de935ce61d2434 [root@test ~]# [root@test ~]#
创建镜像
当我们从 docker 镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。
- 1、从已经创建的容器中更新镜像,并且提交这个镜像
- 2、使用 Dockerfile 指令来创建一个新的镜像
更新镜像
更新镜像之前,我们需要使用镜像来创建一个容器。
[root@test ~]# docker run -t -i test/redis-test:v2 /bin/bash root@5a0acd8e9776:/# apt-get update Get:2 http://deb.debian.org/debian buster InRelease [122 kB] Get:3 http://deb.debian.org/debian buster-updates InRelease [49.3 kB] Get:1 http://security-cdn.debian.org/debian-security buster/updates InRelease [65.4 kB] Get:4 http://deb.debian.org/debian buster/main amd64 Packages [7908 kB] Get:5 http://security-cdn.debian.org/debian-security buster/updates/main amd64 Packages [164 kB] Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [5792 B] Fetched 8313 kB in 9s (945 kB/s) Reading package lists... Done root@5a0acd8e9776:/# root@5a0acd8e9776:/# exit exit [root@test ~]#
在运行的容器内使用 apt-get update 命令进行更新。
在完成操作之后,输入 exit 命令来退出这个容器。
此时 ID 为 5a0acd8e9776的容器,是按我们的需求更改的容器。我们可以通过命令 docker commit 来提交容器副本。
[root@test ~]# docker commit -m="has update" -a="zcy" 5a0acd8e9776 zcy/test/redis-test:v3 sha256:b40eb1d2f69656b4004a65d99f67cd1507235f69d7aee7aad63118800ccb5dd3 [root@test ~]#
各个参数说明:
-m: 提交的描述信息
-a: 指定镜像作者
5a0acd8e9776:容器 ID
zcy/test/redis-test:v3: 指定要创建的目标镜像名
我们可以使用 docker images 命令来查看我们的新镜像 zcy/test/redis-test:v3:
[root@test ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zcy/test/redis-test v3 b40eb1d2f696 6 seconds ago 112MB test/redis-test v2 22486b144c33 3 hours ago 94.8MB test/redis-test v1 e1bd6ab53cd8 4 hours ago 94.8MB redis latest dcf9ec9265e0 3 weeks ago 98.2MB nginx latest 231d40e811cd 3 weeks ago 126MB ansible 2.0 38a4a7650067 3 months ago 481MB celery_env 2.0 1f642bd32d94 3 months ago 2.01GB zabbix/zabbix-agent centos-4.0-latest c5302a10cad2 3 months ago 228MB mysql 5.7 e1e1680ac726 4 months ago 373MB zabbix/zabbix-web-apache-mysql centos-4.0-latest 587c9f53bbbd 4 months ago 474MB zabbix/zabbix-server-mysql centos-4.0-latest 7fbda7c3fa2e 4 months ago 333MB training/webapp latest 6fae60ef3446 4 years ago 349MB [root@test ~]#
使用我们的新镜像 zcy/test/redis-test:v3来启动一个容器
[root@test ~]# docker run -t -i zcy/test/redis-test:v3 /bin/bash root@a66729748621:/# ls bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@a66729748621:/#
构建镜像
我们使用命令 docker build , 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。
[root@test ~]# vim Dockerfile FROM celery_env:2.0 MAINTAINER Zcy "zcy@sudops.com" RUN /bin/echo 'root:123456' |chpasswd RUN useradd test RUN /bin/echo 'test: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 命令来构建一个镜像。
[root@test ~]# docker build -t zcy/celery_env:3.0 . Sending build context to Docker daemon 6.395GB Step 1/9 : FROM celery_env:2.0 ---> 1f642bd32d94 Step 2/9 : MAINTAINER Zcy "zcy@sudops.com" ---> Running in a19623f34f63 Removing intermediate container a19623f34f63 ---> eaca7bbc3c12 Step 3/9 : RUN /bin/echo 'root:123456' |chpasswd ---> Running in f41c78408ec9 Removing intermediate container f41c78408ec9 ---> 07693520cc21 Step 4/9 : RUN useradd test ---> Running in 49a0ec5c2fe2 Removing intermediate container 49a0ec5c2fe2 ---> 2b3ab6893eac Step 5/9 : RUN /bin/echo 'test:123456' |chpasswd ---> Running in 14117a88fdd6 Removing intermediate container 14117a88fdd6 ---> 1e156b2acd6f Step 6/9 : RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local ---> Running in bb934d8e63f5 Removing intermediate container bb934d8e63f5 ---> 37d186fd113b Step 7/9 : EXPOSE 22 ---> Running in 28af735940de Removing intermediate container 28af735940de ---> 651f67c73bd1 Step 8/9 : EXPOSE 80 ---> Running in 20574bb3cac3 Removing intermediate container 20574bb3cac3 ---> 910c011dc503 Step 9/9 : CMD /usr/sbin/sshd -D ---> Running in 6b685131712d Removing intermediate container 6b685131712d ---> 069698b23c60 Successfully built 069698b23c60 Successfully tagged zcy/celery_env:3.0 [root@test ~]# [root@test ~]#
参数说明:
-t :指定要创建的目标镜像名
. :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
使用docker images 查看创建的镜像已经在列表中存在,镜像ID为069698b23c60
[root@test ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zcy/celery_env 3.0 069698b23c60 2 minutes ago 2.01GB zcy/test/redis-test v3 b40eb1d2f696 13 minutes ago 112MB test/redis-test v2 22486b144c33 3 hours ago 94.8MB test/redis-test v1 e1bd6ab53cd8 4 hours ago 94.8MB redis latest dcf9ec9265e0 3 weeks ago 98.2MB nginx latest 231d40e811cd 3 weeks ago 126MB ansible 2.0 38a4a7650067 3 months ago 481MB celery_env 2.0 1f642bd32d94 3 months ago 2.01GB zabbix/zabbix-agent centos-4.0-latest c5302a10cad2 3 months ago 228MB mysql 5.7 e1e1680ac726 4 months ago 373MB zabbix/zabbix-web-apache-mysql centos-4.0-latest 587c9f53bbbd 4 months ago 474MB zabbix/zabbix-server-mysql centos-4.0-latest 7fbda7c3fa2e 4 months ago 333MB training/webapp latest 6fae60ef3446 4 years ago 349MB [root@test ~]#
我们可以使用新的镜像来创建容器
[root@test ~]# docker run -t -i zcy/celery_env:3.0 /bin/bash bash-4.2# id test uid=1000(test) gid=1000(test) groups=1000(test) bash-4.2#
从上面看到新镜像已经包含我们创建的用户 test。
设置镜像标签
我们可以使用 docker tag 命令,为镜像添加一个新的标签。
[root@test ~]# docker tag 069698b23c60 zcy/celery_env:dev
docker tag 镜像ID,这里是 069698b23c60,用户名称、镜像源名(repository name)和新的标签名(tag)。
使用 docker images 命令可以看到,ID为069698b23c60的镜像多一个标签。
[root@test ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zcy/celery_env 3.0 069698b23c60 35 minutes ago 2.01GB zcy/celery_env dev 069698b23c60 35 minutes ago 2.01GB zcy/test/redis-test v3 b40eb1d2f696 About an hour ago 112MB test/redis-test v2 22486b144c33 4 hours ago 94.8MB test/redis-test v1 e1bd6ab53cd8 5 hours ago 94.8MB redis latest dcf9ec9265e0 3 weeks ago 98.2MB nginx latest 231d40e811cd 3 weeks ago 126MB ansible 2.0 38a4a7650067 3 months ago 481MB celery_env 2.0 1f642bd32d94 3 months ago 2.01GB zabbix/zabbix-agent centos-4.0-latest c5302a10cad2 3 months ago 228MB mysql 5.7 e1e1680ac726 4 months ago 373MB zabbix/zabbix-web-apache-mysql centos-4.0-latest 587c9f53bbbd 4 months ago 474MB zabbix/zabbix-server-mysql centos-4.0-latest 7fbda7c3fa2e 4 months ago 333MB training/webapp latest 6fae60ef3446 4 years ago 349MB [root@test ~]#
Docker学习笔记08:docker使用之镜像使用
最新推荐文章于 2023-09-22 14:27:17 发布