Docker custom image-build image-Dockerfile syntax

Docker custom image-build image

Dockerfile

A Dockerfile is a text file used to define a Docker image. It contains a series of instructions and parameters to guide the Docker engine in building the image.

Basic structure and syntax

The basic structure of the Dockerfile is as follows:

# Comment
INSTRUCTION arguments
  • # Comment: Comment, used to explain instructions or provide relevant information.
  • INSTRUCTION: directive, used to specify the operations performed by the Docker engine.
  • arguments: Parameters of the instruction, used to specify the specific content of the operation.

Common commands

In the Dockerfile, we can use a variety of instructions to define the image building process. Here are some commonly used instructions:

FROM

FROMThe directive is used to specify the base image, that is, the image on which the new image depends. Example:

FROM ubuntu:latest

FROM ubuntu:latestIt is an instruction in Dockerfile FROM, used to specify the base image on which to build a new image. Here is a detailed explanation of the parameters of this command:

  • ubuntu: This is the name of the base image, indicating that the Ubuntu operating system is used as the base image. Ubuntu is a popular Linux distribution that is widely used for developing and deploying applications.
  • latest: This is a tag for the base image that specifies the specific version to use. In this example, latestit means using the latest version of Ubuntu as the base image.

RUN

RUNDirectives are used to execute commands in the image. Multiple RUNdirectives can be used to execute multiple commands. Example:

RUN apt-get update && apt-get install -y package

RUN apt-get update && apt-get install -y packageIt is an instruction in the Dockerfile RUN, used to execute commands in the image. Here is a detailed explanation of the parameters of this command:

  • apt-get: This is a package management tool in Ubuntu and Debian systems used to install, upgrade and remove software packages.
  • update: This is apt-geta subcommand of which updates the package list to get the latest available package information.
  • &&: This is the logical AND operator in Shell, used to connect two commands, and the latter command will be executed only after the previous command is executed successfully.
  • install: This is apt-getanother subcommand of which is used to install the specified package.
  • -y: This is apt-get installan option to the command to automatically answer confirmation prompts during installation to avoid the need for manual confirmation during installation.
  • package: This is the name of the package to install.

To sum up, RUN apt-get update && apt-get install -y packagethe purpose of the command is to execute two commands in the image: first update the package list, and then install the specified package and automatically answer the confirmation prompt during the installation process.

COPY

COPYDirective is used to copy a file or directory from the build context to a specified path in the image. Example:

COPY src/ /app/

COPY src/ /app/Is a directive in a Dockerfile COPYthat copies a file or directory from the build context to a specified path in the image. Here is a detailed explanation of the parameters of this command:

  • src/: This is the path to the source file or directory to be copied. In this example, src/it means the directory in the build context src/, which is the directory under the directory where the Dockerfile is located src/.
  • /app/: This is the destination path, specifying where in the image the source file or directory is to be copied. In this example, /app/represents the directory in the image /app/.

To sum up, COPY src/ /app/the function of the command is to copy the directory in the build context src/to the directory in the image /app/. In this way, when running the container, you can access the files or directories copied in the image.

WORKDIR

WORKDIRThe command is used to set the working directory, which is the default directory when executing subsequent commands in the image. Example:

WORKDIR /app

WORKDIR /appIs the instruction in the Dockerfile WORKDIR, used to set the working directory within the container. Here is a detailed explanation of the parameters of this command:

  • /app: This is the path to the working directory to be set. In this example, /apprepresents the directory under the root directory within the container app.

To sum up, WORKDIR /appthe function of the command is to set the working directory within the container to /appthe directory. This means that in subsequent commands, if the full path is not specified, relative paths will be /appresolved relative to the directory. This makes it easy to manipulate and manage files in the container.

EXPOSE

EXPOSEThe directive is used to declare the port that the container needs to listen to when running. Example:

EXPOSE 8080

EXPOSE 8080is a directive in a Dockerfile EXPOSEthat declares the network port that the container will listen on when running. Here is a detailed explanation of the parameters of this command:

  • 8080: This is the network port number to be declared. In this example, 8080represents the network port number that the container will listen on.

To summarize, EXPOSE 8080what the command does is declare a port that the container will listen to when running 8080so that other containers or hosts can communicate with the container through that port. This declaration does not automatically map the container's port to the host. You need to use -pparameters when running the container to perform port mapping.

CMD

CMDDirectives are used to specify commands to be executed when the container starts. Multiple CMDinstructions can be used, but only the last one takes effect. Example:

CMD ["npm", "start"]

CMD ["npm", "start"]Is a directive in the Dockerfile CMDthat is used to set the default command to be executed when the container starts. Here is a detailed explanation of the parameters of this command:

  • ["npm", "start"]: This is the default command to execute. In this example, npm startit's a common command used to launch a Node.js application.

To sum up, CMD ["npm", "start"]the function of the command is to set the default command to be executed when the container starts npm start. This means that when the container starts, it will automatically run npm startthe command to start the Node.js application. If other commands are provided when running the container, the default command will be overridden.

Steps and considerations for building an image

The steps to build an image are as follows:

  1. Create a new Dockerfile and define the image build process in it.
  2. Use the command in the terminal docker buildto build the image, specifying the path to the Dockerfile.

When building an image, you also need to pay attention to the following things:

  • Each instruction creates a new image layer, so try to put similar operations together to reduce the number of image layers.
  • Use appropriate caching mechanisms to avoid repeated downloading and installation of dependencies.
  • Try to use official base images, or verified and maintained images.

Example case

Here is a sample Dockerfile that demonstrates how to build an image of a simple Node.js application:

# 使用Node.js官方基础镜像
FROM node:14

# 设置工作目录
WORKDIR /app

# 复制项目文件到镜像中
COPY package.json .
COPY package-lock.json .

# 安装依赖
RUN npm install

# 复制应用代码到镜像中
COPY . .

# 暴露应用的端口
EXPOSE 3000

# 启动应用
CMD ["npm", "start"]

In this example, we first FROMspecify the base image as Node.js version 14 using the directive. Then use WORKDIRthe command to set the working directory to /appand copy the project files to the image. Next, use RUNthe command to install the dependencies, and then use COPYthe command to copy the application code to the image. Finally, use EXPOSEthe directive to declare the port of the application and use CMDthe directive to specify the command to start the application.

Summarize

Dockerfile is the key file that defines a Docker image. By writing a Dockerfile, we can define and build a customized Docker image to meet specific application needs. This article introduces the basic structure and common instructions of Dockerfile, and provides an example case to demonstrate how to build a simple Node.js application image.

If you want to further learn and understand more advanced usage and techniques of Dockerfile, you can refer to the following resources:

  • Docker official documentation: https://docs.docker.com/engine/reference/builder/
  • Dockerfile best practices: https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/132825328