docker icon indicating copy to clipboard operation
docker copied to clipboard

Enable exposed port to be configurable to run container in host networking mode on a port different than 80

Open kanishkdudeja opened this issue 6 years ago • 15 comments

If I'm running the container with --net=host, it can only be reached on port 80 (ports mapping is disabled when network mode is host).

It would be great to have the possibility to set the port where nginx will listen to with an environment variable (an example is this image: https://github.com/mesoscloud/chronos).

kanishkdudeja avatar Oct 20 '17 13:10 kanishkdudeja

I looked a bit into this and found this mentioned in the official ngix docker repo

Out-of-the-box, nginx doesn't support environment variables inside most configuration blocks. But envsubst may be used as a workaround if you need to generate your nginx configuration dynamically before nginx starts. https://github.com/docker-library/docs/tree/master/nginx#using-environment-variables-in-nginx-configuration

As of now I don't think envsubst is included by default with alpine. We need to include this when building the image: RUN apk add --update $RUNTIME_DEPS && apk add --virtual build_deps $BUILD_DEPS && cp /usr/bin/envsubst /usr/local/bin/envsubst && apk del build_deps

Afterwards we can perform the environment substitution in the run.sh script.

sa3dany avatar Oct 24 '17 15:10 sa3dany

This feature would be extremely helpful.

Port 80 is the default on many images. In the context of a Kubernetes pod, each container must have a unique port – so I cannot run Nginx and PHPMyAdmin within the same pod.

KristianWindsor avatar May 29 '19 22:05 KristianWindsor

Solution: you can change the port with a Dockerfile.

FROM phpmyadmin/phpmyadmin
RUN sed -i 's/80/8080/g' /etc/nginx.conf

KristianWindsor avatar May 30 '19 02:05 KristianWindsor

@KristianWindsor: Thanks for the info, It's not really a persistent solution because you have to do that after every container update.

ecdlguy avatar Feb 17 '20 10:02 ecdlguy

But we do not have a nginx version anymore, right?

williamdes avatar Feb 17 '20 12:02 williamdes

So it's easier to add this functionality using environment variables?

ecdlguy avatar Feb 17 '20 12:02 ecdlguy

https://github.com/phpmyadmin/docker/blob/master/apache/Dockerfile

I have to look if it is possible with the image php gives us

williamdes avatar Feb 17 '20 13:02 williamdes

A way to avoid building your own image (to keep things simpler) is to start the image like this:

docker run \
-it \
--rm \
--net=host \
--entrypoint=/bin/bash \
phpmyadmin/phpmyadmin:5.0.1 \
-c "sed -i 's/Listen 80/Listen 8080/g' /etc/apache2/ports.conf && /docker-entrypoint.sh apache2-foreground"

spantaleev avatar Mar 14 '20 07:03 spantaleev

Thank you for the command I can integrate changing the port to an ENV variable now

williamdes avatar Mar 14 '20 08:03 williamdes

Ref https://github.com/docker-library/php/issues/94#issuecomment-320695125

@J0WI would such a solution be accepted in the entrypoint using an ENV variable ?

williamdes avatar Mar 17 '20 21:03 williamdes

Is there a use case to run them in the same pod? For Docker you can also use -p 8080:80 to expose it on a different port.

J0WI avatar Mar 17 '20 22:03 J0WI

in Google Cloud Run, there is an already defined env variable PORT (default to 8080), and it is supposed your container honor that variable.

sergiokessler avatar Jun 28 '20 22:06 sergiokessler

Any updates about this feature?

simann avatar Jun 03 '21 18:06 simann

Any updates about this feature?

Hi @J0WI You have more expertise than me, would you too consider this feature as valid ?

williamdes avatar Jun 11 '21 11:06 williamdes

IMHO this shouldn't be changed in every image based on the PHP base image https://github.com/nextcloud/docker/pull/1504#issuecomment-862410960

J0WI avatar Jun 16 '21 14:06 J0WI

Duplicate of #340

williamdes avatar Aug 03 '23 14:08 williamdes

Done in #369

williamdes avatar Aug 03 '23 14:08 williamdes