简体   繁体   中英

How to expose custom port (TCP) from Docker container using VisualStudio docker debugger (launchSettings.json profile)?

I want to be able to debug docker containers in Visual Studio by specifying Docker profile in launchSettings.json. However, launchSettings.json provide only option to set httpPort and httpsPort (which are mappings from 80 and 443 to custom ports outside of the container).

I have a TCP server application (TCPListener) that listens on custom port (let's say, 9999). I want to expose this port outside of the container during debugging. Can I do this somehow (by editing launchSettings.json or some other configuration) or I'm stuck with running Docker image separately and attaching to it somehow?

This is now possible. In 'launchSettings.json', add a 'Docker' profile with the below properties. For example;

"Docker": {
      "commandName": "Docker",
      "httpPort": 54623, 
      "environmentVariables": {
        "ASPNETCORE_URLS": "http://+:5000",
        "ASPNETCORE_ENVIRONMENT": "Development"
      }

    }

The above configuration will forward the Host port 54623 to the Docker port 5000 . The Docker port 5000 will be automatically exposed during debugging with this profile.

I figured it out. I'm still not sure if it's possible via launchSettings.json, but at least this is the solution that works for me:

Add

<DockerfileRunArguments>-p 9999:9999</DockerfileRunArguments>

to PropertyGroup of *.csproj file, these command line arguments will be appended to Docker RUN command before starting the container. The only issue I see with it is that now we have port number duplicated in multiple places.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM