Docker Community Forums

Share and learn in the Docker community.

Question regarding hostconfig.json vs config.v2.json


(Williamclarkmcbride) #1

I’m working on a python script to change the port bindings for a container (without having to create a new container to do so, which seems like a really poor substitute for simply changing the port). According to what I’ve found from searching around, you can do this by stopping the container (and the Docker engine itself) and then changing the port binding entries in hostconfig.json and/or config.v2.json. The and/or part is the confusing part, and looking at the files themselves doesn’t give me much more clarity. For example:

  1. I made a test Nginx container without setting any port bindings in the run command. Docker ps shows it as using 80/tcp for the port. config.v2.json has this entry: “ExposedPorts”:{“80/tcp”:{}} while hostconfig.json shows “PortBindings”:{} with nothing in it. Note that I don’t get the nginx landing page when I try to navigate to the page in a browser

  2. I made another test Nginx container, this time binding port 80 on the container to port 20000 on the server. The config.v2.json file has an entry of “ExposedPorts”:{“80/tcp”:{}} and “Ports”:{“80/tcp”:[{“HostIp”:“0.0.0.0”,“HostPort”:“20000”}]} while the hostconfig.json file only has the entry “PortBindings”:{“80/tcp”:[{“HostIp”:"",“HostPort”:“20000”}]} and this time it works when I go to servername.companyname.com:20000 in a browser

  3. The last one (and the one that prompted me to make the python script in question) is from a Docker-Compose stack for Netbox. In the docker-compose.yml file that created the stack, the only port binding given for the Nginx container in the stack is 8080, with no port on the host defined. At least according to the documentation this means that Docker picks an ephemeral port any time the container is stopped and started again. Which is why I need this script, I’d like to set up a permanent port binding for the container without having to recreate the container and integrate it into my compose stack. Anyways, looking at the files for this container I see “ExposedPorts”:{“80/tcp”:{},“8080/tcp”:{}} and “Ports”:{“80/tcp”:null,“8080/tcp”:[{“HostIp”:“0.0.0.0”,“HostPort”:“32772”}]} though the “HostPort” entry changes every time the container is restarted. The hostconfig.json contains “PortBindings”:{“8080/tcp”:[{“HostIp”:"",“HostPort”:""}]} and nothing else.

I also noticed that config.v2.json shows “Ports”: null when a container is stopped. So it appears to me that hostconfig.json contains the permanent config of the container, while config.v2.json shows the running config, and is generated whenever the stopped container is started. So presumably to change the port binding of an existing container I would only need to change it in hostconfig.json, and not in config.v2.json?