欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux CentOS7 下 Docker镜像管理[image](亲测非转载)

程序员文章站 2022-07-06 18:02:36
...

 

docker安装参考:https://blog.csdn.net/tangyaliang11/article/details/87266194
docker官方镜像列表地址:https://hub.docker.com/search?q=&type=image

获取镜像
命令:docker [image] pull name[:tag]
如获取nginx1.14镜像命令:$ docker image pull nginx:1.14 。下载的就是nginx1.14版本。通过命令docker images可以查看到有两个版本的nginx 一个是获取镜像是带tag的,一个获取镜像时不带tag获取最新版本的镜像。

[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.14                1293e2b0a1af        8 days ago          109MB
nginx               latest              f09fe80eb0e7        8 days ago          109MB
hello-world         latest              fce289e99eb9        6 weeks ago         1.84kB

删除镜像
命令:docker image rm name/imageID,还有其他删除方式个人不建议所以不说了避免混淆。
如通过docker image rm nginx 删除的镜像则是latest版本,不必担心会删除其他的版本。
也可以通过镜像id删除。

[aaa@qq.com ~]# docker image rm nginx
Untagged: nginx:latest
Untagged: aaa@qq.com:dd2d0ac3fff2f007d99e033b64854be0941e19a2ad51f174d9240dda20d9f534
Deleted: sha256:f09fe80eb0e75e97b04b9dfb065ac3fda37a8fac0161f42fca1e6fe4d0977c80
Deleted: sha256:355e42205f69706706acf084bc2ed484ea5c034a8861b5bcebb7d8bf141fd686
Deleted: sha256:70cc0be2e302cca187cfadaaf528005d004bec56d46ae890a8a06bdd96768ef1
[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.14                1293e2b0a1af        8 days ago          109MB
hello-world         latest              fce289e99eb9        6 weeks ago         1.84kB
[aaa@qq.com ~]# docker image rm 1293e2b0a1af
Untagged: nginx:1.14
Untagged: aaa@qq.com:d1eed840d5b357b897a872d17cdaa8a4fc8e6eb43faa8ad2febb31ce0c537910
Deleted: sha256:1293e2b0a1af4854799d5f4a335c59f475cb68ef34e35f8e901d77971eb47315
Deleted: sha256:7016890e9376cbb07ba5ac82aa99da0183ecc886bd3307f7b0bdb08cb6d862f6
Deleted: sha256:7860b3787f080232f56aedd7d431277c2cae522773a7a04c38b158db4b7756d5
Deleted: sha256:0a07e81f5da36e4cd6c89d9bc3af643345e56bb2ed74cc8772e42ec0d393aee3
[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        6 weeks ago         1.84kB

清理镜像
命令:docker image prune,清理临时镜像,支持的选项-a --all,将删除所有没有使用的镜像。

[aaa@qq.com ~]# docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: hello-world:latest
untagged: aaa@qq.com:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3
untagged: nginx:latest
untagged: aaa@qq.com:dd2d0ac3fff2f007d99e033b64854be0941e19a2ad51f174d9240dda20d9f534
deleted: sha256:f09fe80eb0e75e97b04b9dfb065ac3fda37a8fac0161f42fca1e6fe4d0977c80
deleted: sha256:355e42205f69706706acf084bc2ed484ea5c034a8861b5bcebb7d8bf141fd686
deleted: sha256:70cc0be2e302cca187cfadaaf528005d004bec56d46ae890a8a06bdd96768ef1
deleted: sha256:0a07e81f5da36e4cd6c89d9bc3af643345e56bb2ed74cc8772e42ec0d393aee3

Total reclaimed space: 109.2MB

创建镜像
主要有三种方式:1、基于已有镜像的容器创建。2、基于本地模板导入。3基于Dockerfile创建。
第三种方式最常用也是我主要使用方式,所以前两种不讲了。以nginx镜像为例。

// 创建一个test文件夹
[aaa@qq.com test]# mkdir /usr/local/test
// 进入文件夹
[aaa@qq.com test]# cd /usr/local/test
// 下载nginx1.13.7
[aaa@qq.com test]# wget http://nginx.org/download/nginx-1.13.7.tar.gz
// 创建Dockerfile文件
[aaa@qq.com test]# tonch Dockerfile
// 编辑文件
[aaa@qq.com test]# vi Dockerfile
##文件内容
FROM centos:7 #系统环境
MAINTAINER  tangyaliang aaa@qq.com #镜像作者信息
RUN yum provides '*/applydeltarpm' #这三行是解决一个软件版本错误
RUN yum install deltarpm -y
RUN rpm --import /etc/pki/rpm-gpg/RPM*
RUN yum install -y gcc gcc-c++ autoconf automake make zlib zlib-devel openssl openssl-devel pcre pcre-devel #安装nginx依赖
ADD nginx-1.13.7.tar.gz /usr/local/ #将下载的nginx添加至镜像,会自动解压缩
WORKDIR /usr/local/nginx-1.13.7/ #设置工作目录
RUN ./configure --prefix=/usr/local/nginx && make && make install #与正常环境安装一样
RUN ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/ #建立一个引用
CMD ["nginx","-g","daemon off;"] #镜像启动后执行的命令,启动nginx
##文件内容
//构建镜像
[aaa@qq.com test]# docker image build -t nginx:myapp .
//依靠镜像运行容器,并且将宿主机8888端口映射给容器的80端口
[aaa@qq.com test]# docker container run -idt -p 8888:80 --name myapp nginx:myapp
# 显示镜像列表
[aaa@qq.com test]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               myapp               a70a6ca493f9        24 minutes ago      703MB
nginx               latest              f09fe80eb0e7        8 days ago          109MB
centos              7                   1e1148e4cc2c        2 months ago        202MB
# 显示容器列表
[aaa@qq.com test]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
4a19ad0109b0        nginx:myapp         "nginx -g 'daemon of…"   5 seconds ago       Up 4 seconds        0.0.0.0:8888->80/tcp   myapp
e02303e20e46        nginx               "nginx -g 'daemon of…"   28 minutes ago      Up 28 minutes       0.0.0.0:8080->80/tcp   nginx123

访问刚运行的容器。

linux CentOS7 下 Docker镜像管理[image](亲测非转载)