Dockerfile details

Dockerfile is a text file that contains the basic image and various instructions used when assembling a new image. Use the dockerfile file to define the image, then run the image and start the container.

Build image steps

① Write a dockerfile
② Use ​​​docker build​​​ to build the image
③ ​​​docker run​​​ Run the image
④ ​​​docker push​​Publish the image to the remote

Components of dockerfile

A dockerfile contains the following parts:

  • Base image information: Use the FROM keyword to specify the base image information. FROM is the first instruction in the dockerfile file.
  • Maintainer information: Specify using the MAINTAINER keyword. You can usually use the name or email of the dockerfile creator as the maintainer information.
  • Mirror operation command: If a mirror operation command is not executed, a new layer will be added to the image.
  • Container startup execution command: The user specifies the command to be executed when starting the container, specified through: CMD ENTRYPOINT

Common mirror operation instructions:

-instruction- -describe-
FROM #Basic image, everything starts from here
MAINTAINER Who wrote the mirror, name + email
RUN Commands that need to be run when building the image
ADD Add content: For example, add a tomcat compressed package
WORKDIR Mirror's working directory
VOLUME Directory where the mirror is mounted
EXPOSE Keep exposed ports
CMD Specify the command to be run when this container starts. Only the last one will take effect and can be replaced.
ENTRYPOINT Specify the command that needs to be run when this container is started. You can append the command
ONBUILD The ONBUILD command is run when building an inherited DockerFile. Trigger command
COPY Similar to ADD, copy our files to the mirror
ENV Set environment variables when building

Execute docker build command in dockerfile

docker build command

The docker build command is used to create an image using a Dockerfile

docker build [OPTIONS] PATH | URL | -

-c: Control CPU usage
-f: Specify Dockerfile name
-m: Set the upper limit of build memory
-q: Do not display information
about the build process -t: Label the built image

Example

Build an image, name it hello, and specify the context directory as the current directory:

docker build -t "hello" .

Build an image, name it hello, and specify the context directory as test:

docker build -t "hello" ./test/

Build an image, name it hello, label it new, specify the context directory as the current directory, the file as newfile in the test directory, and do not display the build process:

docker build -q -f test/newfile -t "hello:new" .
-parameter- -explain-
-build-arg=[] – Set variables when the image is created;
–cpu-shares Set cpu usage weight;
–cpu-period Limit CPU CFS cycles;
–cpu-quota Limit CPU CFS quota;
–cpuset-cpus Specify the CPU id used;
–cpuset-mems Specify the memory id used;
–disable-content-trust Ignore verification, enabled by default;
-f Specify the Dockerfile path to use;
–force-rm Delete the intermediate container during the mirroring process;
–isolation Use container isolation technology;
–label=[] Set the metadata used by the image;
-m Set the maximum memory value;
–memory-swap Set the maximum value of Swap to memory + swap, "-1" means no limit to swap;
–no-cache The process of creating an image does not use caching;
–pull Try to update the image to a new version;
–quiet, -q Quiet mode, only the image ID is output after success;
–rm Delete the intermediate container after successfully setting up the image;
–shm-size Set the size of /dev/shm, the default value is 64M;
- decrease Ulimit configuration.
–squash Compress all operations in the Dockerfile into one layer.
–tag, -t: The name and tag of the image, usually in name:tag or name format; multiple tags can be set for one image in one build.
–network: Default. Set network mode for RUN instruction during build

使用dockerfile构建镜像实战

使用dockerfile 构建前端vue项目

在这里插入图片描述
Dockerfile 文件内容

from node:16.20.1
MAINTAINER DETALCAIJ
WORKDIR /usr/app
COPY ./ /usr/app
RUN git clone https://gitee.com/vdpadmin/variant-form.git
RUN npm install --registry=https://registry.npm.taobao.org
EXPOSE 8080
CMD ["npm","run","server"]

在这里插入图片描述

通过Dockerfile 构建contos系统本地镜像

Dockerfile文件

FROM centos:7.9.2009
# 指定基础镜像为 centos
MAINTAINER  mufenggrow [email protected]
ENV  MYPATH  /usr/local
WORKDIR $MYPATH

RUN yum -y install vim
RUN yum -y install net-tools

EXPOSE  80

CMD echo $MYPATH
CMD echo "-----end-----"
CMD /bin/bash

构建并测试
通过命令构建镜像 最后有个 .语法如下:

 docker build -f dockerfile 文件名 -t 镜像名:[tag] .

通过Dockerfile构建镜像文件

docker build -t mufeng_centos

查看构建好的镜像

 root@ff-xas:/opt# docker images
REPOSITORY            TAG               IMAGE ID       CREATED              SIZE
mufeng_centos         latest            924e651184db   About a minute ago   667MB
node                  16.20.1           a21a68db6806   3 weeks ago          909MB
tomcat                latest            7ba61facbe26   3 weeks ago          425MB
openresty/openresty   1.13.6.2-alpine   ef57f6ca4202   4 years ago          49.1MB

Guess you like

Origin blog.csdn.net/tian830937/article/details/132437494