I have dockerized my web application and can run it within Visual Studio, however, the host port number it chooses to use is not consistent.
Where do I configure it to always use the same port numbers?
By default, the EXPOSE instruction does not expose the container's ports to be accessible from the host. In other words, it only makes the stated ports available for inter-container interaction.
When running Docker natively on Linux, you can access host services using the IP address of the docker0 interface. From inside the container, this will be your default route. This would permit access to any ports on the host from Docker containers.
2.1 Use port range for exposing multiple Docker PORTS Docker provides a port range that can be used for exposing multiple numbers of ports.
With the container running (F5 debug, command line) - execute the following command: "docker ps -a" You will see a list of running containers; there's a column called "Ports". Find the entry for your container - probably the solutionname:dev. Look at the ports column, you will see AA -> BB.
Developing inside a Container The Visual Studio Code Remote - Containers extension lets you use a Docker container as a full-featured development environment. It allows you to open any folder inside (or mounted into) a container and take advantage of Visual Studio Code's full feature set.
Docker - Managing Ports. In Docker, the containers themselves can have applications running on ports. When you run a container, if you want to access the application in the container via a port number, you need to map the port number of the container to the port number of the Docker host.
For the framework, you can either use "Container Orchestration Support" or this hack. DockerfileRunArguments seems to be the simplest way to prevent VS2019 from cooking up random port numbers during docker run, without the need for an explicit docker-compose.override.
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.
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.
In your projects launchSettings.json add the following items (in yellow):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With