Get the App
SLTechnology News&Howtos  ›  Servers  › 

The method of deploying spring-boot maven applications using Docker

Shulou Source: shulou.com Published: 2022-06-02 03:14:31 10月01日 Update

This article introduces the deployment of spring-boot maven applications using Docker and shares them with you. The details are as follows:

The deployment process is divided into the following steps:

Create a simple spring-boot application package run application containerization application add docker support to create docker image run docker container view running container startup / shutdown / restart / delete docker container

1. Create a simple spring-boot application

In IntelliJ IDEA, File- > New- > Project:

Then click Next- > Finish.

two。 Package and run the application

Add dependencies to pom.xml:

Org.springframework.boot spring-boot-starter-web

Add a controller:

Controllerpublic class HelloWorld {@ GetMapping ("/ hello") public void helloworld (HttpServletResponse response) throws IOException {response.getWriter () .write ("Hello Spring-boot");}}

Run the project, visit: http://localhost:8080/hello, if present: Hello Spring-boot step 2 completed.

3. Containerized application

Create a Dockerfile file in the project directory: src/main/docker/Dockerfile (no suffix), as follows:

FROM frolvlad/alpine-oraclejdk8:slimVOLUME / tmpADD demo-0.0.1-SNAPSHOT.jar app.jarRUN sh-c 'touch / app.jar'ENV JAVA_OPTS= "" ENTRYPOINT ["sh", "- c", "java $JAVA_OPTS-Djava.security.egd=file:/dev/./urandom-jar / app.jar"]

Where demo-0.0.1-SNAPSHOT.jar is the file name in the packaged / target/ of the project

4. Add docker support to pom files

Ramer com.spotify docker-maven-plugin 0.4.11 ${docker.image.prefix} / ${project.artifactId} src/main/docker / ${project.build.directory} ${project.build.finalName} .jar

5. Create a docker image

Cmd goes to the current directory:

Cd Z:/Desktop/springboot-demo

Package and create an image:

Mvn package-Dmaven.test.skip=true docker:build

Note: please make sure that maven has been added to path; and that docker is installed, if not, visit: https://www.docker.com/community-edition#/download to download the appropriate version.

6. Run the docker container

# # it's best to add-- name parameter docker run-- name=springboot-docker-demo-p 808080 name=springboot-docker-demo 8080-t ramer/demo

Visit from the browser: http://localhost:8080/hello

7. View running containers

Cmd: docker ps

8. Start / shut down / restart / delete the docker container

Cmd: docker start/stop/restart/rm CONTAINER_ID/NAME

Where:

CONTAINER_ID: container id, which can be viewed by executing docker ps

NAME: is the name of the container, which is the name after docker run-name

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Tags: Container application run file image project support content being directory appropriate that is parameters name name suffix that is controller file name best Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info Shulou Technology OPPO Reno Huawei