如何为Spring Boot应用程序创建dockerfile

In this article, we’ll create a Spring Dockerfile. It is my goal to provide all the information you need to get started. We’ll cover what I discovered during a pet project of mine. In this project, I used Gradle and MongoDB to create a small application for the storage and retrieval of pictures.

在本文中,我们将创建一个Spring Dockerfile。 我的目标是提供入门所需的所有信息。 我们将介绍我在我的一个宠物项目中发现的内容。 在这个项目中,我使用GradleMongoDB创建了一个用于存储和检索图片的小型应用程序。

In this article, we’ll go over every line you need to create a Spring Dockerfile. We’ll stick with the basics, so don’t worry if you’re unfamiliar with this subject. At the end of this article, you’ll be ready to apply these techniques yourself. Having some expertise with Docker is a game-changer for any developer out there.

在本文中,我们将介绍创建Spring Dockerfile所需的每一行。 我们会坚持基础知识,因此如果您不熟悉此主题,请不要担心。 在本文的结尾,您将准备好自己应用这些技术。 对于Docker的开发者来说,拥有一些Docker专业知识可以改变游戏规则。

You read that correctly. Your job is not done with hacking around — even with the perfect test-driven mindset. To be a true craftsperson, you need to get your code into production. It has to work well with other services, it has to be stable, and it must be of value to your end-users. In our industry, they expect you to be familiar with containers. This alone should motivate you to navigate through this article.

您没看错。 即使拥有完美的测试驱动型思维,您的工作也不会随处可见。 要成为一名真正的工匠,您需要将代码投入生产。 它必须与其他服务一起很好地工作,必须稳定,并且必须对最终用户有价值。 在我们的行业中,他们希望您熟悉容器。 仅此一项就可以激发您浏览本文。

But don’t worry, it won’t take long. We’ll cover everything in just five minutes.

但是不用担心,它不会花很长时间。 我们将在五分钟内涵盖所有内容。

创建一个Spring应用程序 (Create a Spring Application)

Getting started with a Spring project is simple. There are so many guides out there. All you need in the beginning is to go to the Spring initializr website and pick the right dependencies.

Spring项目的入门很简单。 那里有很多指南。 在开始时,您所需要做的就是访问Spring initializr网站并选择正确的依赖项。

Junior programmers often get all too excited at this point. Don’t add dependencies you don’t need from the start. While it might be tempting to include fancy libraries (e.g. Lombok), you might not need them just yet (YAGNI). So try to add only the basics. Every dependency you add will also increase the size of your application.

初级程序员在这一点上常常感到非常兴奋。 从一开始就不要添加不需要的依赖项。 虽然可能很想包含精美的库(例如Lombok),但您可能还不需要它们( YAGNI )。 因此,请尝试仅添加基础知识。 您添加的每个依赖项也会增加应用程序的大小。

If you don’t have an application, go to spring.io and create one. If you don’t want to waste too much time developing, just create a hello world rest controller. It should be sufficient. Basically, I’d recommend going for Gradle or Maven. It shouldn’t matter that much. In this project, I went with Gradle.

如果您没有应用程序,请转到spring.io并创建一个。 如果您不想浪费太多时间进行开发,只需创建一个hello world rest控制器即可。 应该足够了。 基本上,我建议您选择Gradle或Maven。 没关系的。 在这个项目中,我和Gradle一起去了。

To run the application using Gradle:

要使用Gradle运行应用程序:

./gradlew bootRun

To run the application using Maven:

要使用Maven运行应用程序:

./mvnw spring-boot:run

To verify that your application is working correctly, have a quick look. If you’ve followed an online guide correctly, you should see a similar result running a basic curl command (GET request):

要验证您的应用程序是否正常运行,请快速浏览。 如果您正确地遵循了在线指南,则运行基本curl命令(GET请求)应该会看到类似的结果:

$ curl localhost:8080
Hey, I'm a hello world program (try this yourself)!

That’s it. The application is running! In the following section, we’ll make sure that it also works in every production environment. Your code might be cross-platform, but having Docker installed should be sufficient to run the program. That way, the code can really run anywhere. Well, on any computer or server with Docker, of course.

而已。 该应用程序正在运行! 在下一节中,我们将确保它也适用于每个生产环境。 您的代码可能是跨平台的,但是安装Docker应该足以运行该程序。 这样,代码实际上可以在任何地方运行。 好吧,当然,在任何使用Docker的计算机或服务器上。

创建一个Dockerfile (Create a Dockerfile)

In the previous section, we successfully built a Spring application. Recently, I did the same thing for a Java 14 application. The first instruction is related to this version. You see, we don’t have to start completely from scratch. We’ll use a small — therefore alpine — OpenJDK image for Java 14:

在上一节中,我们成功构建了一个Spring应用程序。 最近,我对Java 14应用程序做了同样的事情。 第一条说明与此版本有关。 您知道,我们不必从头开始。 我们将为Java 14使用一个小的(因此是高山的)OpenJDK映像:

FROM openjdk:14-jdk-alpine

Next, we’ll make sure we have a user group and a user for our application. Technically, you could continue without using the following two lines, but it would not be wise to do so (it’s just a best practice). The user inside the container will end up with all the needed permissions to run the application.

接下来,我们将确保我们有一个用户组和一个应用程序用户。 从技术上讲,您可以继续使用而不使用以下两行,但是这样做并不明智(这只是最佳实践)。 容器内的用户最终将具有运行该应用程序所需的所有权限。

Note: There is so much to learn about Docker. The permissions thing is an important one that I discovered while working with Jenkins. If you have any advice for me, please let me know in the comments!

注意:关于Docker有很多东西要学习。 权限是我与Jenkins合作时发现的重要内容。 如果您对我有任何建议,请在评论中让我知道!

RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring

The next line is essential. It copies the jar file from inside the build folder (a result of the Gradle build step) from the local machine into a Docker folder. For ease of use, let’s copy it to our spring user folder:

下一行至关重要。 它将jar文件从本地机器的build文件夹内复制(通过Gradle构建步骤)到Docker文件夹。 为了易于使用,让我们将其复制到我们的spring用户文件夹中:

COPY build/libs/*.jar home/spring/application.jar

Voila, we now have a jar file to work with. Two more lines and we’re done. Because there was no reason to change the port on which the application is running (it is running in Docker), I left it at 8080. The next line does nothing more than provide us with this information. Yes, that’s right. Don’t shoot me — I will specify the host port later (during the Docker run command)! You can see it as documentation.

瞧,我们现在有一个jar文件可以使用。 再加两行,我们就完成了。 因为没有理由更改应用程序运行的端口(它在Docker中运行),所以我将其保留在8080。下一行仅向我们提供了此信息。 是的,这是对的。 别开枪我—我稍后会指定主机端口(在Docker run命令期间)! 您可以将其视为文档。

EXPOSE 8080

The last line is the one that does the magic. We’ll run the application with the well-known java -jar command:

最后一行是做魔术的那一行。 我们将使用著名的java -jar命令运行该应用程序:

ENTRYPOINT ["java", "-jar", "/home/spring/application.jar"]

And that’s it. If you build and run this Docker container correctly, you’ll see the same result as in step 1.

就是这样。 如果正确构建并运行此Docker容器,您将看到与步骤1相同的结果。

OK, you’ve read this far, so it should be fair to give you those commands. I’ll include them for you:

好的,您已经阅读了到目前为止,因此给您这些命令应该是公平的。 我将为您包括它们:

  • docker build -t app/hi:v0.1.0

    docker build -t app/hi:v0.1.0

Project app with application name hi, feature version v0.1.0. I’ve made up a naming convention for versioning the builds, but feel free to use any versioning system you like. If you want to recommend one, that would be greatly appreciated.

项目应用 应用程序名称为hi,功能版本为v0.1.0。 我已经制定了用于对版本进行版本控制的命名约定,但是可以随时使用您喜欢的任何版本控制系统。 如果您想推荐一个,将不胜感激。

  • docker run app/hi:v0.1.0 -p 8080:8080

    docker run app/hi:v0.1.0 -p 8080:8080

Everyone with this Docker image will be able to run your application. From now on, it will work on every machine with Docker installed. Isn’t that awesome? Let’s provide it to the users and we’ll get instant feedback! Getting feedback early is very valuable!

拥有此Docker映像的每个人都将能够运行您的应用程序。 从现在开始,它将在安装了Docker的每台机器上运行。 那不是很棒吗? 让我们将其提供给用户,我们将立即获得反馈! 尽早获得反馈非常有价值!

奖励:MongoDB docker-compose示例 (Bonus: MongoDB docker-compose Example)

Setting up application containers, databases, and other parts of a larger application with commands can become a tedious job. I don’t like working with commands (if you don’t document them, good luck remembering all of them). There is an awesome solution for this called docker-compose. In this file, you can describe the parameters like ports, Dockerfiles to use, restart strategies, and much more.

使用命令设置应用程序容器,数据库和较大应用程序的其他部分可能会变得很繁琐。 我不喜欢使用命令(如果您不记录命令,请记住所有命令)。 有一个很棒的解决方案叫做docker-compose 。 在此文件中,您可以描述参数,例如端口,要使用的Dockerfile,重新启动策略等。

Let’s break down this example of a docker-compose file for a MongoDB application. Firstly, it spins up the MongoDB database. Afterward, it launches the application that handles the requests of storing and retrieving the images. The pet project is still in its early stages, so the configuration will most likely change in the future.

让我们分解一下MongoDB应用程序的docker-compose文件的示例。 首先,它启动了MongoDB数据库。 之后,它启动处理存储和检索图像请求的应用程序。 宠物项目仍处于初期阶段,因此将来很有可能会更改配置。

Feel free to ask any questions if you’re interested in this pet project. Suggestions are welcome too! It might be a topic for future articles, so stay tuned.

如果您对此宠物项目感兴趣,请随时提出任何问题。 也欢迎提出建议! 这可能是以后文章的主题,请继续关注。

version: '3.8'
services:
  image-store:
    build:
      context: ./application
      dockerfile: Dockerfile
    ports:
      - 8080:8080
    depends_on:
      - mongodb
    networks:
      - my-network


  mongodb:
    image: mongo:4.4.0
    ports:
      - 27017:27017
    volumes:
      - mongodb_data_container:/data/db
    networks:
      - my-network


volumes:
  mongodb_data_container:


networks:
  my-network:

结论 (Conclusion)

Well, that’s it. Thanks for reading all the way through! To conclude, I’ll sum up what we did in this article.

好,就是这样。 感谢您一直阅读! 最后,我将总结本文中的操作。

We created a small Spring application. It should be easy enough to create a Spring application with all those great guides out there. Always start with a tiny application.

我们创建了一个小的Spring应用程序。 创建带有所有出色指南的Spr​​ing应用程序应该足够容易。 始终从一个很小的应用程序开始。

Afterward, we learned how to wrap this application inside a container so it can be shared effectively with our coworkers. Nowadays, Docker is a must-have tool in your toolbox — period. In the current job market, there is a high demand for developers with knowledge of Docker and Kubernetes.

之后,我们学习了如何将此应用程序包装在容器中,以便可以与我们的同事有效共享。 如今,Docker已成为工具箱中的必备工具。 在当前的就业市场中,对具有Docker和Kubernetes知识的开发人员有很高的需求。

Finally, we saw a hint of what a slightly more mature application would look like. Docker-compose will keep frustrations away while setting up and maintaining containers. Start using it early in your development cycle.

最后,我们看到了稍微成熟一些的应用程序的外观。 Docker-compose将在设置和维护容器时消除挫败感。 在开发周期的早期开始使用它。

翻译自: https://medium.com/better-programming/how-to-create-a-dockerfile-for-a-spring-boot-application-46878997382f

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值