Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

enter image description here

Where do I configure it to always use the same port numbers?

like image 715
adam0101 Avatar asked Jan 03 '20 15:01

adam0101


People also ask

Does Docker expose a port by default?

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.

Can Docker container access ports on host?

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.

Can Docker expose multiple ports?

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.

How to find the port number of a docker container?

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.

How do I use Docker container in Visual Studio Code?

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.

How do I manage ports in Docker?

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.

How to prevent random port numbers during Docker run in vs2019?

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.


Video Answer


2 Answers

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.

like image 182
adam0101 Avatar answered Oct 06 '22 22:10

adam0101


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

enter image description here

like image 40
Arthur Kater Avatar answered Oct 06 '22 23:10

Arthur Kater