Search code examples
spring-bootdockerdockerfiledocker-run

Spring boot apps port mapping in docker container


In my application.yml file, I added the server port on 8080.

server:
  port: 8080

Now in my Dockerfile, I am exposing port range 8080-8089. Here my goal is I will run this image on the different external and internal ports.

My Dockerfile is given bellow

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} user-service.jar
EXPOSE 8000-8089

ENTRYPOINT ["java","-jar","/user-service.jar"]

My docker image build command is given below:

docker image build -t user-service .

Now after building this image, my docker run command is given below:

docker run -d -p 8080:8080  -it user-service

Here, user-service is my docker image TAG. After this, I can access the app on 8080 port.

But the problem is when I run another container with port mapping -p 8081:8081, the application is running but won't access using port 8081.

My docker run command is:

docker run -d -p 8081:8081  -it user-service

Now my goal is I want to skip the application.yml server port with my external and internal port mapping in docker

Notes:

  1. This is .jar deployment. For WAR deployment we can easily skip the application.yml server port by server's configuration.
  2. I will change the port from the docker image to run container. I don't have scope to modify the application.yml file or Dockerfile after docker build

Thanks in advance


Solution

  • Yes, finally I figured it out. For Spring boot embedded tomcat deployment, during running the container you have to set SERVER_PORT to override the application.yml server port property.

    docker run -d -e SERVER_PORT=8081 -p 8081:8081  -it user-service