【问题标题】:Springboot Docker Windows7春季启动泊坞窗 7
【发布时间】:2018-10-29 10:57:36
【问题描述】:

我创建了最简单的基本 spring-boot hello world 应用程序并尝试在 docker 上运行它。但是我收到错误。以下是更多详细信息--

操作系统 - Windows 7,64 位

Docker 版本:Docker 版本 18.03.0-ce,构建 0520e24302

Springboot 版本:1.5.14.BUILD-SNAPSHOT

我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.springboot.docker</groupId>
    <artifactId>spring-boot-docker</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-docker</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.14.BUILD-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

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

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

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


</project>

我的 Dockerfile

FROM java:8
VOLUME /tmp
EXPOSE 8082
ADD target/spring-boot-docker.jar spring-boot-docker.jar
ENTRYPOINT ["java","-jar","spring-boot-docker.jar"]

我的应用程序.properties

server.port=8082

我的 java 类

@SpringBootApplication
public class SpringBootDockerApplication {

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

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String sayHi() {
        return "Hi";
    }
}

我通过在本地运行此应用程序对其进行了测试,当我点击:http://localhost:8082/hello 我看到预期的消息“Hi”

然后我做了 mvn clean install 来创建我的应用程序的目标 jar。 然后我在我的 Windows 机器上启动了“Docker Quickstart Terminal”。 之后我去了我的代码所在的目录,从那里我运行了下面的命令::

docker build . -t spring-boot-docker  

当我检查 docker 镜像时,我可以看到 spring-boot-docker 创建的新镜像。

之后我运行下面的命令从该图像创建容器

docker run --name springbootdocker -d spring-boot-docker:latest

我可以看到使用命令创建的容器:“docker ps” 我还检查了“docker logs springbootdocker”

然后我尝试点击 URL: http://localhost:8082/hello

我收到错误消息:

无法访问此网站 localhost 拒绝连接。 在 Google 上搜索 localhost 8082 你好 ERR_CONNECTION_REFUSED

在 Windows 上运行 docker 是否存在已知问题?

我是否需要更改 Dockerfile 才能在 ubuntu 或类似的东西中运行它?

【问题讨论】:

  • 你必须发布端口。添加 -P 以发布所有公开的端口或签入 -p 以获取自定义映射。
  • @user2105103 我们如何使用 -p 进行自定义映射?你能举个例子或给我发送示例命令
  • 我试过这个命令:docker run -P --name springbootdocker -d spring-boot-docker:latest 这没有帮助

标签: windows docker spring-boot windows-7 64-bit


【解决方案1】:

答案很简单。 虽然一切都在windows上发生,但底层操作系统是Linux,所以我们必须用VM的linux机器的ip地址替换localhost 所以不要打:http://localhost:8082/hello 我不得不打:http://192.168.99.100:8082/hello

【讨论】:

    猜你喜欢
    • 2017-10-12
    • 2023-03-05
    • 2018-10-28
    • 2021-11-05
    • 2018-12-20
    • 2019-06-28
    • 2016-10-20
    • 1970-01-01
    • 2021-05-19
    相关资源
    最近更新 更多