I have some tutorial where I change files in /var/lib/docker
, but I also mention that it is not recommended in a production system.
If you make a mistake, your container will not work at all. You also have to realize that you need to stop Docker before you edit that hostconfig.json
. It usually means all of your containers stops and you have to restart them if the restart poilicy does not do that. So until you finish stopping Docker, editing json file (correctly), starting Docker, Starting the container, your service will be completely unavailable.
Even if you manage to do everything, some firewall settings can deny to access additional ports.
Changing hostconfig.json is something that I haven’t done before your post, so I did it now. It didn’t work for me. Maybe I made a mistake, but it’s not trivial. In case of Docker Desktop, that hostconfig.json is in the virtual machine, so it is harder to edit.
On the other hand, you could just copy everything out from the container that you want to share, and run a server on any port on the host. You can even start the new server in a new container and mount the data into the container.
Or you can run a proxy server on the host which forwards traffic to the container, or you can just use socat since the containers’ ports are accessible from the host:
socat TCP-LISTEN:8889,fork TCP:172.17.0.2:8080
In my above example 8889 is the host port I forward to the container. 172.17.0.2
is the IP address of the container and 8080 is the port on which the new service is running. If you want to run it on Windows, you need to run socat in a new container which has a forwarded port from the host to itself.
if you don’t know the IP address of the container, you can find it by inspecting the container:
docker container inspect containername