我想从Docker容器访问我的网站,但不能。我尝试实现的步骤如下。完成所有步骤后,我无法访问http://localhost:8080/index
页,我在哪里出错?
Spring-Boot项目名称为demo
。我的部分代码:
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
。
docker build -t myimage1 .
然后,使用此命令从myimage1
创建一个新容器。
docker run --name mycontainer1 myimage1
然后Maven下载必要的文件并为我启动我的应用程序。最后输出:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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
)转换为图像?如何访问我的网页?
答案 0 :(得分:1)
运行Docker容器时,默认情况下不会发布任何应用程序碰巧监听的所有端口。
为了发布端口,您需要在使用映像运行容器时指定端口。有关如何执行此操作的所有详细信息,可以查看 docker run 命令的文档中的“ EXPOSE”部分:https://docs.docker.com/engine/reference/run/
简而言之,您想在运行容器时添加另一个选项:
docker run --name mycontainer1 -p 8080:8080 myimage1
我不确定您是否想通过添加
实现这一目标EXPOSE 8080
在您的Dockerfile中。实际上,这并不意味着在使用映像运行容器时端口将被暴露。正如您在Dockerfile reference中所发现的:
EXPOSE指令实际上并未发布端口。它充当构建映像的人员和运行容器的人员之间的一种文档类型,有关打算发布哪些端口的信息。要在运行容器时实际发布端口,请使用docker run上的-p标志发布并映射一个或多个端口,或使用-P标志发布所有公开的端口并将其映射到高阶端口。
答案 1 :(得分:0)
要完全解决您的问题(在开发阶段,例如对Spring Boot项目进行泊坞并在本地浏览器中浏览相应的webapp),必须完成三个独立的任务:
使用Dockerfile
来利用Docker映像的构建,该映像受益于Docker的缓存机制(避免每次从头开始重新下载Maven依赖项,从而加快构建速度)
确保Spring Boot应用侦听0.0.0.0
特殊IP的指定端口 ,而不是localhost
;
最后发布给定的端口,以便您可以运行例如:
$ xdg-open http://localhost:8080/index
@Poger的答案很好地解释了步骤3,因此,我将仅对步骤1和2进行详细说明:
我在这个SO线程中提出了一个Dockerfile
:受How to cache maven dependencies in Docker启发,this blog article适用于一般的Java / Maven项目(不仅是Spring Boot项目) ):
# our base build image
FROM maven:3.5-jdk-8 as maven
WORKDIR /app
# copy the Project Object Model file
COPY ./pom.xml ./pom.xml
# fetch all dependencies
RUN mvn dependency:go-offline -B
# copy your other files
COPY ./src ./src
# build for release
# NOTE: my-project-* should be replaced with the proper prefix
RUN mvn package && cp target/my-project-*.jar app.jar
# smaller, final base image
FROM openjdk:8u171-jre-alpine
# OPTIONAL: copy dependencies so the thin jar won't need to re-download them
# COPY --from=maven /root/.m2 /root/.m2
# set deployment directory
WORKDIR /app
# copy over the built artifact from the maven image
COPY --from=maven /app/app.jar ./app.jar
# set the startup command to run your binary
CMD ["java", "-jar", "/app/app.jar"]
但是要进一步完善,请注意,建议您pass an extra system property java.security.egd
:
CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/app.jar"]
或者如果您更喜欢ENTRYPOINT
而不是CMD
:
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/app.jar"]
如SO线程How do I access a spring app running in a docker container?中所述,在容器化应用程序的上下文中,应避免使用localhost
并在大多数情况下将其替换为0.0.0.0
(即,如果该应用程序应充当Web服务,以响应来自容器外部的传入请求。
总而言之,您应该尝试在application.properties
文件中添加以下行:
server.address=0.0.0.0