Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I use EXPOSE $PORT in a Dockerfile, can I un-expose the port it when I use `docker run`?

I'm working on a Dockerfile for a web app that will use an nginx-proxy container. It's also got a CLI for doing app domain stuff (creating/modifying db, running cleanup jobs, etc.)

In 99% of cases, when I boot a container, I want to use the webapp. I've got an EXPOSE 3000 in the Dockerfile and that works perfectly well for NGINX-proxy. Nginx-proxy uses docker-gen, which listens for docker's start and stop events and re-builds the NGINX config based on exposed ports.

The problem comes when I want to run a CLI-based container. I don't want to EXPOSE 3000. I want to un-expose port 3000, so NGINX-proxy doesn't change the NGINX configs.

Is this possible from docker run? Reading the docs didn't give any clarity, and trying docker run -p '' didn't do work (I got a docker: No port specified: ::<empty>. error).

Honestly, this is a bit of a nitpick, and it's not that big of a deal. I can take EXPOSE 3000 out of the Dockerfile and just do -p 3000 on the command line. I just like having it in the Dockerfile so it's on by default and I just have to turn it off in the few instances when I'll need it.

I also know that I can use a second Dockerfile that inherits from the first.

I'm just curious if it's possible to un-publish a port exposed in the Dockerfile when I do docker run (or possibly in docker-compose).

like image 402
JT. Avatar asked Nov 04 '15 23:11

JT.


2 Answers

Even a couple of years later, the situation hasn't changed much. There is no UNEXPOSE but atleast there is a workaround with "docker save" and "docker load" allowing to edit the metadata of a docker image. Atleast to remove volumes and ports, I have created a little script that can automate the task, see docker-copyedit.

like image 53
Guido U. Draheim Avatar answered Sep 29 '22 08:09

Guido U. Draheim


As far as I know that's not possible now. The best thing you can do is too use the -P option to map the 3000 to some random port, so it won't conflict with the main container instance, e.g.

docker run -it -P <some-image>

This will result in the following docker ps

main_container   0.0.0.0:3000->3000/tcp
run_container    0.0.0.0:32769->3000/tcp
like image 28
Dmitry Sokurenko Avatar answered Sep 29 '22 08:09

Dmitry Sokurenko