从零开始的Docker [ 2 ] --- Docker容器管理及镜像制作,import 和 load

Docker 管理镜像

一、制作镜像

容器内的服务不能以后台方式启动

重新修改容器的名字

docker rename e63dca6e4930 docker  # rename 唯一标识 新名称

1.给目前容器制作镜像

目前的容器镜像名为 docker
新文件名为 centos-neko.tar

docker export -o centos-neko.tar docker

2.尝试停止并删除容器

docker stop docker

docker rm docker

删除库内镜像

docker rmi centos-1

3.恢复

要想恢复原来被删除的容器,需要导入 tar 文件为一个镜像到本地仓库

docker import centos-neko.tar neko

验证

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
neko         latest    3bf194b11664   47 seconds ago   209MB

4.运行

运行这个新的镜像为一个新的容器

docker run -itd neko bash 

5.验证

进入容器验证,原来创建的文件是否还存在

docker exec -it 448 bash

二、保存本地镜像到本地文件

1.创建

docker export 打包 container 文件系统

docker export -o thecontainer.tar container_name

2.导入加载镜像

docker load -i redis.tar

3.用 docker import 载入

可以为新镜像指定name和tag

docker import thecontainer.tar newimagename:tag

4.docker import 和 docker load 的区别

注意:

docker import 和docker load 的区别在于:

load 是用来导入镜像存储文件到本地镜像库的,镜像存储文件是用save从本地镜像库保存到本地硬盘的镜像备份文件,一般容量相对容器的快照文件较大,保存的是完整的记录,导入时,不能重新指定标签(tag)等元数据信息;

而 import 导入的是容器的快照文件,容器的快照文件体积较小,它丢弃了历史记录和元数据信息,仅仅保存容器当时的快照状态。

看了另一位博主的文章 docker save load export import区别详解

还有 一位 运维开发大佬的文章 docker import 和docker load的区别

三、创建自己的镜像 docker commit

1.通过容器创建

1.启动容器,并安装软件

[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED       STATUS       PORTS     NAMES
448f7f33b79d   neko      "bash"    3 hours ago   Up 3 hours             unruffled_babbage
[root@localhost ~]# docker run -it neko bash
[root@89293525c260 /]# yum -y install vim

2.commit 提交到本地仓库

commit 语法:
docker commit [选项] <容器ID或容器名> [<镜像名>[:<标签>]]

示例:
打开另外一个终端,或者退出容器,在宿主机上执行如下命令:

开始提交到本地仓库

[root@localhost ~]# docker commit \
> --author "neko<[email protected]>" \
> -m "install vim" \
> 448f7f33b79d \
> centos-vim:1.0

sha256:6cfcf2ab431ebe460f0e768a76da2767c9c352ff250651c62b03aaba4d8e4052

参数说明:
–author 作者
–message 说明信息

[root@localhost ~]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE示例:
centos-vim   1.0       6cfcf2ab431e   14 seconds ago   209MB

注:此操作只能在本地操作 [本地仓库]

四、Dockerfile详解

1.编辑Docker文件

[root@localhost ~]# mkdir centos_dockerfile
[root@localhost ~]# cd centos_dockerfile/
[root@localhost centos_dockerfile]# vim dockerfile
FROM centos
LABLE maintainer="neko<[email protected]>" description="Install tree vim*"
RUN rpm -qa | grep tree || yum  install -y tree vim*

2.开始构建镜像

命令语法格式:
docker bulid -t 仓库名/镜像名:tag .
docker build [选项] <上下文路径/URL/->

1 执行命令

[root@localhost centos_dockerfile]# docker build -t centos:1.20 .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM centos:latest
 ---> e934aafc2206
Step 2/3 : LABEL maintainer="neko<[email protected]>" description="Install tree vim*"
 ---> Using cache
 ---> 1207b2848015
Step 3/3 : RUN rpm -qa | grep tree || yum  install -y tree     vim*
 ---> Running in 33d321b249d7
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
...略...
Complete!
Removing intermediate container 33d321b249d7
 ---> adc30981bc84

Successfully built adc30981bc84    # 表示构建成功
Successfully tagged centos:1.20    # TAG 标签
[root@localhost centos_dockerfile]#

2.docker build 构建镜像

[root@localhost centos_dockerfile]# mkdir test 
[root@localhost centos_dockerfile]# touch ockerfile.centos
[root@localhost centos_dockerfile]# mkdir test/a.txt
[root@localhost centos_dockerfile]# mkdir test/b.txt
[root@localhost centos_dockerfile]# mkdir test/test.neko
[root@localhost centos_dockerfile]# tree .
.
├── dockerfile.centos
└── test
    ├── a.txt
    ├── b.txt
    └── test.neko

[root@localhost centos_dockerfile]# docker build -f dockerfile.centos -t centos .  

-f 只要不是默认的Dockerfile 就需要 加 -f 指定路径

五、FROM 指令

主要作用是指定一个镜像作为构建自定义镜像的基础镜像,在这个基础镜像之上进行修改定制。

这个指令是 Dockerfile 中的必备指令,同时也必须是第一条指令。

Docker Store 上有很多高质量的官方镜像,可以直接作为我们的基础镜像。

作为服务类的,如 Nginx Mongo 等.

用于开发的, 如 Python golang 等.

操作系统类, 如 Centos ubuntu 等.

除了一些现有的镜像,Docker 还有一个特殊的镜像 scratch
这个镜像是虚拟的,表示空白镜像
这以为着这将不以任何镜像为基础镜像。

###制作自己的go语言 Hello World

1.检测是否有 gccglibc-static 没有则安装

rpm -qa gcc glibc-static || yum -y install gcc glibc-static

2.编辑 C 源代码文件

[root@localhost ~]# cat hello.c
#include <stdio.h>

int main()
{
    
    
   printf("Hello, Neko!! \n");
   return 0;
}

3.将源代码文件编译为可执行的二进制文件

gcc --static hello.c -o hello

测试

[root@localhost ~]# ./hello 
Hello, Neko!! 

4.编辑 Dockerfile

在有 hello 二进制的文件目录下,编译 Dockerfile 文件,内容如下:

[root@localhost ~]# cat Dockerfile.hello
FROM scratch
ADD hello /
CMD ["/hello"]

ADD 是把当前目录下的 hello 文档拷贝到 容器中的根目录下
CMD 执行根目录下的 hello 文件

5.构建新的镜像

[root@localhost helloc]#  docker build -f Dockerfile.hello -t hello.c .
Sending build context to Docker daemon  863.7kB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : ADD ./hello /
 ---> Using cache
 ---> 9351708f0cdc
Step 3/3 : CMD ["/hello"]
 ---> Using cache
 ---> 1c203177d2bd
Successfully built 1c203177d2bd
Successfully tagged hello.c:latest

6.查看本地仓库验证

[root@localhost helloc]# docker image ls hello
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
hello        1.11      1c203177d2bd   9 minutes ago   861kB

六、LABEL 指令

LABEL maintainer="nekoosu.com"
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \
that label-values can span multiple lines."

一个镜像可以有多个LABEL标签。可以把他们写在一行或用反斜线进行续航

LABEL multi.label1="value1" \
      multi.label2="value2" \
      other="value3"

要查看镜像的 LABEL 信息,请使用该docker inspect命令。

要查看镜像的 LABEL 信息,请使用该docker inspect命令。

七、ENV 指令

用于设置环境变量
格式有两种:
ENV <key> <value>
ENV <key1>=<value1> <key2>=<value2>...

推荐的还是这种

ENV VERSION=1.0 DEBUG=on \
 NAME="BiliBili Cheers!"

支持环境变量: ADD、COPY、ENV、EXPOSE、LABEL、USER、WORKDIR、VOLUME、STOPSIGNAL、ONBUILD。

八、RUN 指令

RUN 指令是在容器内执行 shell 命令,默认会是用 /bin/sh -c 的方式执行。

执行命令的两种方式

RUN (shell形式,该命令在shell中运行)
RUN [“executable”, “param1”, “param2”](exec形式)

正确写法:

FROM alpine
ENV name="neko"
RUN ["/bin/sh", "-c", "/bin/echo $name"]

注意引号及命令的写法 否则会被作为普通的字符串输出了,因为 $name 是 shell 中的用法,而这里里并没有 使用到 shell
注意: exec的方式下,列表中的内容会被解析为JSON数组,这意味着您必须在单词周围使用双引号(“) 而非单引号(’)。

猜你喜欢

转载自blog.csdn.net/Houaki/article/details/112592482
今日推荐