我正在尝试学习Docker,但我什至无法入门:(
我做了一个微服务,如果您调用它,它只会返回“测试”。如果我运行java -jar test.jar
并转到localhost:8080
,浏览器将显示预期结果:
但是,如果我使用Docker部署该JAR,则无法访问该页面:
这是我的Dockerfile
:
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=test.jar
ARG PORT=8080
EXPOSE ${PORT}
VOLUME /tmp
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
docker build -t test .
Sending build context to Docker daemon 16.2MB
Step 1/7 : FROM openjdk:8-jdk-alpine
---> 5801f7d008e5
Step 2/7 : ARG JAR_FILE=test.jar
---> Using cache
---> 7095632eee4a
Step 3/7 : ARG PORT=8080
---> Using cache
---> 4a1351be9f66
Step 4/7 : EXPOSE ${PORT}
---> Using cache
---> 6f7958533a34
Step 5/7 : VOLUME /tmp
---> Using cache
---> 6bafa2fa82cf
Step 6/7 : ADD ${JAR_FILE} app.jar
---> Using cache
---> 2dc3da720820
Step 7/7 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
---> Using cache
---> 60348d53cf97
Successfully built 60348d53cf97
Successfully tagged test:latest
docker run test -p 8080:8080
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.4.RELEASE)
2018-09-03 18:53:26.595 INFO 1 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on 81a95eb3474a with PID 1 (/app.jar started by root in /)
2018-09-03 18:53:26.599 INFO 1 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2018-09-03 18:53:26.669 INFO 1 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@12f40c25: startup date [Mon Sep 03 18:53:26 GMT 2018]; root of context hierarchy
2018-09-03 18:53:28.215 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-09-03 18:53:28.253 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-09-03 18:53:28.253 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.32
2018-09-03 18:53:28.272 INFO 1 --- [ost-startStop-1] 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/lib/jvm/java-1.8-openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64:/usr/lib/jvm/java-1.8-openjdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2018-09-03 18:53:28.355 INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-09-03 18:53:28.356 INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1691 ms
2018-09-03 18:53:28.422 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-09-03 18:53:28.426 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-09-03 18:53:28.427 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-09-03 18:53:28.427 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-09-03 18:53:28.427 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-09-03 18:53:28.589 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-03 18:53:28.833 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@12f40c25: startup date [Mon Sep 03 18:53:26 GMT 2018]; root of context hierarchy
2018-09-03 18:53:28.944 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public java.lang.String com.example.demo.NewRestController.getTEst()
2018-09-03 18:53:28.949 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-09-03 18:53:28.950 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-09-03 18:53:28.977 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-03 18:53:28.977 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-03 18:53:29.140 INFO 1 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-09-03 18:53:29.191 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-09-03 18:53:29.195 INFO 1 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 3.008 seconds (JVM running for 3.491)
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81a95eb3474a test "java -Djava.securit…" 2 minutes ago Up 2 minutes 8080/tcp happy_feynman
我已经坚持了一天以上,我无法将头缠住它。我该怎么办?
答案 0 :(得分:5)
将docker run test -p 8080:8080
更改为docker run -p 8080:8080 test
作为当前语法,而不是在localhost
上公开端口,而是将-p 8080:8080
作为参数传递给程序。您可以在docker ps
中看到它,在端口下您只有8080/tcp
,这意味着您的端口暴露在docker网络内部-未映射到主机。更多信息:https://docs.docker.com/engine/reference/run/