Docker Learning-Chapter 6 Deploying Springboot Project in Docker (Windows)

First, create a springboot project

Create a springboot project spring-boot-docker

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

HelloController.java

@RestController
public class HelloController {
    @GetMapping("/docker")
    public String hello() {
        return "Hello Docker!!";
    }
}

Write Dockerfile and place it under src / docker directory

FROM java:8
VOLUME /tmp
ADD spring-boot-docker-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

Second, deploy the project to Docker

1. Prepare documents

Execute mvn command to package springboot project

mvn install

Copy the jar package and Dockerfile to a certain folder

2. Build the image file

cdTo the directory where the jar package and Dockerfile are stored

Execute image build command

docker build -t spring-boot-docker .

-tRepresents the name and label of the image to be built, usually in the format name: tag or name, if no tag is declared, the default is latest

.Represents the current directory, which is the directory where the Dockerfile is located

After running, check the images, you can see that the image has been built successfully

docker images

3. Run the project

Run mirror

docker run -p 8080:8080 -d spring-boot-docker

-pIndicates port mapping, the left side of the colon is the port number outside the Docker container, and the right side is the port number inside the container

-dIndicates running in the background

View the boot log of springboot

# 查看容器
docker container ls
# 查看运行日志
docker logs -f -t --tail 行数 容器名

View running images

docker ps

The browser visits http: // localhost: 8080 / docker to view the results returned by the interface

3. Problem handling

Windows access browser shows that the connection is not available.

Docker runs on Linux. We run Docker on a Windows system. In fact, we first install a Linux environment under Windows, and then run Docker in this environment. Therefore, the localhost used in the access service refers to the address of this Linux environment, not Windows.

Execute in the QuickStart terminal docker-machine ip defaultand find the address of the Linux environment under Window system is 192.168.99.100.

Visit http://192.168.99.100:8080/docker and still return that the connection is not available.

In fact, the reason is that Docker Toolbox cannot be accessed by the Internet .

Open Oracle VM VirtualBox, configure "Port Forwarding" in Settings> Network> NIC 1> Advanced, add a forwarding rule, point the 8080 port of the host (127.0.0.1) to the 8080 port of the subsystem, and visit http: // localhost: 8080 / docker is enough.

Published 40 original articles · 25 praises · 100,000+ views

Guess you like

Origin blog.csdn.net/yym373872996/article/details/105678227