In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
In this article, I'll show you how to develop and run simple Spring Web applications using Java 8 without having to install Java 8 on your local computer.
Python developers use virtual environments to create and manage separate environments for different projects, each using a different version of Python to execute, store, and parse Python dependencies. Java and many other technologies do not support the concept of virtual environment. Docker comes to help us at this point.
Docker is a virtualization platform. You can find basic information and installation guides on the official Docker website.
Once the Docker toolkit is installed, there is no need to install Java 8 or MySQL needed in our sample application.
First, let's examine the Docker-compose file:
Version: '2'services: springappserver: build: context:. Dockerfile: springapp.dockerfile ports:-"8080 net-spring-db volumes: -.: / vol/development depends_on:-mysqldbserver mysqldbserver: build: context:. Dockerfile: mysqldb.dockerfile ports:-"3306 net-spring-db 3306" networks:-net-spring-db environment: MYSQL_DATABASE: testdb MYSQL_USER: myuser MYSQL_PASSWORD: mypassword MYSQL_ROOT_PASSWORD: myrootpassword container_name: mysqldbservernetworks: net-spring-db: driver: bridge
We have two servers each on net-spring-db'. The first one is named 'springappserver', and configured with springapp.dockerfile. The second one is named mysqldbserver and configured using mysqldb.dockerfile.
Now, let's take a look at springapp.dockerfile:
# # Java 1.8 & Maven Dockerfile### pull base image.FROM java:8# maintainerMAINTAINER Dursun KOC "dursunkoc@gmail.com" # update packages and install mavenRUN\ export DEBIAN_FRONTEND=noninteractive & &\ sed-I's multiverse$ # (. * multiverse$\) /\ 1Universe g'/ etc/apt/sources.list & &\ apt-get update & &\ apt-get-y upgrade & &\ apt-get install-y vim wget curl maven# attach volumesVOLUME / vol/development# create working directoryRUN mkdir -p / vol/developmentWORKDIR / vol/development# maven execCMD ["mvn" "clean", "package", "exec:java"]
The Docker file configures the Docker image, which inherits from the Java 8 image of Docker Hub. On the Java 8 image, I installed vim,wget,curl,Maven and set up the volume to place my existing project code. Finally, execute the Maven command to run my application.
Now let's check the mysqldb.dockerfile:
FROM mysql/mysql-serverMAINTAINER Dursun KOC # Copy the database initialize script: # Contents of / docker-entrypoint-initdb.d are run on mysqld startupADD mysql/ / docker-entrypoint-initdb.d/
The Docker file configures the Docker mirror, which inherits from the MySQL / mysql-server image of Docker Hub. On the MySQL image, I placed my db-schema creation scripts, which are located in the MySQL folder. I have a SQL file-data.sql-in this folder to create the 'person' table.
Now, let's look at the application structure.
Our application starts with the src / com / turkcell / softlab / Application.java file, and our only Controller is PersonController (src / com / turkcell / softlab / controller / PersonController.java).
You can run the entire project with a simple command:
Docker-compose up-d
When testing, use the following two commands on the local computer:
Create a new person:
Curl-H "Content-Type:application / json"-X POST-d "{\" first\ ":\" Mustafa\ ",\" last\ ":\" KO c\ ",\" dateofbirth\ "381110400000," placeofbirth ":\" Erzincan\ "}" http://192.168.99.100 http://192.168.99.100 8080max people "
List the existing people in the database:
Curl-H "Content-Type:application / json"-X GET "http://192.168.99.100:8080/people"
Summary
The above is the editor to introduce to you the method of developing Java 8 Spring Boot application in Docker. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to the website!
If you think this article is helpful to you, you are welcome to reprint it, please indicate the source, thank you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.