How to build Docker + Springboot app (for basic learning)

Introduction

Recently, I haven't had the opportunity to build the source from scratch for maintenance and document creation, and I tend to forget about the basics.

At that time, I happened to watch this video.

How to create Docker Image and run Java App (Spring Boot Jar) in a Docker Engine | Tech Primers(2017) https://www.youtube.com/watch?v=FlSup_eelYE

The content of the video is very easy to understand, and by the way, I remembered that I had never made it from scratch, so I carried out construction work for studying.

Construction requirements

Make the Springboot app run on the Docker container. Make it accessible from the outside with the Rest API.

DockerDesktop setup

Allow Docker to run in a windows environment.

Installer

Can be downloaded from this official https://hub.docker.com/editions/community/docker-ce-desktop-windows Start [Docker Desktop Installer.exe] and follow the procedure

Start Docker

Start by rebooting after the installer

ProxySetUp (if needed)

Add proxies and below to the ~ / .docker / config.json file.

{
  "credsStore": "desktop",
  "stackOrchestrator": "swarm",
  "credStore": "desktop",
  "proxies":
	 {
	   "default":
	   {
	     "httpProxy": "http://127.0.0.1:3001",
	     "httpsProxy": "http://127.0.0.1:3001",
	     "noProxy": "*.test.example.com,.example2.com"
	   }
	 }
}
https://docs.docker.com/network/proxy/

Springboot initial build

○ Create a Spring boot Project https://start.spring.io/

Added Rest Repository to Dependency to create RestAPI.

  • Add a library that suits your purpose.

image.png

After making the settings, press the GENERATE button to download the ZIP file.

Place locally

image.png

Create sample module for Rest

Create a controller package under poc.dockerspringboot and create a sample file


package poc.dockerspringboot.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rest/message/helloWorldOnDocker")
public class HelloWorldOnDocker {

    @GetMapping
    public String hello(){
        return "Hello from Docker Container";
    }
}

Perform maven build

Perform mvn clean install image.png

Docker File setup

Creating a Docker File

Create a Dockerfile directly under the project

image.png

Creation example

FROM adoptopenjdk:11-jre-openj9
ADD target/docker-springboot.jar docker-springboot.jar
EXPOSE 8081
ENTRYPOINT ["java", "-jar", "docker-springboot.jar"]

FROM: Specify base image (Java)

In OpenJDK, use adoptopenjdk that can be used for commercial purposes. https://hub.docker.com/_/adoptopenjdk

ADD: Add module

Add the generated module to Docker. A module is created under target, but since the file name of jar is long, specify the output name with [finalName].

<build>
	<finalName>docker-springboot</finalName>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

Reference: Spring-boot-maven-plugin setting information https://spring.pleiades.io/spring-boot/docs/current/maven-plugin/reference/html/

EXPOSE: Port for external disclosure

Specify the port you want to open in the Docker container

Inside the container, the default port when spring-boot starts is 8080 If you want to change this value, add server.port = 8082 etc. to application.properties

When Docker is started, the port that is started internally and the port that is open to the outside are linked.

ENTRYPOINT: Start point

Start the module with the jar command. By executing this command, the embedded server (Tomcat) will start and accept access.

Container startup

#Image creation
docker build -f Dockerfile -t docker-spring-boot .

#Confirmation of creation
docker images

#Container startup
docker run -p 8081:8080 docker-spring-boot


Access confirmation

http://localhost:8081/rest/message/helloWorldOnDocker image.png

finally

There are no new technical discoveries, but occasionally doing this kind of work clears your thinking. Described as a guideline so that it can be transferred later.

Recommended Posts

How to build Docker + Springboot app (for basic learning)
How to build docker environment with Gradle for intelliJ
How to build CloudStack using Docker
How to build Rails 6 environment with Docker
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to use nginx-ingress-controller with Docker for Mac
[Rails] How to build an environment with Docker
3 Tips to Improve Lead Time for Docker Build
How to install Docker
How to build android-midi-lib
How to run the SpringBoot app as a service
How to make Laravel faster with Docker for Mac
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
How to introduce Basic authentication
How to set Docker nginx
Let's write how to make API with SpringBoot + Docker from 0
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
How to build Rails, Postgres, ElasticSearch development environment with Docker
Introduction to Programming for College Students: How to Draw Basic Rectangle
[App development 0.5] [Node.js express Docker] Build an environment for Node.js Express MongoDB using Docker
How to specify validation for time_field
How to install JMeter for Mac
How to run JavaFX on Docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
Articles referred to for incorporating Docker
[Artifactory] How to use Docker repository
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
How to build vim on Ubuntu 20.04
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
How to start Camunda with Docker
[For those who create portfolios] How to use binding.pry with Docker
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
How to build an environment for any version of Ruby using rbenv
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
[Docker] How to build when the source code is bind-mounted on the container
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
How to share files with Docker Toolbox
How to change app name in rails
How to install Play Framework 2.6 for Mac
[Rails] How to use rails console with docker
[Basic] How to write a Dockerfile Self-learning ②
Don't know how to build task'credentials: edit'
Precautions when making Docker for deep learning
How to create a Maven repository for 2020
How to use Docker in VSCode DevContainer
How to build SquashTM and how to support Japanese
[Ruby] How to use slice for beginners
Docker container build fails to install php-radis
Needed for iOS 14? How to set NSUserTrackingUsageDescription
Understand in 5 minutes !! How to use Docker
Note: [Docker] How to start and stop
[SpringBoot] How to write a controller test
How to run Blazor (C #) with Docker
[For beginners] How to debug in Eclipse
[App development 1] [Node.js express Docker] Build an environment for Node.js Express MongoDB (mongoose) using Docker [December 2020]
How to display the amount of disk used by Docker container for each container
Thinking about how to use Ionic-Native, Hybrid and HTML5 for mobile app production
[Error resolution] Occurs when trying to build an environment for spring with docker
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
How to start with Hyper-V instead of WSL2 on Docker Desktop for Windows
[Introduction to Docker] Create a Docker image for machine learning and use Jupyter notebook