How can kubernetes dynamically expose my docker port?

1/31/2017

I have a dockerfile of a ruby app that runs a puma server on port 5000. (It EXPOSE the port and also uses the RUN command to run puma -p 5000.

In my deployment.yaml I have to set containerPort to be 5000 to match this port.

This seems odd to me that my configuration lists the port in 2 different places. If I need to change the port it means I am changing the configuration in multiple places which is against the principles of the 12 factor app where configs are all in the same place.

Is there a way to set the port in only 1 place?

-- Terence Chow
docker
dockerfile
kubernetes

1 Answer

1/31/2017

In your deployment.yaml you actually don't have to specify the containerPort; all ports are exposed. From the docs:

ports ContainerPort array

List of ports to expose from the container. Exposing a port here gives the system additional information about the network connections a container uses, but is primarily informational. Not specifying a port here DOES NOT prevent that port from being exposed. Any port which is listening on the default "0.0.0.0" address inside a container will be accessible from the network. Cannot be updated.

-- Pixel Elephant
Source: StackOverflow