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并不像看起来那样简单。