当容器运行时,使用的镜像如果在本地中不存在,Docker 就会自动从 Docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像资源下载。
列出镜像列表
我们可以使用 docker images 来列出本地主机上的所有镜像
[root@lavm-5wutyiasaf ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v2 3e987db64805 26 hours ago 77.9MB
test/ubuntu v1 906851c5bf30 26 hours ago 77.9MB
ubuntu 22.04 b103ac8bf22e 13 days ago 77.9MB
ubuntu latest bf16bdcff9c9 2 weeks ago 78.1MB
nginx latest 1e5f3c5b981a 8 weeks ago 192MB
mysql 8.0 355bbda86f66 8 weeks ago 772MB
hello-world latest 74cc54e27dc4 4 months ago 10.1kB
dko0/lsky-pro latest c89f58b471b2 2 years ago 838MB
centos latest 5d0da3dc9764 3 years ago 231MB
参数说明:
REPOSITORY
:表示镜像的仓库源TAG
:镜像的标签IMAGE ID
:镜像 IDCREATED
:镜像创建时间SIZE
:镜像大小
关于标签
同一仓库源可以有多个 TAG,代表这个仓库源的不同版本,如 ubuntu 的仓库源里,有 22.04、有 15.10,我们使用 REPOSITORY:TAG
来定义不同的镜像。
使用指定版本的镜像,例如 ubuntu 15.10
[root@lavm-5wutyiasaf ~]# docker run -it --name 15.10ubuntu ubuntu:15.10 /bin/bash
Unable to find image 'ubuntu:15.10' locally
15.10: Pulling from library/ubuntu
7dcf5a444392: Pull complete
759aa75f3cee: Pull complete
3fa871dc8a2b: Pull complete
224c42ae46e7: Pull complete
Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3
Status: Downloaded newer image for ubuntu:15.10
root@355aaeb6bcfa:/#
参数说明
-i
:交互式操作-t
:终端--name 15.10ubuntu
:容器命名为 15.10ubuntuubuntu:15.10
:指定 ubuntu 15.10 版本镜像为基础来运行容器/bin/bash
:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash
获取新的镜像
当我们在本地主机上使用一个不存在的镜像时,Docker 就会自动下载这个镜像。如果想预先下载的话可以使用 docker pull
命令来下载。
[root@lavm-5wutyiasaf ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
dad67da3f26b: Already exists
d0a755bf09a1: Pull complete
4f4fb700ef54: Pull complete
be5c5a616c3a: Pull complete
d1042d58e186: Pull complete
c06cec1379c2: Pull complete
Digest: sha256:f6557a77ee2f16c50a5ccbb2564a3fd56087da311bf69a160d43f73b23d3af2d
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
查找镜像
可以从 Docker Hub 官网上搜索我们想找的镜像
如图所示
删除镜像
镜像删除使用 docker rmi 命令,比如
[root@lavm-5wutyiasaf ~]# docker rmi test/ubuntu:v2
Untagged: test/ubuntu:v2
Deleted: sha256:3e987db64805f8dc339715d3ab86fbb5292b20a6d86d54bf36aab37110581035
创建镜像
当我们从 docker 镜像仓库中下载的镜像已经无法满足我们的需求时,我们可以通过以下两种方式对镜像进项更改。
- 1、 从已经创建的容器中更新镜像,并且提交这个镜像。
- 2、使用 Dockerfile 指令来创建一个新的镜像。
更新镜像
更新镜像之前,我们需要使用镜像来创建一个容器:
[root@lavm-5wutyiasaf ~]# docker run -i -t --name gpsubuntu ubuntu:22.04 /bin/bash
root@b5725b3366a8:/# apt-get update
root@b5725b3366a8:/# apt-get upgrade -y
root@b5725b3366a8:/# exit
完成操作之后我们可以用 docker commit 来提交容器副本:
[root@lavm-5wutyiasaf ~]# docker commit -m="update ubuntu" -a="root" b5725b3366a8 root/ubuntu:v3
sha256:c6f36ed0bdfe94da595738b153ef554e544786e59645d04b054dd0fbbb96a8dc
参数说明:
-m
:提交的描述信息-a
:指定镜像作者b5725b3366a8
:容器 IDroot/ubuntu:v3
:指定要创建的目标镜像名
构建镜像
我们使用命令 docker build,从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfle 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。
[root@lavm-5wutyiasaf ~]# mkdir -p /my-docker-images
[root@lavm-5wutyiasaf ~]# cd /my-docker-images/
[root@lavm-5wutyiasaf my-docker-images]# ls
[root@lavm-5wutyiasaf my-docker-images]# vim Dockerfile
[root@lavm-5wutyiasaf my-docker-images]# cat Dockerfile
FROM centos:6.7
MAINTAINER Fisher "fisher@sudops.com"
RUN /bin/echo 'root:123456' | chpasswd
RUN useradd sean
RUN /bin/echo 'sean:123456' | chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local
EXPOSE 22
EXPOSE 80
CMD /usr/sbin/sshd -D
[root@lavm-5wutyiasaf my-docker-images]#
Dockerfile 参数说明:
FROM centos:6.7
:指定基础镜像(且必须是第一条指令)MAINTAINER Fisher
:标注镜像维护者信息。RUN
:RUN 指令告诉 Docker 在镜像内执行命令。EXPOSE
:声明(但不强制开放)容器需要开放的端口,供运行时用-p
或--expose
做映射。CMD
:指定容器启动后的默认命令。
使用 docker build 命令构建镜像:
[root@lavm-5wutyiasaf my-docker-images]# docker build -t sean/centos:latest .
[+] Building 1.4s (9/9) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 297B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/5] FROM docker.io/library/centos:latest 0.0s
=> [2/5] RUN /bin/echo 'root:123456' | chpasswd 0.3s
=> [3/5] RUN useradd sean 0.3s
=> [4/5] RUN /bin/echo 'sean:123456' | chpasswd 0.2s
=> [5/5] RUN /bin/echo -e "LANG="en_US.UTF-8"" > /etc/default/local 0.3s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:e9e69b3c803280a0ee538532fbbe96a89f79f7358466011659a4c4240f7a1d05 0.0s
=> => naming to docker.io/sean/centos:latest
参数说明:
-t
:指定要创建的目标镜像.
:Dockerfile 文件所在目录,可以指定绝对路径,也可以用.
来表示当前路径
设置镜像标签
我们可以使用 docker tag 命令,为镜像添加一个新的标签:
镜像名和标签自定义修改标签:
语法:
docker tag [OPTIONS] SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
参数说明:
[OPTIONS] SOURCE_IMAGE[:TAG]
: 你想打标签的原始镜像(可以用镜像名或镜像ID)。
TARGET_IMAGE[:TAG]
: 你想赋予的新镜像名和标签。
[:TAG]
:标签是可选的,不指定默认是 latest。
# 使用镜像名和标签自定义修改标签
[root@lavm-5wutyiasaf ~]# docker tag sean/centos:latest seann/centos:6.7
[root@lavm-5wutyiasaf ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sean/centos latest e9e69b3c8032 25 hours ago 232MB
seann/centos 6.7 e9e69b3c8032 25 hours ago 232MB
# 使用镜像ID自定义修改标签
[root@lavm-5wutyiasaf ~]# docker tag e9e69b3c8032 my/centos:6.8
[root@lavm-5wutyiasaf ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my/centos 6.8 e9e69b3c8032 25 hours ago 232MB
sean/centos latest e9e69b3c8032 25 hours ago 232MB
seann/centos 6.7 e9e69b3c8032 25 hours ago 232MB