英文:
How to keep Visual Studio or Docker from changing the host port numbers?
问题
我已将我的Web应用程序Docker化,并可以在Visual Studio中运行它,但它选择使用的主机端口号不一致。
我应该在哪里配置它以始终使用相同的端口号?
英文:
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?
答案1
得分: 66
我可以通过编辑 launchSettings.json
并在那里指定值来实现这一点。这类似于Arjun的回答,但据我所知,没有UI可以像选择IIS Express时那样添加端口号 - 因此,您必须直接编辑该文件。
更新: 实际上,在我的情况下,即使这样做后,端口号仍然不一致。对我有用的是右键单击项目并添加“容器编排支持”。然后在docker-compose项目中设置端口号。这也有助于覆盖环境变量。
更新 2: 对于任何在 launchSettings.json
中更改端口号的人,您可能还需要删除任何现有的容器和/或映像以使更改生效。对我来说,清理解决方案还不够。我必须从Docker Desktop中删除它们。
英文:
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.
答案2
得分: 9
在你的项目 launchSettings.json 中添加以下项目(黄色部分):
答案3
得分: 7
我在docker-compose.yml中设置了端口,但没有起作用。
原来被docker-compose.override.yml覆盖了。
您可以选择:
- 在override中设置端口(始终适用)
- 在docker-compose.yml中更改它并从override中删除它(然后您可以为不同的配置设置不同的端口)
英文:
I set the port in docker-compose.yml, but it didn't work.
Turns out it was overriden by docker-compose.override.yml.
You can either:
- set the ports inside override (applies always)
- change it in docker-compose.yml and remove it from override (you can then set different ports for different configurations)
答案4
得分: 6
没有用户界面,但您可以修改.csproj项目文件以添加DockerfileRunArguments
<PropertyGroup>
<DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
<DockerLaunchUrl>http://{ServiceIPAddress}</DockerLaunchUrl>
<DockerfileRunArguments>-p 8080:80</DockerfileRunArguments>
</PropertyGroup>
编辑:
如果您使用的是NetCore,那么可以使用launchSettings.json解决方案。对于框架,您可以使用"Container Orchestration Support"或这个hack。
英文:
There is no UI but you can modify the .csproj project file to add DockerfileRunArguments
<PropertyGroup>
<DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
<DockerLaunchUrl>http://{ServiceIPAddress}</DockerLaunchUrl>
<DockerfileRunArguments>-p 8080:80</DockerfileRunArguments>
</PropertyGroup>
Edit:
If you are using NetCore then you can go with the launchSettings.json solution. For the framework, you can either use "Container Orchestration Support" or this hack.
答案5
得分: 3
在从VS2019中添加了docker-compose编排支持后,我遇到了相同的问题。我通过将以下内容进行更改来解决它:
ports:
- "80:80"
- "443:443"
根据docker-compose文档,端口的配置方式如下:
要么同时指定
ports (HOST:CONTAINER)
,要么只指定容器端口(将选择一个临时主机端口)。
因此,由于默认的VS2019 docker-compose文件中没有定义主机端口,这些主机端口被选择为临时的
。我不得不查找这个词的定义,它似乎是短暂的
的同义词。
英文:
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.
答案6
得分: 2
I tried setting "httpPort": 8080
in launchSettings.json, but it didn't work at all, even after removing and recreating the container.
What worked for me was:
Right click project in Visual Studio > Add
> Container Orchestration Support
(if you've set this up already, you don't need to do it again)
This sets up a docker-compose.yml
file, with a Dockerfile
for the app. There's also a docker-compose.override.yml
file that's used for overriding the docker-compose.yml
with environment-specific settings (e.g. for dev, or prod). By default, this exposes the container port as port 80
, but doesn't expose a fixed host port to map the container port to. To change it (e.g. to map a fixed host port of 8080
):
version: '3.4'
services:
coreapp:
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "8080:80"
Note the use of double quotes i.e. "8080:80"
英文:
I tried setting "httpPort": 8080
in launchSettings.json, but it didn't work at all, even after removing and recreating the container.
What worked for me was:
Right click project in Visual Studio > Add
> Container Orchestration Support
(if you've set this up already, you don't need to do it again)
This sets up a docker-compose.yml
file, with a Dockerfile
for the app. There's also a docker-compose.override.yml
file that's used for overriding the docker-compose.yml
with environment-specific settings (e.g. for dev, or prod). By default, this exposes the container port as port 80
, but doesn't expose a fixed host port to map the container port ot. To change it (e.g. to map a fixed host port of 8080
):
version: '3.4'
services:
coreapp:
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "8080:80"
Note the use of double quotes i.e. "8080:80"
答案7
得分: 0
在Visual Studio 2017中,我能够通过以下步骤找到它:
- 右键单击Web项目,然后单击属性。
- 点击“调试”选项卡。
- 在IIS Express配置下,您将在“应用程序 URL”框中找到端口。
更多信息,请访问:Docker端口管理
英文:
For me, I was able to find it with the following steps in Visual Studio 2017:
- Right click on the web project, then click Properties.
- Click on the Debug tab.
- Under the Profile IIS Express, you will find the port at the App URL box.
For more, visit: Docker Port Managing
答案8
得分: 0
我不需要翻译的代码部分:
我遇到了同样的问题,不得不修改 docker-compose 项目中的 docker-compose.override.yml 文件。
最初,端口部分看起来像这样(这导致了自动端口分配):
ports:
- "80"
- "443"
我将其更改为:
ports:
- "8080:80"
- "56850:443"
这样强制调试使用指定的端口。
英文:
I had the same issue and had to modify docker-compose.override.yml in the docker-compose project.
Originally, the ports section looked like that (which was causing automatic port assignments):
ports:
- "80"
- "443"
I changed it to
ports:
- "8080:80"
- "56850:443"
And that forced the Debug to use the ports specified.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论