Shekhar
Shekhar

Reputation: 11788

How to give port number for docker container at run time?

I have written one Scala and Akka HTTP based REST API and created Dockerfile to create Docker image for this API. My Dockerfile is as follows:

FROM maven:3.6.0-jdk-8-alpine AS MAVEN_TOOL_CHAIN
COPY pom.xml /tmp/parent/
COPY data-catalogue/pom.xml /tmp/parent/data-catalogue/
COPY data-catalogue/src /tmp/parent/data-catalogue/src/
WORKDIR /tmp/parent/data-catalogue/
RUN mvn package

FROM java:openjdk-8
COPY --from=MAVEN_TOOL_CHAIN /tmp/parent/data-catalogue/target/data-catalogue-1.0-SNAPSHOT.jar /opt/data-catalogue.jar

COPY data-catalogue/src/main/resources/logback.xml /opt/logback.xml

ENTRYPOINT ["java", "-Dlogging.config=/opt/logback.xml", "-jar", "/opt/data-catalogue.jar", "prod"]
CMD ["8080"]

Everything is good so far. I can run one container using this image.

Now requirement is to run two containers using this image on same Docker host. I have modified REST APIs main class such that it will take port number on which it has to run as an command line argument. If command line argument is not provided then it will listen for requests on 8080 port.

I would like to know how do I provide command line parameter to my REST API while starting container?

For example:

  1. First instance of REST API should start/run on port 5555 so this 5555 argument should reach main class of REST API
  2. Second instance of REST API should start/run on port 1111 so this 5555 argument should reach main class of REST API

I have tried to use ENTRYPOINT and CMD for this but my command line argument simply does not reach main class and REST API starts on 8080 port only.

Upvotes: 2

Views: 3443

Answers (3)

Fisharp
Fisharp

Reputation: 654

Docker PORT mapping is your answer.

Dockerizing your API is exactly the opposite to provide your API with the port you want to run it on everytime. That's exactly what you don't want to do when going the Docker way.

Your API should be able to attend to your requests through whichever port you decide to EXPOSE on your docker image, and then, at run time, you just need to map any port you wish from your host to the inner port of your API (which, inside its container, will be attending to the same "internal" port always.

So.. how does it look like?

docker run -d --name api-1 -p 5555:8080 my/api

and then...

docker run -d --name api-2 -p 1111:8080 my/api

Now both instances are running on your host, and you're able to hit both of them, each with a different host port (even when internally they're using the same port number)

Upvotes: 4

rajesh-nitc
rajesh-nitc

Reputation: 5549

Through Environment Variables

In Dockerfile:

ENV PORT 8080 

You can override the above env on the cmd by passing -e for e.g.

docker run -d my_image -e "PORT=5555"

Consume the env in your application code

So for e.g. if you don't provide env on the cmd, your application code will receive 8080 as PORT value. If you do override the env on cmd, your application code will receive 5555 as PORT value

Upvotes: 3

LinPy
LinPy

Reputation: 18618

you can set ARG to your container:

ARG MYPORT

then export it like:

CMD [ $MYPORT ]

then start your docker like this:

export MYPORT=5000 ; docker run ....

Upvotes: 2

Related Questions