TL;DR
The server.port property defined in a Spring Boot application.properties file is not getting honored by the Docker engine when running a web application in Tomcat.
Background :
I have a simple Spring Boot application containing a single rest endpoint. I am able to run this application in Eclipse and access the rest endpoint successfully from the browser.
However, when I try to deploy the same application in a Docker container, I am unable to access the rest endpoint from the browser.
Dockerfile :
FROM tomcat
EXPOSE 8000
RUN rm -rf /user/local/tomcat/webapps/*
COPY ./target/my-web-app.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh","run"]
Spring Boot application.properties :
spring.application.name=my-web-app
server.port=8000
spring.jpa.show-sql=true
spring.h2.console.enabled=true
Docker images output :
CK@Ping MINGW64 /c/Program Files/Docker Toolbox
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myrepo/my-web-app 0.0.1-SNAPSHOT 625e9d889139 4 minutes ago 694MB
Start the container :
Note that I map the server.port of my Spring Boot application.properties file to the external port.
docker run -p 8000:8000 -d myrepo/my-web-app:0.0.1-SNAPSHOT
docker container ls output :
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3eff09347de3 myrep/my-web-app:0.0.1-SNAPSHOT "catalina.sh run" Less than a second ago Up 4 seconds 0.0.0.0:8000->8000/tcp, 8080/tcp hardcore
Error in the Browser :
This site can’t be reached
I am trying to access the URL using :
http://192.168.99.100:8000/my-web-app/getResults
The same app works just fine when launched from Eclipse instead of a Docker container and I am able to access the rest end point using :
http://localhost:8000/my-web-app/getResults
Update/Edit:
I found the root cause. I need to map port 8000 to 8080 instead of mapping 8000 to 8000 itself :
docker run -p 8000:8080 -d myrepo/my-web-app:0.0.1-SNAPSHOT
This means that the server.port property that I defined in my application.properties is not getting honored when starting up the container.
What is even more strange is that I can set the server.port to 8111 for example and still be able to access the URL if I use -p 8000:8080.
However, tomcat starts up on the port specified by server.port if I run the same application in Eclipse rather than a Docker container.
Bottom line : The server.port property defined in a Spring Boot application.properties file is not getting honored by the Docker engine.