如何将Spring-Boot Web服务转换为Docker映像? [英] How to convert a Spring-Boot web service into a Docker image?

查看:111
本文介绍了如何将Spring-Boot Web服务转换为Docker映像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Docker容器访问我的网站,但不能。我尝试实现的步骤如下。完成所有步骤后,我无法访问 http:// localhost:8080 / index 页面,我在哪里出错了?

I want to access my website from a Docker container but I can't. The steps I am trying to implement are as follows. After all the steps I can't access the http://localhost:8080/index page, where did I make a mistake?

Spring-Boot项目的名称为 demo 。我的部分代码:

The Spring-Boot project name is demo. Parts of my code:

package com.qwerty.demo.rest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FunRestService {

    @GetMapping("/index")
    public String setIndex() {
        return "HELLO WORLD!";
    }
}

我的 dockerfile 代码:

FROM openjdk:8
COPY . /usr/var/www/MYPROJECT
WORKDIR /usr/var/www/MYPROJECT
EXPOSE 8080
CMD ./mvnw spring-boot:run

我使用此命令将Spring-Boot项目构建为 myimage1

I build the Spring-Boot project to myimage1 with this command.

docker build -t myimage1 .

然后,我从 myimage1

Then, I create a new container from myimage1 with this command.

docker run --name mycontainer1 myimage1

然后Maven下载必要的文件并为我启动我的应用程序。最后输出:

And Maven downloads necessary files and starts my app for me. Last output:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-01-19 17:40:32.604  INFO 6 --- [           main] com.qwerty.demo.DemoApplication     : Starting DemoApplication on 8086b6e010fb with PID 6 (/usr/var/www/MYPROJECT/target/classes started by root in /usr/var/www/MYPROJECT)
2019-01-19 17:40:32.613  INFO 6 --- [           main] com.qwerty.demo.DemoApplication     : No active profile set, falling back to default profiles: default
2019-01-19 17:40:34.119  INFO 6 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-01-19 17:40:34.170  INFO 6 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-01-19 17:40:34.171  INFO 6 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.14]
2019-01-19 17:40:34.186  INFO 6 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib]
2019-01-19 17:40:34.288  INFO 6 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-01-19 17:40:34.289  INFO 6 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1559 ms
2019-01-19 17:40:34.602  INFO 6 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-19 17:40:34.882  INFO 6 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''2019-01-19 17:40:34.888  INFO 6 --- [           main] com.qwerty.demo.DemoApplication     : Started DemoApplication in 3.176 seconds (JVM running for 69.839)

应该做什么我们确实将一个这样的Spring-Boot项目(使用 Dockerfile )转换为映像?如何访问我的网页?

What should we do to convert one such Spring-Boot project (using a Dockerfile) into an image? How can I access my web page?

推荐答案

运行Docker容器时,所有应用程序碰巧监听的所有端口

When you run a Docker container, all the ports, which any application happen to listen on within it, are not published by default.

要发布端口,需要在使用图像运行容器时指定端口。有关如何执行此操作的所有详细信息,可以检查 docker run 命令的文档中的 EXPOSE部分:> https://docs.docker.com/engine/reference/run/

In order to publish a port, you need to specify it while running the container using your image. For all the details on how to do it, you can check the "EXPOSE" section in the documentation of docker run command: https://docs.docker.com/engine/reference/run/

简而言之,您想在运行容器时添加另一个选项:

Shortly speaking, you want to add another option while running your container:

docker run --name mycontainer1 -p 8080:8080 myimage1

我不确定您是否要通过添加

I'm not sure if you wanted to achieve this by adding

EXPOSE 8080

实际上,这并不意味着在使用映像运行容器时端口将被暴露。您可能会在 Dockerfile参考中找到:

in your Dockerfile. Actually, this does not mean that the port will be exposed when the image is used to run a container. As you might find in Dockerfile reference:


EXPOSE指令实际上并不发布端口。它充当构建映像的人员和运行容器的人员之间的一种文档类型,有关打算发布哪些端口的信息。要在运行容器时实际发布端口,请使用docker run上的-p标志发布并映射一个或多个端口,或使用-P标志发布所有公开的端口并将它们映射到高阶端口。

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

这篇关于如何将Spring-Boot Web服务转换为Docker映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆