1 创建镜像
创建Docker镜像主要有以下四种方式:
- 从已有的镜像创建镜像:使用docker pull命令从Docker Hub或者其他的镜像仓库拉取镜像,拉取完成会创建一个新的镜像
- 使用Dockerfile创建镜像:可以使用docker build命令根据Dockerfile创建一个新的镜像
- 从容器中创建镜像:使用docker commit命令从一个容器中创建一个新的镜像
- 使用镜像文件创建镜像:使用docker import命令从本地镜像文件或者远程镜像文件url地址创建一个镜像
1.1 拉取镜像,从已有镜像创建镜像
使用docker pull命令拉取一个镜像,以拉取ubuntu最新的docker镜像为例,命令为
docker pull ubuntu
或者
docker pull ubuntu:latest
我们还可以拉取指定tag的镜像,比如
docker pull ubuntu:22.04
1.2 从Dockerfile中构建一个新的镜像
先在当前目录下创建一个Dockerfile文件,然后在文件中加入以下内容
# 指定基础镜像
FROM ubuntu
# 镜像维护者/作者
MAINTAINER stubbornhuang
# 环境变量
ENV MYPATH /usr/local
# 工作路径,容器创建后,终端默认进入的工作目录,或者理解为落脚点
WORKDIR $MYPATH
# 构成镜像时,执行的命令
# 从Dockerfile到images的过程中会执行的命令
RUN apt-get update
RUN apt-get install -y vim
RUN apt-get install -y net-tools
RUN apt-get install -y iputils-ping
# 将宿主机的文件复制到镜像中
ADD asdf.txt /usr/local/
# 暴露端口
EXPOSE 80
# 指定容器在启动后要进行的操作
CMD /bin/bash
然后就可以使用这个Dockerfile文件构建一个基于ubuntu镜像的定制化镜像,使用以下命令
docker build -f  Dockerfile -t my_ubuntu .
其中:
- -f:指定Dockerfile文件
- -t:指定要创建的目标镜像名
- .:Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
命令执行完成之后,就可以使用docker images查看镜像列表中是否已经创建了名为my_ubuntu的镜像。
1.3 从容器中创建镜像
比如我们先拉取一个ubuntu镜像
docker pull ubuntu
然后使用以下命令基于ubuntu镜像运行一个容器
docker run -i -t --name=my_ubuntu ubuntu /bin/bash
其中:
- -i:表示交互式操作
- -t:表示终端
- --name:设置容器名字,此时容器名称为my_ubuntu
- ubuntu:表示使用ubuntu镜像为基础运行一个容器
- /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash
执行完上述命令后,我们就跳转到了dokcer的终端,如下
[root@ai-0008 ~]# docker run -i -t ubuntu /bin/bash
root@e218edb10161:/# 
上面root@后面的字符就是我们创建的容器ID,这里的是e218edb10161,在运行的容器内部运行以下命令,对容器进行修改
apt-get update
apt-get install nano
之后输入
exit
退出当前容器,回到系统的命令行终端,然后使用docker commit命令将此容器创建一个新的镜像
docker commit -m="apt-get update" -a="stubbornhuang" e218edb10161 stubbornhuang/ubuntu:v1
其中:
- -m:提交镜像的描述信息
- -a:提交镜像的作者信息
- e218edb10161:容器ID
- stubbornhuang/ubuntu:v1:所创建镜像的名称
执行完命令之后,就创建一个名为stubborn/ubuntu,tag为v1的镜像。
1.4 使用镜像文件创建镜像
比如假设将一个镜像导出名为my_ubuntu.tar的文件,我们可以使用以下命令根据该镜像文件创建一个新的镜像
 docker import  my_ubuntu.tar my_ubuntu:v2
2 列出本机已有的镜像列表
使用
docker images
列出本机已有Docker镜像列表。
[root@ai-0008 temp]# docker images
REPOSITORY                            TAG       IMAGE ID       CREATED         SIZE
stubbornhuang/ubuntu                      v3    02ddc87b8a6e   20 hours ago    1.75GB
ubuntu                                latest    c6b84b685f35   3 weeks ago     77.8MB
zookeeper                             latest    f65dba39ec27   21 months ago   278MB
redis                                 latest    aea9b698d7d1   21 months ago   113MB
各个字段说明:
- REPOSITORY:表示镜像的仓库源
- TAG:镜像标签
- IMAGE ID:镜像ID
- CREATED:镜像创建时间
- SIZE:镜像大小
3 查找、搜索镜像
我们可以使用docker search命令在Docker Hub中搜索镜像,比如查找ubuntu镜像
docker search ubuntu
输出
[root@ai-0008 temp]# docker search ubuntu
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                           Ubuntu is a Debian-based Linux operating sys…   16372     [OK]       
websphere-liberty                WebSphere Liberty multi-architecture images …   296       [OK]       
ubuntu-upstart                   DEPRECATED, as is Upstart (find other proces…   115       [OK]       
neurodebian                      NeuroDebian provides neuroscience research s…   103       [OK]       
ubuntu/nginx                     Nginx, a high-performance reverse proxy & we…   98                   
ubuntu/squid                     Squid is a caching proxy for the Web. Long-t…   66                   
open-liberty                     Open Liberty multi-architecture images based…   61        [OK]       
ubuntu/apache2                   Apache, a secure & extensible open-source HT…   60                   
ubuntu/bind9                     BIND 9 is a very flexible, full-featured DNS…   59                   
ubuntu/mysql                     MySQL open source fast, stable, multi-thread…   52                   
ubuntu-debootstrap               DEPRECATED; use "ubuntu" instead                52        [OK]       
ubuntu/prometheus                Prometheus is a systems and service monitori…   50                   
ubuntu/kafka                     Apache Kafka, a distributed event streaming …   33                   
ubuntu/postgres                  PostgreSQL is an open source object-relation…   31                   
ubuntu/redis                     Redis, an open source key-value store. Long-…   19                   
ubuntu/dotnet-aspnet             Chiselled Ubuntu runtime image for ASP.NET a…   11                   
ubuntu/dotnet-runtime            Chiselled Ubuntu runtime image for .NET apps…   10                   
ubuntu/zookeeper                 ZooKeeper maintains configuration informatio…   10                   
ubuntu/dotnet-deps               Chiselled Ubuntu for self-contained .NET & A…   10                   
ubuntu/grafana                   Grafana, a feature rich metrics dashboard & …   9                    
ubuntu/prometheus-alertmanager   Alertmanager handles client alerts from Prom…   9                    
ubuntu/memcached                 Memcached, in-memory keyvalue store for smal…   5                    
ubuntu/cortex                    Cortex provides storage for Prometheus. Long…   4                    
ubuntu/telegraf                  Telegraf collects, processes, aggregates & w…   4                    
ubuntu/cassandra                 Cassandra, an open source NoSQL distributed …   2  
4 删除镜像
我们可以使用docker rmi命令删除镜像,比如删除ubuntu镜像
docker rmi ubuntu
5 查看镜像构建历史
我们可以使用docker history查看镜像构建历史,比如查看ubuntu的构建历史
docker history ubuntu
输出
[root@ai-0008 temp]# docker history ubuntu
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
c6b84b685f35   3 weeks ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      3 weeks ago   /bin/sh -c #(nop) ADD file:aa9b51e9f0067860c…   77.8MB    
<missing>      3 weeks ago   /bin/sh -c #(nop)  LABEL org.opencontainers.…   0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  LABEL org.opencontainers.…   0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  ARG LAUNCHPAD_BUILD_ARCH     0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  ARG RELEASE                  0B 
参考
- https://www.runoob.com/docker/docker-image-usage.html
- [https://blog.csdn.net/carefree2005/article/details/130596862](
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:Docker – 镜像操作入门指南
原文链接:https://www.stubbornhuang.com/2794/
发布于:2023年09月09日 13:48:28
修改于:2023年09月12日 15:09:54
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
 
				 
 
 
																					 
														
						 
														
						 
														
						
评论
70