1. Create a SpringBoot project

  • For the version, select 2.5.2. The corresponding mode varies with different versions
  • Select Spring Web and Spring Boot Actuator components

Click Finish

2. Create an interface

We will write a Restful interface under the startup class for later testing

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DockerDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DockerDemoApplication.class, args);
    }

    @GetMapping("/demo")
    public String dockerDemo(a){
        return "docker success"; }}Copy the code

3. Modify the POM. XML file

<build> <! <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <! Executions --> <layers> <enabled>true</enabled> </layers> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>Copy the code

4. Compile a Dockerfile file

FROM Java :8 ENV SERVER_PORT 8999 ## docker-demo.jar finalName ADD target/docker-demo.jar app.jar ENTRYPOINT  ["java", "-jar","/app.jar"]Copy the code

5. Upload the image

docker build -t docker-demo .
Copy the code

6. Rename the mirror

Docker Tag IMAGEID: REPOSITORYCopy the code

7. To run

docker run -itd --name docker-demo -p 8999:8999 docker-demo
Copy the code

8. Browser access