Docker Community Forums

Share and learn in the Docker community.

[SOLVED] Edit container details (ports and restarts, etc.)


(Niall Deakin) #1

Hi

Sorry if this has been asked a thousand times before, but I was wondering to edit the ports once the container has been created.
A lot of places have said no, so it would have to be ran again, but I’ve found two places which said it could happen (links below).

I’ve tried these, but in the end, the container is “lost”. It doesn’t show on ps -a and I cannot start it.

So, since some of these are from a few years ago, I’d like to know if this is still the same case.

It would help for when servers need to be rebooted as the port numbers change and then I have to change the settings.
It also helps because some containers have a lot of ports when created, and it would be easier to edit after they container is created (rather than listing each port and assigning a number).

Update: is there a way to modify the containers as a whole? As I’ve learnt that I’ll need to enable automatic restarts too.

Update: Solved
Okay, so I’ve figured it out.

I found a couple of pages which led me to the solution, but didn’t actually provide it clearly, so here I go.

  1. Get the container ID (a really long number at the top) with docker inspect [container name]
  2. Stop the container with docker stop [container name]
  3. Edit the container hostconfig file, found at /var/lib/docker/containers/[container ID]/hostconfig.json
  4. Within the PortBindings section, either edit the existing HostPort to whichever port you’d like, or add em yourself (see below)
  5. Save and exit the config file
  6. Restart docker with (sudo) systemctl restart docker
  7. Start up the containers `docker start [container name]

Example config file
I assigned the regular port (i.e. 80) with the last digit of the IP (in this case 2) to create a unique yet manageable system.
NB: I also formatted this to remove spaces, indent, and linebreaks to fit in with the style of the file

"PortBindings": {
	"3306/tcp": [
		{
			"HostIp": "",
			"HostPort": "23306"
		}
	],
	"443/tcp": [
		{
			"HostIp": "",
			"HostPort": "2443"
		}
	],
	"80/tcp": [
		{
			"HostIp": "",
			"HostPort": "280"
		}
	]
}

Links to resources:
https://stackoverflow.com/a/38783433/4546725
https://mybrainimage.wordpress.com/2017/02/05/docker-change-port-mapping-for-an-existing-container/


(Fsejoseph) #2

Depends on the port? Are you using Docker EE UCP?


(Niall Deakin) #3

Thanks for the response.

No, I’m using CE and editing directly via the terminal.