如何在Docker中部署Java应用

本文介绍如何使用Docker容器部署Java应用程序,包括安装Docker、创建Dockerfile以及构建和运行容器的基本步骤。通过示例项目,展示了如何利用Gradle构建工具在Docker容器内直接构建和运行Java应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This article originated on zsiegel.com

Many java developers are familiar with deploying java applications via jar files and application containers such as Tomcat however many organizations are now deploying applications with Docker containers. Lets take a look at the bare minimum steps to get a java application running in a Docker container.

Install Docker

In order to run your java app in a container your are going to need Docker. Once installed Docker will allow you to build and run your containers on your local machine. It runs on many platforms including Linux, MacOS, and Windows. Head over to the Docker website and look for information on the supported platforms.

安装完成后,您可以通过构建并运行你好,世界容器。 以下输出表明您的安装成功。

▲ :zsiegel (master)$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete 
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Create a Dockerfile for your Java app

Now that you have Docker installed lets take a simple Java app built by Gradle and create a Dockerfile. We are going to both build and run the jar file within the container itself which gives us a more consistent environment. You can checkout my sample project on Github and work from this project if you would like.

现在我们有了基本的Java应用程序,让我们看一下Dockerfile的外观。

# NOTE: This is not a production ready Dockerfile. 
# Utilize this only for development purposes

# Use a container image that has both Gradle and the JDK
FROM gradle:5.0.0-jdk8-alpine

# Switch to the `gradle` user defined by our container image
USER gradle

# Copy over the project directory into the container
COPY --chown=gradle:gradle . /java-and-docker

# Set our working directory to our project directory that we set above
WORKDIR /java-and-docker

# Run the build
RUN gradle build

# Run the jar file
# Since we are using JDK8 we set some additional flags to be more container aware
CMD ["java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-jar", "build/libs/java-and-docker-1.0.jar"]

Build and Run your Java app in the Docker container

现在我们已经创建了Dockerfile,我们可以要求Docker首先构建,然后使用以下命令运行我们的容器。

docker build -t zsiegel:java-and-docker .
docker run zsiegel:java-and-docker 

Success

既然您已经成功在Docker容器中构建并运行了Java应用程序,则可以开始浏览相关文章。 这些将帮助您了解Java运行时和应用程序的行为,具体取决于JVM版本和您设置的容器标志。 这很重要,因为在容器中运行Java并不像看起来那样简单。

from: https://dev.to//zac_siegel/how-to-deploy-java-apps-in-docker--15h

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值