This is supposed to bind the container’s internal port 8010 to 127.0.0.1:55001 on the host.
However, the container randomly switches to another port http://127.0.0.1:55000, without me changing anything. I have not published port 55000, and I am only running one container. No dynamic ports (-P) are being used.
Why is Docker ignoring the fixed port mapping after a restart, and how can I stop it from randomly switching to a different port like 55000?
Please, explain the issue in more details. Docker cannot change the port once you started the container. You couln’t do manually, and it will not do automatically. If the app in the container redirects to another URL that is not done by Docker.
Let me explain how I found the issue — perhaps presenting it step by step will clarify the whole matter.
I mounted a LanguageTool image.
I then created a container to run on port 55001 and set it to restart automatically, simply to save time having to do it manually. Note that this is the only container I have.
Next, I entered that port into the relevant extensions or applications that use the LanguageTool container — for example, the LanguageTool plugin for Obsidian, the LanguageTool add-on for Firefox, and the LanguageTool app for Mac.
The connection to the API initially worked correctly in all of these applications and extensions.
However, after several hours, I suddenly noticed that the connection had been lost.
I opened the Docker app and saw that the container was now running on 55000 instead of 55001. That struck me as extremely odd. This forced me to, again, copy the new port and paste it on the LanguageTool app and extensions to re-establish the connection.
This has happened twice.
I assume that Docker, when attempting to restart the container, may have failed to bind it to the original port and instead assigned the default port (55000). However, this is only speculation.
Can you show the command you used to create the container?
If you could try to reproduce the issue again and run
docker container ls
before and after the issue, that could help too. If it is a bug, it can be reported. If it is just using something wrong, we can help. But normally Docker and Docker Desktop cannot change ports unless you intentionally ask for random port numbers. But that would only change when recreating the container, not restarting.