0

The only 2 ways I know of to map X port from my host machine to Y port on a Docker container is to use docker run -p X:Y or docker-compose.yaml. An existing workflow at my company uses the following to create, start, and connect to a devcontainer:

  • docker create ...
  • docker start ...
  • docker exec -it ...

I would now like to add SSH connectivity to the container, but know of no way to map an arbitrary local port to 22 in the container using any of the existing steps (create, start, exec). Is this possible with the existing workflow? Is there some technical difficulty behind the decision not to be able to map ports with docker create?

P.S. The third way I know of to map ports is to stop the container, stop the Docker service, edit the container's hostconfig.json, and restart the Docker service and container -- but I'm not going to do that in our workflow.

Hau
  • 443
  • 6
  • 13
  • Does it answer https://stackoverflow.com/q/19335444? – Sergio Santiago Sep 14 '22 at 18:43
  • Does this answer your question? [How do I assign a port mapping to an existing Docker container?](https://stackoverflow.com/questions/19335444/how-do-i-assign-a-port-mapping-to-an-existing-docker-container) – Daniel Mann Sep 14 '22 at 18:45
  • No, while that answer does work, I already addressed that in my P.S.. It is extremely tedious, slow, and hacky in general. I would like to map a local port to the container using either `docker create`, `docker start`, or `docker exec -it` (either use the existing steps or make at most minor changes). – Hau Sep 14 '22 at 18:51
  • Also, to address the top-voted (not accepted) answer in that SO question, the comments on that answer do a fine job, but I'll reiterate here. The answer offers the solution of stopping the container, saving it to an image, and then recreating the image with the desired port mappings. Environment variables are not preserved, mounts are not preserved -- in fact, almost no configuration is transferred to the new container. Only the filesystem. Also, my situation is not that I have an existing container that I would like to add ports to. I am making a new container. – Hau Sep 14 '22 at 18:57
  • Is there a specific ssh server library you're using inside your application code? Why can't you delete the container (stop the process) and create a new one (re-launch the process) with the options you need? – David Maze Sep 14 '22 at 19:29
  • My goal is to install ssh on the server and expose 22 so that my IDE can ssh in. The problem is that I can't create a mapping from an arbitrary local port to 22 in the container using `docker create`, `docker start`, or `docker exec`. The container doesn't exist yet. I'm trying to create it with the desired port mapping. The only ways I know to do that are `docker run`, `docker-compose`, and stop-docker-service-completely-update-config-file-restart. – Hau Sep 14 '22 at 22:40
  • An update for my specific situation: I've talked with the other people using the devcontainer, and we're all ok with using `docker run -d` instead of the `docker create` command. We don't have any specific need to create the container without running it, so my problem is solved. But the question still stands: If someone needed to create a container with port mappings without starting it, can they do that besides using the hacky procedure I outlined in the P.S.? – Hau Sep 14 '22 at 22:43

0 Answers0