Skip to content

Instantly share code, notes, and snippets.

@freddie-freeloader
Created November 7, 2024 11:58
Show Gist options
  • Save freddie-freeloader/e3b76b24fa58fc304c67606d9e734668 to your computer and use it in GitHub Desktop.
Save freddie-freeloader/e3b76b24fa58fc304c67606d9e734668 to your computer and use it in GitHub Desktop.

Spring docker notes

  • There is a guide for Spring & Docker here
    • mvn spring-boot:build-image -Dspring-boot.build-image.imageName=xdb/rest
      • Creates an image, that then could be started with docker compose

Debug app in docker container

source

To debug the application, you can use JPDA Transport. We treat the container like a remote server. To enable this feature, pass Java agent settings in the JAVA_OPTS variable and map the agent’s port to localhost during a container run. With Docker for Mac, there is a limitation because we can’t access the container by IP without black magic usage.

docker run -e "JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n" -p 8080:8080 -p 5005:5005 -t springio/gs-spring-boot-docker

Manual start

  • tl;dr:
    • mvn spring-boot:run
    • or: mvn package and then java -jar target/xdb-rest.jar
      • Note: For docker containers, they copy the jar into the image & then run it
  • See here on how a simple rest BE is run
    • Relevant part is probably under Run the service

SpringBootApplication is a convenience annotation that adds all of the following:

  • @Configuration: Tags the class as a source of bean definitions for the application context.

  • @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if spring-webmvc is on the classpath, this annotation flags the application as a web application and activates key behaviors, such as setting up a DispatcherServlet.

  • @ComponentScan: Tells Spring to look for other components, configurations, and services in the com/example package, letting it find the controllers.

The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there was not a single line of XML? There is no web.xml file, either. This web application is 100% pure Java and you did not have to deal with configuring any plumbing or infrastructure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment