docker系列文章请戳主页https://me.csdn.net/zhangzeshan
镜像 就相当于类 一个镜像会产生多个容器container
本文主要总结关于镜像的一些基本使用命令 也会有具体的示例
搜索镜像
#搜索nginx镜像
docker search nginx
#搜索点赞数大于30的镜像 OFFICIAL为OK 表示官方镜像
docker search -s 30 nginx
查看持有的所有镜像
#查看持有的所有镜像(显示所有属性)
docker images -a
#查看持有的所有镜像(只显示id)
docker images -q
查看image的分从
docker history IMAGE ID
删除image
#删除单个
docker rmi ab8424e38efd(镜像ID)
删除过程中出现Error response from daemon: conflict: unable to remove repository reference
先查看在使用的容器 docker ps -a
然后根据CONTAINER ID删除 container docker rm CONTAINERID(容器ID)
或者强制删除 docker rmi -f ab8424e38efd(容器ID)
#删除多个
docker rmi a b c
#删除全部(强制)
docker rmi -f $(docker images -q)
获取镜像
#方法1-------利用docker build 创建image
1.创建一个文件夹 进入这个文件夹 然后创建 Dockerfile
(就把这个file当作一个脚本 里面写入命令 执行这个文件 里面的命令也会去执行)
编写内容 (安装vim)
FROM centos
RUN yum -y install vim
保存退出
2.执行Dockerfile 创建docker-image
docker build -t 镜像名字 .(代表运行的dockerFile是基于当前目录的)
3.执行之后 查看imgae 列表
dockers image 就会发现新创建了上面的iamge 并且进入这个image 发现已经装好了vim
#方法2------- docker pull Registry(image名字)---类似于git的拉取
#拉取ubuntu 14.04 拉取私人镜像也是同样的方法
docker pull ubuntu:14.04
运行镜像并挂载另外一个镜像
#语法 docker run -it --name 新的容器名字 --volumes-from 另外一个容器名字 当前名字
如:
docker run -it --name dc02 --volumes-from doc1 zhangzeshan/centos
案例1 自己创建一个Image-----Base Image(基础镜像)
1.拉取hello-world镜像 (该镜像属于baseImage)
docker pull hello-world
2.模仿做一个像上述的镜像 创建一个文件夹 hello-world 进入这个文件夹 创建hello.c 文件 编写内容
#include<stdio.h>
int main(){
printf("hello-world");
}
3.通过gcc编译该文件 首先要进行 yum install glibc-static gcc
gcc -static hello.c -o hello
4.执行命令 查看是否输出 hello-world
./hello
5.创建Dockerfile文件 编写以下内容
FROM scratch
ADD hello /
CMD ["/hello"]
6.创建image
docker build -t zhangzeshan/hello-world .
案例2 从Dockerfile构建一个镜像 也就是 docker build
1.删掉上述的image
docker image rm d34ef706b552(IMAGE ID)
2.创建目录用来存放Dockerfile
mkdir docker-centos-vim
3,在这个文件夹下 创建Dockerfile 文件 编写内容
FROM centos
RUN yum intall -y vim
4.根据 Dockerfile 创建镜像
docker build -t 镜像名字 .
5.查看所有镜像会发现上面的命令 产生了一个新的镜像
docker images
6.使用这个镜像
docker run -it 镜像名
7.执行命令 vim 发现这个镜像也已经装了vim
注意!
build过程中进行调试:
复制要调试的步骤的 id 去执行临时镜像
docker run -it 临时镜像id /bin/bash
关于镜像的相关操作就先介绍到这里 到此 下一篇文章 将介绍关于容器container的使用
有任何问题欢迎在评论区留言
觉得有用的欢迎点个赞加个关注哦!
来源:CSDN
作者:张芝山
链接:https://blog.csdn.net/zhangzeshan/article/details/103847142