简体   繁体   中英

How to set service url http://localhost:8080 for running 'docker-compose up web'

I'm relatively new to Docker and I was asked to make my C# WebAPI (.NET 6) start with the url http://localhost:8080 when running the command 'docker-compose up web'.

I've read many articles but I still haven't found a way to achieve this.

I'm using Visual Studio 2022.

Can someone help, please?

If your app listens on port 80 like it'll do unless you've changed the configuration, then you map port 80 on the container to port 8080 on the host using a ports: section like this in your docker-compose.yml file

services:
  webapi:
    build:
      context: ./api
    ports:
      - 8080:80

Then you'll be able to access it on http://localhost:8080/

Be aware that Swagger isn't available by default when you run a .NET app in a docker container, so you have to access the 'real' API endpoints. If you want Swagger to be available, you need to set the environment variable ASPNETCORE_ENVIRONMENT to 'Development'.

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