Docker EXPOSE using run-time environment variables

A little late but you could also use build args and change your code to:

FROM python:3.6.5-stretch

[ ... ]

ARG MY_SERVICE_PORT=8080
ARG MY_SERVICE_PORT_RPC=50051
# 8080 and 50051 would be the defaults

[ ... ]
# Still functions like environment variables :)
EXPOSE ${MY_SERVICE_PORT}
EXPOSE ${MY_SERVICE_PORT_RPC}

Then you can build with docker build --build-arg MY_SERVICE_PORT=80 -t image_tag before you run. This way you could have your containerized application and your container running with the same ports without getting too complex.


If you are not sure what your exposed ports will be then don't set them in the dockerfile. Just use --expose with docker run to specify ports at run time along with environment variable for details:

https://docs.docker.com/engine/reference/run/#expose-incoming-ports