Change ports and mount of an existing container

Hi there,
I did not find an answer to this seemingly easy Problem.

I need to change and add ports and mounts to an existing Container in Docker Desktop on ubuntu.

Is there a way to do that?
I tried Docker commit, but while I can start and stop the container via docker start and docker stop I always get an error message no such container when trying to commit the container.

Thank you so much for your help.

No, there is no way and if you need that, that means you are using containers wrong. Not to mention that Docker Desktop is for development so you shouldn’t need to have a longrunning service that you can’t recreate :slight_smile:

docker commit just saves the containers filesystem and some metadata as a new imagelayer and adds an alias to it so you can move the image and start it somewhere else. Using docker commit is usually a bad practice and it will never save anything related to the host machine. So no ports and no mounted folders.

Hello,

Yes, there is a way to change and add ports and mounts to an existing container in Docker Desktop on Ubuntu. You don’t need to use docker commit for this purpose.

To change ports, you can stop the container using docker stop [container_name], then remove it with docker rm [container_name], and finally run a new container with the updated port mappings using the docker run command.

For adding mounts, you can also stop and remove the container, then run a new container with the additional mounts specified in the docker run command.

The error message “no such container” when trying to commit might be due to incorrect container name or ID. Make sure you are using the correct name or ID in the command.

Remember to back up any important data in the container before making changes.

and yet you are suggesting to remove the container so the answer is still no, there is no way to add these new definitions to an existing container especially not in Docker Desktop.

hi, a bit of an old issue. But I had to solve it for myself. I am running a development server which means I have to add new ports all the time. So I have script that just adds ports for me. The flow is something like this:

  1. Stop all running containers
  2. Stop dockerd
  3. Change the /docker/containers/{id}/hostconfig.json and the config.v2.json
  4. Start dockerd
  5. Start all docker containers.
    Some people might say “well, you can just commit to an image”. But commiting takes more time in my case(also, I have an automatic web portal which allows me to do specific actions on specific containers based on their id’s, so it would be better to keep the same id after changing a port).
    I think mounts should be really similar.

First of all this topic is in the Docker Desktop category,in which you can’T just stop the daemon easily as you would do with Docker CE.

You should never rely on container IDs. If you really want to do something with containers, you can set labels.

And if you don’t rely on container IDs, recreating a single container is much faster then stopping thr entire docker daemon and starting everything again.

So you also saved dta on the container’s filesystem without volumes? That is the only reason can imagine people thinking about commit, but the only time I used commit was when I just started to use Docker and I didn’t know what I was actually doing.

If anyone saves data on a container’s filesystem without volumes, the best solution is copying the data out and bind mounting it or moving it to a named volume. Then recreating the container is still the best you can do if you want to add a new port.

So while it is great to learn about how Docker works internally, it is absolutely not recommended editing anything manually in the docker data root.

The reason I rely on ID’s can’t be shared but It’s not my fault.
Also, yes there are changes to the container filesystem because it is a development server. A lot of people connect to their container with ssh and use it like a vm.
The entire script makes the system run faster(I run it only on maintenance day as no one uses the server at that day). The previous solution was copying the root of the container filesystem to a tmp mount and rebuilding the entire docker image with the new data at the root(this took a lot of time and space in the machine).
So yeah, I agree it is not ideal. But it is a solution(also, I never worked with docker desktop, so I did not know you can’t stop the deamon).
Anyway, just wanted to try and help with people asking things about docker and i thought this would be a great place to start as I already solved this issue for myself.