0

I'm creating a docker image and I need to expose a port from my container. When building the image, I need to read the port from a variable file (something similar to .env file). Here is my Dockerfile

FROM ...

ENV PIP_CERT=...

USER root

#installing application source
COPY . /microservice


RUN cd /microservice &&\
  pwd &&\
  ls &&\
  echo "---> Installing dependencies..."

EXPOSE $PORT    

How can I do that?

HHH
  • 6,085
  • 20
  • 92
  • 164
  • Please note that the `EXPOSE` instruction does not actually publish the port, it's more like a documentation. – Maroun Oct 24 '19 at 15:22
  • Possible duplicate https://stackoverflow.com/questions/46917831/can-you-use-a-env-file-for-a-stand-alone-dockerfile/46919859 – dchang Oct 24 '19 at 15:36

1 Answers1

1

You don’t need to make this parameterizable. Pick a port number — whatever the “default” port is for your language framework’s unprivileged HTTP service, for example — and hard-code that in the Dockerfile. If the operator wants to pick a different port to publish, they’ll know what the second number to the docker run -p option needs to be.

# Dockerfile
...
EXPOSE 8000
CMD ["myserver", "-addr=0.0.0.0:8000"]
# The second port number 8000 is a fixed property of the image
# The operator can pick any number for the published port
docker run -p 3333:8000 ...

You should be able to re-run an identical image in multiple environments; if you need to re-build your image to run it somewhere else, consider trying to make the properties that need to change inputs to the image at runtime, like environment variables, and not things you fix in the Dockerfile.

David Maze
  • 130,717
  • 29
  • 175
  • 215