Home > front end >  Cancel an EXPOSE from an inherited Dockerfile
Cancel an EXPOSE from an inherited Dockerfile

Time:04-27

I have a docker-compose file which invokes an existing image and modifies it - the base image is from a parent project and not under my control. That base image has an EXPOSE 8080, but one of the primary ways I am modifying it is to change the port the service uses.

How can I negate/cancel that EXPOSE from the docker-compose.yaml?

(Note: Future versions of the parent project are likely to update the base image and our local project will modify those future versions, so the obvious solution of "make a fork which changes the Dockerfile" is not an option. Solutions that need some other local files to override the parent are fine, as long as it's all local and not going to require resolving merge conflicts every time the base image updates.)

CodePudding user response:

You're not the first one searching for this (see https://forums.docker.com/t/how-do-i-unexpose-ports/67863); the answer on Docker's forum suggests that you can't override the EXPOSE statement of a parent image, without as you already considered yourself forking the repo and changing it.

It seems unlikely that this will be possible in the future either, looking at the Github issues https://github.com/moby/moby/issues/2210 and https://github.com/moby/moby/issues/3465; both from 2014 and neither with a relevant solution.

CodePudding user response:

As per the docs, the EXPOSE statement mostly functions as documentation and doesn't do anything more

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

So while I agree that it's annoying that there's an incorrect EXPOSE statement in your image, I wouldn't worry about it.

Full documentation here: https://docs.docker.com/engine/reference/builder/#expose

  • Related