在 Java EE 应用程序开发中使用 Docker [英] Using Docker in development for Java EE applications

查看:23
本文介绍了在 Java EE 应用程序开发中使用 Docker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会加 300 点作为赏金

我最近开始仔细研究 Docker 以及如何使用它来更快地让团队的新成员启动并运行开发环境,以及将新版本的软件交付到生产环境中.

I have recently started to take a closer look at Docker and how I can use it for faster getting new member of the team up and running with the development environment as well as shipping new versions of the software to production.

我对如何以及在哪个阶段将 Java EE 应用程序添加到容器有一些疑问.在我看来,有多种方法可以做到这一点.

I have some questions regarding how and at what stage I should add the Java EE application to the container. As I see it there are multiple ways of doing this.

这是 Docker 之前的典型工作流程(在我的团队中):

This WAS the typical workflow (in my team) before Docker:

  1. 开发人员编写代码
  2. 开发人员使用 Maven 构建代码,生成 WAR
  3. 开发人员在 JBoss 管理控制台/或使用 Maven 插件上传 WAR

现在在 Docker 出现之后,我有点困惑是否应该创建我需要的图像并配置它们,以便在运行 JBoss Wildfly 容器时剩下要做的就是通过管理控制台部署应用程序在网上.或者我应该为每次在 Maven 中构建应用程序时创建一个新容器,并在 Dockerfile 中使用 ADD 命令添加它,然后只运行容器而不在启动后部署到它?

Now after Docker came around I am a little confused about if I should create the images that I need and configure them so that all that is left to do when you run the JBoss Wildfly container is to deploy the application through the admin console on the web. Or should I create a new container for each time I build the application in Maven and add it with the ADD command in the Dockerfile and then just run the container without ever deploying to it once it is started?

在生产中,我猜最后一种方法是它喜欢的方法吗?如果我错了,请纠正我.但是在开发中应该怎么做呢?还有其他工作流程吗?

In production I guess the last approach is what it preffered? Correct me if I am wrong. But in development how should it be done? Are there other workflows?

推荐答案

借助最新版本的 Docker,您可以使用 Docker Links、Docker Volume 和 Docker Compose 轻松实现这一目标.来自 Docker 站点的有关这些工具的更多信息.

With the latest version of Docker, you can achieve that easily with Docker Links, Docker Volume and Docker Compose. More information about these tools from Docker site.

回到您刚才提到的工作流程:对于任何典型的 Java EE 应用程序,都需要一个应用程序服务器和一个数据库服务器.由于您在帖子中没有提到数据库是如何设置的,我假设您的开发环境将为每个开发人员提供单独的数据库服务器.

Back to your workflow as you have mentioned: for any typical Java EE application, an application server and a database server are required. Since you do not mention in your post how the database is set up, I would assume that your development environment will have separated database server for each developer.

考虑到所有这些,我可以建议以下工作流程:

Taking all these into assumption, I could suggest the following workflow:

  • 从官方镜像构建基础 Wildfly 应用服务器.您可以通过以下方式实现:docker pull"命令
  • 运行基础应用服务器:

docker run -d -it -p 8080:8080 -p 9990:9990 --name baseWildflyjboss/wildfly

docker run -d -it -p 8080:8080 -p 9990:9990 --name baseWildfly jboss/wildfly

应用程序服务器现在正在运行,您需要对其进行配置以连接到您的数据库服务器,并在必要时配置数据源设置和其他配置,以便启动您的 Java EE 应用程序.为此,您需要登录 Jboss 容器的 bash 终端:

The application server is running now, you need to configure it to connect to your database server and also configure the datasource settings and other configuration if neccessary in order to start your Java EE application. For this, you need to log into bash terminal of the Jboss container:

docker exec -i -t baseWildfly/bin/bash/

docker exec -i -t baseWildfly /bin/bash/

您现在位于容器的终端中.您可以像在任何 Linux 环境中一样配置应用服务器.

You are now in the terminal of container. You can configure the application server as you do for any linux environment.

您可以通过手动将 WAR 文件部署到 Wildfly 来测试配置.如您所说,这可以通过管理控制台、maven 插件或 ADD 命令轻松完成.我通常用管理控制台来做这件事,只是为了快速测试.当您验证配置有效时,您可以删除 WAR 文件并创建容器的快照:

You can test the configuration by manually deploying the WAR file to Wildfly. This can be done easily with the admin console, or maven plugin, or ADD command as you said. I usually do that with admin console, just for testing quickly. When you verify that the configuration works, you can remove the WAR file and create a snapshot of your container:

docker commit --change "添加基本设置和配置"baseWildfly yourRepository:tag

docker commit --change "add base settings and configurations" baseWildfly yourRepository:tag

您现在可以将创建的映像推送到您的私有存储库并与您的开发团队共享.他们现在可以拉取镜像并立即运行应用服务器进行部署.

You can now push the created image to your private repository and share that with your developer team. They can now pull the image and run the application server to deploy right away.

我们不想使用管理控制台为每个 Maven 构建部署 WAR 文件,因为这太麻烦了,所以下一个任务是使用 Docker Volume 自动化.

We don't want to deploy the WAR file for every Maven build using admin console as that is too cumbersome, so next task is to automate it with Docker Volume.

假设您已经配置了 Maven 将 WAR 文件构建到../your_project/deployments/",您可以将其链接到 Jboss 容器的部署目录,如下所示:

Assuming that you have configured Maven to build the WAR file to "../your_project/deployments/", you can link that to deployment directory of Jboss container as following:

docker run -d -p 8080:8080 -v../your_project/deployments:/opt/jboss/wildfly/standalone/deployments

docker run -d -p 8080:8080 -v ../your_project/deployments:/opt/jboss/wildfly/standalone/deployments

现在,每次您使用 Maven 重建应用程序时,应用程序服务器都会扫描更改并重新部署您的 WAR 文件.

Now, every time you rebuild the application with Maven, the application server will scan for changes and redeploy your WAR file.

为每个开发人员设置单独的数据库服务器也很成问题,因为他们必须在容器中自己配置它,因为他们可能有不同的设置(例如 db 的 url、用户名、密码等...).所以,最终将其 dockerize 是件好事.

It is also quite problematic to have separated database server for each developer, as they have to configure it by themselves in the container because they might have different settings (e.g. db's url, username, password, etc...). So, it's good to dockerize that eventually.

假设你使用 Postgres 作为你的数据库服务器,你可以从 postgres 官方存储库中提取它.准备好图像后,您可以运行数据库服务器:

Assuming you use Postgres as your db server, you can pull it from postgres official repository. When you have the image ready, you can run the db server:

docker run -d -p 5432:5432 -t --name postgresDB postgres

docker run -d -p 5432:5432 -t --name postgresDB postgres

或使用链接的data"目录运行数据库服务器:

or run the database server with the linked "data" directory:

docker run -d -p 5432:5432 -v../your_postgres/data:/var/lib/postgresql -t --name postgresDBpostgres

docker run -d -p 5432:5432 -v ../your_postgres/data:/var/lib/postgresql -t --name postgresDB postgres

第一个命令会将您的数据保存在容器中,而后一个命令会将您的数据保存在主机环境中.

The first command will keep your data in the container, while the latter one will keep your data in the host env.

现在您可以将您的数据库容器与 Wildfly 链接起来:

Now you can link your database container with the Wildfly:

docker run -d -p 8080:8080 --link postgresDB:database -t baseWildfly

docker run -d -p 8080:8080 --link postgresDB:database -t baseWildfly

以下是链接的输出:

现在,您可以为开发人员团队的所有成员提供相同的环境,他们只需进行最少的设置即可开始编码.

Now you can have the same environment for all members in developer's team and they can start coding with minimal set up.

生产环境可以使用相同的基础镜像,这样每当你想发布新版本时,你只需要将WAR文件复制到主机的your_deployment"文件夹中即可.

The same base images can be used for Production environment, so that whenever you want to release new version, you just need to copy the WAR file to "your_deployment" folder of the host.

将应用服务器和数据库服务器 dockerizing 的好处在于,您可以在将来轻松地将其集群以进行扩展或应用高可用性.

The good thing of dockerizing application server and db server is that you can cluster it easily in the future to scale it or to apply the High Availability.

这篇关于在 Java EE 应用程序开发中使用 Docker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆