To start building a base image from a blank image, follow these steps:
- Choose a blank mirror as a base, eg
scratch
mirror. - Create a Dockerfile, which defines the software packages, configuration files, etc. that the base image needs to install.
ADD
Use the or directive in the DockerfileCOPY
to add packages, configuration files, etc. to the image.- Use
RUN
instructions to execute commands in the image, such as installing software packages, configuring environment variables, and so on. - Use
CMD
the orENTRYPOINT
directive to define the command or program to be executed by default after the container starts. - Use
docker build
the command to build the Dockerfile into an image. Here is a simple example Dockerfile for building ascratch
base image based on the image:
plaintextCopy code
FROM scratch # 添加软件包和配置文件 ADD software.tar.gz / ADD config / # 执行命令 RUN apt-get update && \ apt-get install -y software-package && \ rm -rf /var/lib/apt/lists/* # 定义容器启动后默认执行的命令 CMD ["software-package"]
Build the image with docker build
the command:
plaintextCopy code
docker build -t my-base-image .
Where my-base-image
is the name of the image, .
indicating the directory where the Dockerfile is located. After the build is complete, you can use docker run
the command to start the container and execute the default command:
plaintextCopy code
docker run --rm my-base-image
--rm 是容器运行结束后自动删除容器