我上周刚买了一台新电脑。因此,我像往常一样在Windows 10中使用最新的设置了我的工作环境。然后创建了一个非常简单的spring引导REST服务,只为了打招呼,3天前用创建了映像,它可以很好地处理端口映射“docker -p 8090:8080Davy/myapp”。即使在今天,这个映像仍然工作得很好:我甚至可以通过“http://localhost:8090/sayHello””来访问我的应用程序。
因此,我开始构建我的实际应用程序,并完成了一些功能。我想测试我的应用程序,并使用创建了一个新的映像。
现在,我遇到了一个大问题:我再也不能通过端口映射访问运行在容器中的应用程序了,端口映射是“-p 8090:8080Davy/myapp”http://localhost:8090/sayHello”。它得到一个错误页面,上面写着"localhost没有发送任何数据“。
然后,我通过“码头检查'{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}‘548e29f46ca7”获得了容器IP,显示为“172.17.0.2/”。所以我试了一下http://172.17.0.2:8090/sayHello。在等待了一段时间之后,我得到了"172.17.0.2太长时间才能做出回应“的暂停:
我没有看到端口绑定有任何不同:两者都是0.0.0.0:8090->8080/tcp
我多次使用、1 times和docker-compose.yml重新构建映像,我不能再让容器像旧容器那样了。
我还尝试过“docker -p 8088:8080 davyhu/myapp -m http.server --bind0.0.0.0”,但是得到了相同的结果:无法通过本地主机访问应用程序,以及IP超时。
提前感谢您的帮助!
下面是一些更多的信息:
pom.xml中用于构建包的配置(在pom.xml中这两个版本没有更改):
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<name>davyhu/${project.artifactId}</name>
</image>
</configuration>
</plugin>
</plugins>
</build>
PDFController.java
公共类PdfGenerationController {
private static final Logger logger = LoggerFactory.getLogger(PdfGenerationController.class);
private static final SimpleDateFormat DateFormatterIn1 = new SimpleDateFormat("yyyy-MM-dd");
//private static final SimpleDateFormat DateFormatterIn2 = new SimpleDateFormat("dd/MM/yyyy");
private static final SimpleDateFormat DateFormatterOut = new SimpleDateFormat("dd/MM/yyyy");
private static final SimpleDateFormat DateFormatterIn2 = new SimpleDateFormat("yyyy-MM-dd");
//private static final SimpleDateFormat DateFormatterOut = new SimpleDateFormat("yyyy-MM-dd");
@Autowired
private ResourceBundleMessageSource source;
@Value("${pdf.title}")
private static String pdfTitle;
@Value("${pdf.footerText1}")
private static String pdfFooterText1;
@CrossOrigin(origins = "http://localhost:4200")
@PostMapping("/getPdf")
public ResponseEntity<byte[]> getPDF(
@RequestHeader(name = "Accept-Language", required = true) final Locale locale,
@RequestBody String jsonInput) {
logger.info("myCustomerDetails() method started");
logger.info(jsonInput);
logger.info("locale = {}", locale);
JSONObject data = new JSONObject(jsonInput);
byte[] pdfFile = null;
ResponseEntity<byte[]> response = null;
HttpHeaders headers = new HttpHeaders();
try {
pdfFile = new PdfGenertor().generatePDF(data,locale);
}catch (Exception e) {
e.printStackTrace();
response = new ResponseEntity<>(null, headers, HttpStatus.METHOD_FAILURE);
}
headers.setContentType(MediaType.APPLICATION_PDF);
String filename = "fro_soa_form.pdf";
headers.setContentDispositionFormData(filename, filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
response = new ResponseEntity<>(pdfFile, headers, HttpStatus.OK);
return response;
}
private String formatDate(SimpleDateFormat format, String str) {
try {
Date date = format.parse(str);
return DateFormatterOut.format(date);
} catch (Exception e) {
return "";
}
}
@GetMapping("/sayHello")
public String sayHello() {
return "Hello";
}
}
它的代码工作良好,在eclipse与邮递员(PDF显示的杰森输入和头部接受的语言)。
第一和第二映像都是用"mvn弹簧引导:构建映像“构建的。
如果我需要发布什么,请让mw知道。
谢谢!
Dockerfile:
FROM openjdk:11-slim as build
MAINTAINER xxxx.ca
COPY target/fro_soa_backend-0.0.1-SNAPSHOT.jar fro_soa_backend-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/fro_soa_backend-0.0.1-SNAPSHOT.jar"]
发布于 2022-03-28 15:00:19
我搞不懂到底是怎么回事。因此,我从零开始创建了一个新的工作区,导入代码,用Dockerfile将应用程序重新构建到Docker中,然后端口映射再次工作。
现在,我的前端、角度和后端的Spring引导应用程序可以在本地主机上进行通信。
发布于 2022-03-18 15:19:06
好的,告诉您172.17.0.2是ip容器,所以它不能从容器中被触及,它是从容器中创建的,目的是从其他服务或微服务获得访问。
您的应用程序配置似乎没有在这个端口上显式显示,这意味着您的应用程序不在8080端口上,这就是为什么它会得到空响应的原因,当您添加您的字体时,您需要使用您的Dockerfile并在8080中公开您的应用程序。
FROM <image_name>
EXPOSE 8080
https://stackoverflow.com/questions/71529126
复制相似问题