本文源自
许多Java开发人员熟悉通过jar文件和应用程序容器(比如Tomcat)布署Java应用程序,而且许多组织如今正在使用Docker容器布署应用程序。让我们看一下让Java应用程序在Docker容器中运行的最低限度的步骤。
安装Docker
为了在容器中运行您的java应用程序,您将须要Docker。安装后Docker将容许您在本地计算机上建立和运行容器。它可以在许多平台上运行,包括Linux、MacOS和Windows。抵达Docker网站并查找有关支持的平台的信息。
安装后,您可以通过建立和运行hello-world容器来测试您的安装。下边的输出表明您的安装成功。
: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/
为您的Java应用程序创建Dockerfile
如今您早已安装了Docker,让我们使用Gradle建立的简单Java应用程序并创建一个Dockerfile。我们将在容器本身中建立和运行jar文件,这为我们提供了一个更加一致的环境。你可以在Github上查看我的示例项目,假如你乐意,可以从这个项目开始工作。
如今我们早已有了基本的Java应用程序docker部署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"]
在Docker容器中建立并运行您的Java应用程序
如今我们早已创建了Dockerfilelinux更改ip地址,我们可以要求docker首先建立,之后使用以下命令运行我们的容器。
docker build -t zsiegel:java-and-docker .
docker run zsiegel:java-and-docker
成功
如今您早已在Docker容器中成功建立并运行了一个java应用程序linux软件下载,您可以开始浏览相关文章。那些将帮助您了解java运行时和您的应用程序的行为取决于您设置的JVM版本和容器标志。这很重要docker部署java应用,由于在容器中运行java并不像看上去这么简单。