How to keep Visual Studio or Docker from changing the host port numbers?

In your projects launchSettings.json add the following items (in yellow):

enter image description here


I found I can do this by editing launchSettings.json and specifying the values there. This is similar to Arjun's answer, but as far as I can tell there's no UI to add the port numbers like when selecting IIS Express - so you have to edit the file directly.

enter image description here

Update: Actually, in my case, I still had problems getting the port numbers to be consistent after doing this. What worked for me was right-clicking on the project and adding "Container Orchestration Support". Then set the port numbers in the docker-compose project. This helped with overriding environment variables as well.

Update 2: For anyone changing the port numbers in launchSettings.json, you may also need to delete any existing containers and/or images for the changes to take effect. Cleaning the solution wasn't sufficient for me. I had to delete them from Docker Desktop.


After adding docker-compose orchestration support from within VS2019 I had the same issue. I fixed it by changing

    ports:
      - "80"
      - "443"

to

    ports:
      - "80:80"
      - "443:443"

According to the docker-compose documentation, ports are configured in the following way: -

Either specify both ports (HOST:CONTAINER), or just the container port (an ephemeral host port is chosen).

So since the host ports aren't defined in the default VS2019 docker-compose file, These are chosen ephemerally. I had to look up the definition of this word which appears to be a synonym of transient.

lasting for a very short time.


For me, I was able to find it with the following steps in Visual Studio 2017:

  1. Right click on the web project, then click Properties.
  2. Click on the Debug tab.
  3. Under the Profile IIS Express, you will find the port at the App URL box.

For more, visit: Docker Port Managing