7

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.

cliff
  • 83
  • 1
  • 1
  • 5
  • Does this answer your question? : http://stackoverflow.com/questions/22111060/difference-between-expose-and-publish-in-docker – Paul Jan 08 '15 at 07:16
  • That question is talk about how many way to expose the port.I just want to know how to close the exposed port... – cliff Jan 08 '15 at 07:55
  • What do you mean "close". If you don't have -p or -P command when you run the docker image, the port is not exposed out to host. If u don't have port 22 by your app inside docker container, it doesn't opened inside docker container as well – Larry Cai Jan 08 '15 at 14:15
  • 1
    @cliff, the real problem is you should store your data in a volume to avoid this sort of issue. If you used the official mysql image, this is the case and you can just build a new image and run --volumes-from to get the old data. You need to read up on volumes https://docs.docker.com/userguide/dockervolumes/. – Adrian Mouat Jan 09 '15 at 10:17

4 Answers4

3

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!

nnsense
  • 1,056
  • 11
  • 18
2

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.)

seanmcl
  • 9,740
  • 3
  • 39
  • 45
0

Remove your "expose 22" line in your Dockerfile, and rebuild your image

user2915097
  • 30,758
  • 6
  • 57
  • 59
  • I've done a lot work on this image,i don't want to recreate :( – cliff Jan 08 '15 at 07:55
  • 4
    @cliff that's a bit odd - the whole point of Dockerfiles is that they make it easy and repeatable to recreate images. Your containers should be cattle, not pets... – Adrian Mouat Jan 08 '15 at 09:19
  • May be you created your image with many operations and an eventual commit ? As Adrian said, something like `docker build -tag="my_imagev3".` should be your motto – user2915097 Jan 08 '15 at 12:19
  • Every post, from closing ports, to exposing ssh, to not wanting to 'recreate'... suggests you are doing it wrong. – user2105103 Jan 08 '15 at 20:17
  • Yeah, it's *very* wrong. You have to think of containers as transient, or you will end up in trouble like this. In this case you need to understand data volumes https://docs.docker.com/userguide/dockervolumes/ – Adrian Mouat Jan 09 '15 at 10:20
  • 1
    images are immuable, you build another one when you do a slight or big modification. – user2915097 Jan 09 '15 at 10:40
0

Remove the 'EXPOSE' directive, then rebuild.

I see you have mentioned you don't want to rebuild, but if you can't rebuild your container quickly and safely without data loss, you're not using docker correctly. I would research into volumes to ensure the needed data can persist.

George
  • 115
  • 1
  • 3