菜鸟系列docker——docker镜像下(5)

1. docker镜像构建

该章节主要介绍2种构建docker镜像的方式。当然,当前基本上需要的基础镜像都有,直接使用即可。例如数据库、中间件之类的,只需要拿来使用即可,但是依然存在需要开发者构建镜像的情况。

1.1 docker commit

不推荐使用
先看看docker commit的含义

[centos@jiliguo docker]$ docker commit --help

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)
[centos@jiliguo docker]$ 

从一个改变的容器构建镜像,也就是说,docker commit启动一个容器,修改容器,再将修改后的容器保存为一个新的镜像。
接下来将演示一下如何通过docker commit 构建镜像

  • 运行镜像,并查看文件内容:
[centos@jiliguo docker]$ docker run  -it --name=helloworld myhelloworld:v1.0 bash
root@9508f1279b44:/# cat /var/local/hello-world.txt 
hello world
  • 修改容器内文件内容
root@9508f1279b44:/# cat /var/local/hello-world.txt 
hhhh
  • 保存镜像
[centos@jiliguo ~]$ docker ps
CONTAINER ID        IMAGE                                     COMMAND                  CREATED              STATUS              PORTS                                                                       NAMES
9508f1279b44        myhelloworld:v1.0                         "bash"                   About a minute ago   Up About a minute                                                                               helloworld
[centos@jiliguo ~]$ docker commit helloworld hhhh:v1.0
sha256:99d078dd4c709bb799092517947aa88600aca7e3fd5fe3101e5d98a5bad2ba0d
[centos@jiliguo ~]$ 
  • 测试
[centos@jiliguo ~]$ docker run -it hhhh:v1.0 bash
root@0475a8f33943:/# cat /var/local/hello-world.txt 
hhhh
root@0475a8f33943:/# 

从上可知新的镜像的确是保存的修改后的内容

1.2 Dockerfile构建

通过dockerfile文件构建镜像,查看下docker build

[centos@jiliguo docker]$ docker build --help

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

演示一下:
Dockerfile:这就是hello-world镜像的dockerfile文件

FROM ubuntu
COPY hello-world.txt /var/local/hello-world.txt
CMD cat /var/local/hello-world.txt

构建镜像

[centos@jiliguo docker]$ docker build -t myhelloworld:v1.0 .
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/3 : COPY hello-world.txt /var/local/hello-world.txt
 ---> 53bb09dc5daa
Step 3/3 : CMD cat /var/local/hello-world.txt
 ---> Running in 14f24da48b05
Removing intermediate container 14f24da48b05
 ---> 45219c78619f
Successfully built 45219c78619f
Successfully tagged myhelloworld:v1.0
--cpu-shares :设置 cpu 使用权重;
--cpu-period :限制 CPU CFS周期;
--cpu-quota :限制 CPU CFS配额;
--cpuset-cpus :指定使用的CPU id;
--cpuset-mems :指定使用的内存 id;
--disable-content-trust :忽略校验,默认开启;
-f :指定要使用的Dockerfile路径;
--force-rm :设置镜像过程中删除中间容器;
--isolation :使用容器隔离技术;
--label=[] :设置镜像使用的元数据;
-m :设置内存最大值;
--memory-swap :设置Swap的最大值为内存+swap,"-1"表示不限swap;
--no-cache :创建镜像的过程不使用缓存;
--pull :尝试去更新镜像的新版本;
--quiet, -q :安静模式,成功后只输出镜像 ID;
--rm :设置镜像成功后删除中间容器;
--shm-size :设置/dev/shm的大小,默认值是64M;
--ulimit :Ulimit配置。
--tag, -t: 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。
--network: 默认 default。在构建期间设置RUN指令的网络模式

有时候并不会创建成功,可能是在某个环节出现了问题,这个时候我们可以一行一行命令调试,每次添加一行命令,构建一下镜像,就知道在哪一行错误了。

通过docker history可以查看构建镜像记录

[centos@jiliguo docker]$ docker history myhelloworld:v1.0
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
45219c78619f        22 minutes ago      /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "cat …   0B                  
53bb09dc5daa        22 minutes ago      /bin/sh -c #(nop) COPY file:8181b3ba47dd4294…   12B                 
94e814e2efa8        5 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           5 weeks ago         /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
<missing>           5 weeks ago         /bin/sh -c rm -rf /var/lib/apt/lists/*          0B                  
<missing>           5 weeks ago         /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   745B                
<missing>           5 weeks ago         /bin/sh -c #(nop) ADD file:1d7cb45c4e196a6a8…   88.9MB 

1.3 dockerfile常见命令

FROM
指定 base 镜像。
MAINTAINER
设置镜像的作者,可以是任意字符串。
COPY
将文件从 build context 复制到镜像。
COPY 支持两种形式:
COPY src dest
ADD
与 COPY 类似,从 build context 复制文件到镜像。不同的是,如果 src 是归档文件(tar, zip, tgz, xz 等),文件会被自动解压到 dest。
ENV
设置环境变量,环境变量可被后面的指令使用
EXPOSE
指定容器中的进程会监听某个端口,Docker 可以将该端口暴露出来。
VOLUME
将文件或目录声明为 volume。
WORKDIR
为后面的 RUN, CMD, ENTRYPOINT, ADD 或 COPY 指令设置镜像中的当前工作目录。
RUN
在容器中运行指定的命令。
CMD
容器启动时运行指定的命令。
ENTRYPOINT
设置容器启动时运行的命令
。
Dockerfile 中可以有多个 ENTRYPOINT/CMD 指令,但只有最后一个生效。
如果你觉得写的不错,请移步www.itkezhan.top或者关注公众号IT程序员客栈
原文地址:https://www.cnblogs.com/i-dandan/p/10752932.html