where is hostconfig.json (Docker desktop + WSL2 environment)
Asked Answered
C

3

16

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.

Coruscate answered 3/1, 2021 at 2:26 Comment(2)
Deleting and recreating the container should be pretty routine, and it's better practice than trying to modify Docker internals.Relentless
@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?Coruscate
S
22

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$ 
Sexagesima answered 25/5, 2021 at 11:27 Comment(3)
You are right that all files can be found under the directory. But it's always rebuilt and my modification were all lost.Britisher
Do I just put that path into the explorer file search (after putting in the container id)?Tillotson
Yes, \\wsl$\docker-desktop-data\version-pack-data\community\docker\containers\{containerid} should take you straight to the hidden folder of the container.Sexagesima
H
11

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.

Harmon answered 21/4, 2023 at 7:51 Comment(0)
S
5

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
Scheider answered 1/7, 2022 at 6:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.