Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In docker,how to close the port expose by dockerfile?

Tags:

docker

I've make a image by a dockerfile in which i expose a port by the line "expose 22".

"sudo docker build -t mysql_server . "

after that,i find i didn't need the port.

how can i do to close 22 port?

EDIT: @Adrian Mouat @seanmcl

The image is a mysql.So the data in database all in my container named "cliff_mysql56".if I rebuild the image and run it,I have to move all my data from this container to the new runner :(. That's the reason I don't want to rebuild.

like image 882
cliff Avatar asked Jan 08 '15 05:01

cliff


People also ask

How do I close port in Docker?

Stop YouTrack docker container For a graceful shutdown, you can also use the standard ` docker kill --signal=SIGTERM <containerId> ` command.

What is Dockerfile expose port?

What Is Docker Expose Port? The expose keyword in a Dockerfile tells Docker that a container listens for traffic on the specified port. So, for a container running a web server, you might add this to your Dockerfile: EXPOSE 80.

How do I close Dockerfile?

To exit out of the docker container bash shell. Just run exit or hit ctrl-D like you normally would.


2 Answers

Over 2 years after this is still unsanswered, and you've probably rebuilt your image a thousand times meanwhile. Anyway, I was thinking about this question, actually you cannot commit and runa new image because the EXPOSE directive will be in use when you'll run it again. But, you could possibly avoid rebuilding from scratch doing a commit and using the new image as a base for a new Dockerfile..

I'm not thinking this is a great solution but yes, sometimes you just need a fix, even if far from being "the state of the art" (also, I didn't tried this myself but I don't see why it shouldn't work).

Basically, first you commit your current image into a new one:

docker commit mysql_server new_mysql_server

Then you create a minimal new Dockerfile using your previous image, something like:

FROM new_mysql_server
EXPOSE 80 (or just remove EXPOSE)

ENTRYPOINT ["/what/ever"]

Then you build it

docker build -t cmysql_server .

Stop/Cleaning images and previous containers is optional, eventually you should have a new image without the additional port.

Even though you don't need this answer anymore, I hope it could be a useful suggestion for somebody else.

Cheers!

like image 144
nnsense Avatar answered Oct 01 '22 06:10

nnsense


The EXPOSE line in your Dockerfile is only really relevant for linking containers with --link. If you don't map your port using -p when you run it, the port is not 'open'. If you're really attached to your image, just leave it in and don't worry about it. (Though I do agree with the apt image=cattle analogy.)

like image 44
seanmcl Avatar answered Oct 01 '22 08:10

seanmcl