Talk about Docker and Dockerfile

Table of contents

I. Introduction

2. Understand Dockerfile

3. Dockerfile instructions

Four, multi-stage construction

5. Advanced usage of Dockerfile

6. Summary

I. Introduction

For developers, knowing Docker but not knowing Dockerfile means not knowing Docker. The last article took you to learn the basic usage of Docker: "One article takes you to learn Docker" . Today, I will take you to learn Dockerfile to help you get started quickly And create efficient Docker images.

2. Understand Dockerfile

A Dockerfile is a text file that defines the build process for a Docker image. It describes how to build a mirror in the form of instructions, gradually adding configuration, files and dependencies from the base mirror, and finally forming the mirror we need. Provides us with an easy and repeatable way to define the image build process.

3. Dockerfile instructions

  • FROM instruction:  The FROM instruction is the first instruction of the Dockerfile, which is used to specify the base image. Choosing the right base image is very important as it will directly affect the size and performance of the image. We can also take advantage of multi-stage builds to reduce image size.
  • RUN command:  The RUN command is used to execute commands during image building. With RUN, we can install packages, run scripts, and configure the environment.
  • COPY and ADD commands:  These two commands are used to copy local files into the image. The difference is that the ADD instruction supports automatic decompression and remote URLs, but the COPY instruction is recommended as it is more specific and predictable.
  • CMD and ENTRYPOINT directives:  These two directives are used to define the commands to be executed when the container starts. The commands defined by CMD can be overridden by the docker run command line parameters, while the commands defined by ENTRYPOINT will always be executed.

Here is an example of a simple Dockerfile:

# 使用 openjdk 镜像作为基础镜像
FROM openjdk:latest

# 设置工作目录
WORKDIR /app

# 复制 Java 项目的 JAR 文件到镜像中
COPY target/myapp.jar /app/

# 定义容器启动时执行的命令
CMD ["java", "-jar", "myapp.jar"]

In the above example, we used openjdk:latest as the base image and copied the Java project's JAR file into the image. Then, the command to be executed when the container starts is defined through the CMD command, that is, run java -jar myapp.jar to start the Java application.

Four, multi-stage construction

Multi-stage builds are a technique for optimizing the size of Docker images, especially for building compiled language applications such as Java projects. In a multi-stage build, we can define multiple FROM instructions in a Dockerfile, each instruction representing a build stage. The final image only retains the stage defined by the last FROM instruction, and other intermediate products will not be included in the final image, thereby reducing the size of the image.

Dockerfile example:

# 第一阶段:构建 Java 项目
FROM maven:latest AS builder

WORKDIR /app

COPY pom.xml .
RUN mvn dependency:go-offline

COPY src/ /app/src/
RUN mvn package

# 第二阶段:运行 Java 项目
FROM openjdk:latest

WORKDIR /app

COPY --from=builder /app/target/myapp.jar /app/

CMD ["java", "-jar", "myapp.jar"]

In the example above, we used two FROM directives:

FROM maven: latest AS builder means to build the Java project in the first stage, using Maven image for dependency installation and project construction;

FROM openjdk: latest indicates the second stage, using the OpenJDK image to run the Java project. With the COPY --from directive, we copy the built JAR files from the first-stage image to the second-stage, thereby reducing the size of the final image.

5. Advanced usage of Dockerfile

  • Using ARG and ENV: The ARG directive is used to pass arguments during the build process, while the ENV directive is used to set environment variables. Using these instructions, we can more flexibly customize the image building process.
  • Using WORKDIR: The WORKDIR directive is used to set the working directory, the default directory for running commands inside the container. This makes the Dockerfile easier to read and maintain.
  • Use VOLUME: The VOLUME command is used to create a mount point in the container, so that the data in the container can be persisted on the host.

Dockerfile example:

# 第一阶段:构建 Java 项目
FROM maven:latest AS builder

# 使用 ARG 指令传递构建参数
ARG APP_VERSION=1.0.0
ARG BUILD_ENV=production

# 设置工作目录
WORKDIR /app

# 复制 pom.xml 并安装项目依赖
COPY pom.xml .
RUN mvn dependency:go-offline

# 复制源代码并构建项目
COPY src/ /app/src/
RUN mvn package -DskipTests

# 第二阶段:运行 Java 项目
FROM openjdk:latest

# 使用 ENV 指令设置环境变量
ENV APP_PORT=8080
ENV BUILD_ENV=${BUILD_ENV}

# 使用 VOLUME 指令创建挂载点
VOLUME /app/logs

# 设置工作目录
WORKDIR /app

# 复制构建好的 JAR 文件到镜像中
COPY --from=builder /app/target/myapp-${APP_VERSION}.jar /app/

# 定义容器启动时执行的命令
CMD ["java", "-jar", "myapp-${APP_VERSION}.jar", "--port=${APP_PORT}", "--env=${BUILD_ENV}"]

In the above example, we first use the ARG directive to define the build parameters APP_VERSION and BUILD_ENV, and use the ARG directive in the FROM maven:latest AS builder phase to pass the build parameters.

In this way, specific values ​​can be passed through the --build-arg parameter when building, for example:

cssCopy code
docker build --build-arg APP_VERSION=2.0.0 --build-arg BUILD_ENV=staging -t my-java-app .

In this way, images of different versions and different environments can be built.

At the same time, we use the VOLUME command to create a mount point /app/logs, so that the log files in the container can be persistently saved on the host.

6. Low code deployed by Docker

The JNPF rapid development platform is a full-stack development platform based on SpringBoot+Vue3, which meets the needs of micro-services, front-end and back-end separation architectures, and quickly builds business applications based on visual process modeling, form modeling, and report modeling tools. The platform can be deployed locally and also supports K8S deployment. Free trial address: https://www.jnpfsoft.com/?csdn

In addition to the above-mentioned functions, it is also equipped with visual function engines such as chart engine, interface engine, portal engine, and organizational user engine to basically realize the visual construction of the page UI. There are hundreds of functional controls and templates built in, so that it can meet the personalized needs of users to the greatest extent under the simple operation of dragging and dropping.

Through the visual drag-and-drop method, it takes only 2 hours to complete the development of an application in the traditional mode with JNPF. To build an application with JNPF, you only need to pay attention to the business itself, data storage, operating environment, server, network security, etc., and the platform will handle it all for you.

7. Summary

Dockerfile is the core tool for building Docker images, which makes the image building process simple, repeatable and efficient. Through the introduction of this article, you have learned the basic syntax and common instructions of Dockerfile, as well as some best practices. With your practice and in-depth study, I believe you will be able to create better Docker images and better apply Docker in software development and deployment.

Guess you like

Origin blog.csdn.net/wangonik_l/article/details/132097570