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

Getting Started Tutorial for Beginners ⑨: How to Build a Portal Website

Moreover, an article website built with a blog pro...

Teach you MySQL query optimization analysis tutorial step by step

Preface MySQL is a relational database with stron...

An article to help you learn CSS3 picture borders

Using the CSS3 border-image property, you can set...

Introduction to common MySQL storage engines and parameter setting and tuning

MyISAM, a commonly used storage engine in MySQL c...

Summary of MySQL view principles and usage examples

This article summarizes the principles and usage ...

Solution to data duplication when using limit+order by in MySql paging

Table of contents summary Problem Description Ana...

CSS HACK for IE6/IE7/IE8/IE9/FF (summary)

Since I installed the official version of IE8.0, ...

Implementing access control and connection restriction based on Nginx

Preface Nginx 's built-in module supports lim...

Design a simple HTML login interface using CSS style

login.html part: <!DOCTYPE html> <html l...

Creating a file system for ARM development board under Linux

1. Please download the Busybox source code online...

How to insert batch data into MySQL database under Node.js

In the project (nodejs), multiple data need to be...

Basic concepts and common methods of Map mapping in ECMAScript6

Table of contents What is a Mapping Difference be...

How to install a virtual machine with Windows services on Mac

1. Download the virtual machine Official download...

Nginx stream configuration proxy (Nginx TCP/UDP load balancing)

Prelude We all know that nginx is an excellent re...

MySQL independent index and joint index selection

There is often a lack of understanding of multi-c...