Two ways to make images with Docker

Two ways to make images with Docker

There are two ways for Docker to make an image: docker commit to create an image and build an image based on dockerfile. In dddd , we talk about how to use dockerfile to build an image. Only the FROM and RUN commands are used, which is relatively simple.

This article talks about how to use docker commit to make images and other commands and usage of dockerfile.

First, dockers commit to make an image

Make an image through docker commit, run a basic image, and then install the software and modify the configuration file on the basic image. Then submit the changes to the new image.

1、docker run

1) Docker images first check if there is an existing local image, select the required basic image, if not needed, use docker pull to pull the required image

2) Then use docker run to run the image as a container

2. docker commit -a author information -m description information container name mirror name (take one yourself)

-a Specify the author information -m Submit the description information -p suspend the container by default when committing

The specific implementation is as follows:

 

 

In the last red box, the docker commit command finally saves the name of the modified image, just pick one yourself

3. docker diff View changes in the image storage layer

4、docker history

-q only display mirror ID

 

Description:

1) In addition to what you really want to modify with the commit image, other files will be changed or added due to the execution of the command. If you are installing software packages, compiling and building, a lot of extraneous content will be added, which Causing the mirror image to be extremely bloated ,

Docker commit means that the mirror operations are all black-box operations , it is difficult for others to know what is happening inside, and maintenance is very difficult. The official does not recommend creating by commit.

2) This method is the most free and easiest if you don't worry about the mirror becoming bigger and bigger.

Second, the Dockerfile command

A Dockerfile is a text file that contains a series of instructions. Each instruction builds a layer. Therefore, the content of each instruction describes how the layer should be built.

Note: When building the image, make sure to add only what you really need to add to each layer, and any unrelated things should be cleaned up

1. FROM specifies the base image

格式:FROM <image> 或者 FROM <image>:<tag>

For example: FROM centos: 7

2. MAINTAINER  designated maintainer information

 

Format: MAINTAINER <name>

For example: MAINTAINER hxq [email protected]

3. Run command

1) Shell mode:

RUN <command> is like the command entered directly on the command line

RUN <command> uses `/ bin / sh -c` by default to execute commands

例:RUN  yum install -y httpd

例:RUN  echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html

2) exec mode

RUN ["executable file", "parameter 1", "parameter 2"], this is more like the format in a function call

Note: The RUN command is used to execute the command line. Note that each use of a RUN (dockerfile command) is to add one more layer to the image (the maximum limit is 127 layers).

          Execute multiple instructions to connect with && (apt-get plus -y, which means that the system installation prompts select yes by default, otherwise the mirroring fails; if there is a line break between the && command, remember to bring \ after the line )

 

4. COPY copy files

格式:COPY src desc
复制本地主机src目录或文件到容器的desc目录,desc不存在时会自动创建。

5、ADD 高级复制

 

格式:ADD src dest
该命令将复制指定本地目录中的文件到容器中的dest中,src可以是是一个绝对路径,也可以是一个URL或一个tar文件,tar文件会自动解压为目录。

 

6、CMD 容器启动命令

 

是可以被 docker run 指令覆盖的,而ENTRYPOINT的参数可以被--entrypoint覆盖;会比CMD或者docker run指定的命令要靠前执行。

 

7、ENTRYPOINT 入口点
ENTRYPOINT ["executable","param1","param2"]
ENTRYPOINT command param1,param2 会在shell中执行。
用于配置容器启动后执行的命令,这些命令不能被docker run提供的参数覆盖。和CMD一样,每个Dockerfile中只能有一个ENTRYPOINT,当有多个时最后一个生效。

 

8、ENV 用设置环境变量

 

这些环境变量,后续可以被RUN指令使用,容器运行起来之后,也可以在容器中获取这些环境变量。
格式为:EVN key value
ENV word hello
RUN echo $word

 

9、ARG 构建参数

 

10、VOLUMN 定义匿名卷

 

格式: VOLUME ["/data"]
作用是创建在本地主机或其他容器可以挂载的数据卷,用来存放数据。

 

11、EXPOSE 暴露端口

在启动容器的使用使用-P,Docker会自动分配一个端口和转发指定的端口,使用-p可以具体指定使用哪个本地的端口来映射对外开放的端口。
格式: EXPOSE port [port2,port3,...]
比如:EXPOSE 80 这条指令告诉Docker服务器暴露80端口,供容器外部连接使用。

 

12、WORKDIR  指定工作目录

 

为后续的RUN CMD ENTRYPOINT指定配置工作目录,可以使用多个WORKDIR指令,若后续指令用得是相对路径,则会基于之前的命令指定路径。
格式: WORKDIR /path
比如: WORKDIR /path/to/workdir

 

13、USER 指定容器运行时的用户名或UID

 

后续的RUN也会使用指定的用户。要临时使用管理员权限可以使用sudo。在USER命令之前可以使用RUN命令创建需要的用户。
格式为:USER username
比如:RUN groupadd -r docker && useradd -r -g docker docker

 

14、HEALTHCHECK 健康检查

15、ONBUILD 镜像触发器

 

格式:ONBUILD [INSTRUCTION]
该配置指定当所创建的镜像作为其他新建镜像的基础镜像时所执行的指令。
例如下面的Dockerfile创建了镜像A:
ONBUILD ADD . /app
ONBUILD RUN python app.py

 

则基于镜像A创建新的镜像时,新的Dockerfile中使用from A 指定基镜像时,会自动执行ONBBUILD指令内容,等价于在新的要构建镜像的Dockerfile中增加了两条指令:
FROM A
ADD ./app
RUN python app.py
16、docker build
创建好Dockerfile之后,通过docker build命令来创建镜像,该命令首先会上传Dockerfile文件给Docker服务器端,服务器端将逐行执行Dockerfile中定义的指令。
通常建议放置Dockerfile的目录为空目录。另外可以在目录下创建.dockerignore文件,让Docker忽略路径下的文件和目录,这一点与Git中的配置很相似。

 

通过 -t 指定镜像的标签信息,例如:docker build -t regenzm/first_image . ## "."指定的是Dockerfile所在的路径

 

Guess you like

Origin www.cnblogs.com/hld123/p/12751539.html