14

As I know, docker doesn't support any command that change port mapping after make container.
But this answer said that by changing the hostconfig.json file, I can change port mapping.

How do I assign a port mapping to an existing Docker container?
I'm using docker desktop in windows + WSL2.So path of hostconfig.json is different with other answers.
Where is hostconfig.json?
In my case, (\wsl$\docker-desktop-data\mnt\wsl\docker-desktop-data\data, /var/lib/docker(in wsl)) directories are not exist.

Vadzim
  • 24,954
  • 11
  • 143
  • 151
Yunseong Jeon
  • 143
  • 1
  • 4
  • Deleting and recreating the container should be pretty routine, and it's better practice than trying to modify Docker internals. – David Maze Jan 03 '21 at 12:12
  • 2
    @DavidMaze Then you mean make container as image and using that image, make new container when I need new ports? Could you explain why it is better? Modify docker internal config may have a problem? – Yunseong Jeon Jan 03 '21 at 15:28

3 Answers3

20

Where it can be accessed

hostconfig.json for a container can be found in WSL2 using Windows Explorer at the hidden network path \\wsl$\docker-desktop-data\version-pack-data\community\docker\containers\{containerid}\hostconfig.json

Windows Explorer Screenshot with hostconfig.json

Where it is not (but may be expected)

The subdirectories of /mnt/wsl/docker-desktop-data/version-pack-data are not exposed to WSL (for some reason):

deno@DeonJ-T570:/mnt/wsl/docker-desktop-data/version-pack-data$ sudo ls -al
[sudo] password for deno: 
total 0
drwxr-xr-x 2 root root  40 May 25 12:09 .
drwxr-xr-x 6 root root 120 May 25 12:09 ..
deno@DeonJ-T570:/mnt/wsl/docker-desktop-data/version-pack-data$ 
Spastika
  • 364
  • 3
  • 10
  • 1
    You are right that all files can be found under the directory. But it's always rebuilt and my modification were all lost. – Sheldon Wei Dec 24 '21 at 02:36
  • Do I just put that path into the explorer file search (after putting in the container id)? – David G Jan 19 '23 at 00:29
  • Yes, `\\wsl$\docker-desktop-data\version-pack-data\community\docker\containers\{containerid}` should take you straight to the hidden folder of the container. – Spastika Jan 27 '23 at 06:09
6

I know this question has already been answered, but the path in my system is different. In case anyone else has the same problem, I didn't want to keep the solution for myself.

The path to the Docker container on my system (Windows 11) is:

\\wsl.localhost\docker-desktop-data\data\docker\containers

It's just slightly different, but until a few minutes ago, I doubted that the file hostconfig.json was accessible on my system because the numerous paths that you find on the internet didn't exist on my system.

4

Docker Desktop (Windows 10)

You can find it here(see below steps for more info):

cd /var/lib/docker/containers/container-id

Steps to change container host port, without deleting the container:

  • Stop running container
  • docker run --net=host --ipc=host --uts=host --pid=host -it --security-opt=seccomp=unconfined --privileged --rm -it -v /:/host alpine
  • chroot /host
  • cd /var/lib/docker/containers/container-id
  • cat hostconfig.json //You will see the current port that you are using
  • echo 'the above json, with the changed port' > hostconfig.json //Use outer single-quotes to preserve the json double-quotes
  • Restart docker-desktop
jumping_monkey
  • 5,941
  • 2
  • 43
  • 58