How to start a Java program in docker

How to start a Java program in docker

Create a simple Spring boot web project

Use the idea tool to create a Spring boot web project. Since it is a test, just click next.




Write a test API for access. The service port number does not need to be changed. I changed it to 8701 locally.

When the program starts, it is found that the program is not the default port 8080. Visit: http://localhost:8701/v1/hello


The above simple web project is built. Now let's run this demo project through docker.

The first step is to install Docker (detailed steps are not given here).

In the second step, we need a docker image with a java environment. I downloaded one from the NetEase Cloud Mirror Center, address: https://c.163yun.com/hub#/library/repository/info?repoId=65430. You can also find the corresponding image with a Java environment from Alibaba Cloud and other platforms to make it.

Get the image and pull it locally

docker pull hub.c.163.com/housan993/centos7_jdk8:latest

In the third step, we will package the demo project into a jar package and use mvn install. For convenience, I will directly get the generated jar from target to the project root directory.

In the fourth step, we write a Dockerfile file under the project to create a mirror of the demo project.
Execute the commands in the dockerfile file from Baidu

Dockerfile file content:
FROM hub.c.163.com/housan993/centos7_jdk8:latest
COPY demo-0.0.1-SNAPSHOT.jar /
CMD java -jar demo-0.0.1-SNAPSHOT.jar

After writing the Dockerfile file, we use the Docker command to build an image. The docker command will automatically find the Dockerfile file in the current directory (the default file name is Dockerfile), and then specify the directory path. "." indicates the current

docker build -t demo-img .

If you see the following log, the image is created successfully.

In the fifth step, we start the container of our program according to the created image, and map the port (8701) to 8701 of the local machine.

docker run -d -p 8701:8701 demo-image

After running, we will get a long string of characters, which is the CONTAINER ID of the container. Let's docker ps and see

Let's take a look at the container startup log to see if our Java program is running.

docker logs [CONTAINER ID] 

From the container log, we found that it is exactly the same as the log of starting a Java program locally, so I will access the demo program through the mapped port to see if it works.

Let's stop the demo container and see if we can still access it.
Stop container command: docker stop [CONTAINER ID]
No access. . .

Let's start our demo container again
docker start [CONTAINER ID]
It is accessible again. So far, we have completed the simple use of running Java programs through Docker.

The above is just a simple use of Docker to run Java programs. If we are interested in the future, we can package, build and other commands into shell scripts, automatically generate container versions, and dynamically take values ​​of parameter variables. I can only say that container talk is very fun, and it will be very convenient for automated deployment of your own projects in the future. The k8s container orchestration tool will be introduced later, which will be even more interesting.

This is the end of this article about the steps to start a Java program with docker. For more information about starting a Java program with docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of how to use Docker to build a simple Java development and compilation environment
  • How to develop Java 8 Spring Boot applications in Docker
  • Steps to build a Java environment using Docker
  • How to use Docker, a Java data development auxiliary tool, and ordinary programs

<<:  Method of dynamically loading geojson based on Vue+Openlayer

>>:  MySQL code execution structure example analysis [sequence, branch, loop structure]

Recommend

Implementation of effective user groups and initial user groups in Linux

First check the /etc/group file: [root@localhost ...

6 Practical Tips for TypeScript Development

Table of contents 1. Determine the entity type be...

Docker installation steps for Redmine

Download the image (optional step, if omitted, it...

5 tips for writing CSS to make your style more standardized

1. Arrange CSS in alphabetical order Not in alphab...

Detailed explanation of incompatible changes of components in vue3

Table of contents Functional Components How to wr...

MySQL multi-master and one-slave data backup method tutorial

Overview Operations on any one database are autom...

CentOS 6.4 MySQL 5.7.18 installation and configuration method graphic tutorial

The specific steps of installing mysql5.7.18 unde...

Ubuntu installation graphics driver and cuda tutorial

Table of contents 1. Uninstall the original drive...

Linux Disk Quota Management Graphical Example

Disk quota is the storage limit of a specified di...

WeChat applet implements simple chat room

This article shares the specific code of the WeCh...

Practical way to build selenium grid distributed environment with docker

Recently, I needed to test the zoom video confere...

Docker nginx + https subdomain configuration detailed tutorial

Today I happened to be helping a friend move his ...