Home > Mobile >  Pass Spring Boot port through Docker run
Pass Spring Boot port through Docker run

Time:11-05

I want to run the same application on multiple containers on the same Docker network, I have another application that will start the docker containers, the issue is the number of containers is undefined, so I want to pass the ports when I run the image not build it, by default the container will run on port 8080, but when a new container starts it will also try to run on 8080, therefore I want to change the port to not cause the other containers to fail.

Here is the Dockerfile for the application:

FROM maven:3.8.6-openjdk-18-slim as BUILD
WORKDIR /usr/src/app

#Download dependencies
COPY pom.xml ./
RUN ["/usr/local/bin/mvn-entrypoint.sh", "mvn", "verify", "clean", "--fail-never"]

#Create war file
COPY ./src ./src
RUN mvn -f ./pom.xml package

FROM openjdk:18.0.2.1-jdk-slim
ARG port
ENV PORT $port
WORKDIR /usr/src/app
COPY --from=BUILD /usr/src/app/target/*.war /usr/src/app/*.war
ENTRYPOINT java -jar "/usr/src/app/*.war" --server.port=$PORT
EXPOSE $PORT

if I use --build-arg it works since it's using the ARG port, but since I want to set it on run time so I made an environmental variable.

If I run docker run -it <container_id> -e "PORT=8085" the application still runs on 8080 or the --build-arg if provided.

Is there a way to provide the port when using docker run instead of building the same image every single time I want it to run on a different port?

CodePudding user response:

There should be no conflict if multiple containers are using the same port since each container has a different IP address, which can be verified with docker container inspect NAME|ID.

  • Related