Simsons
Simsons

Reputation: 12745

How to access WebAPi after deploying on docker

I have my Dockerfile:

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /build
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o output

FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY  --from=build /build/output .
ENTRYPOINT ["dotnet","TestDockerApi.dll"]

I am creating an image using :

docker build -t testdocker/api .

and then running a container from image using :

docker run testdocker/api

I can see following message on my console:

Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.

I am trying to access using http://localhost/app/TestDockerApi/Values , but it does not work.

Do I need to use docker image IP to access that .

I can see few tutorials suggesting to do this in Entrypoint :

ENTRYPOINT ["dotnet","TestDockerApi.dll","--server.urls","http://0.0.0.0:5000"]

And then while running the container, mapping the port:

docker run -p 80:5000 testdocker/api

Is there any way I could access the API with out using portforwarding? I am just trying to get the basics right , why and what should I do.

Upvotes: 1

Views: 4947

Answers (1)

muratiakos
muratiakos

Reputation: 1092

The Dockerfile does not manage network configuration outside of the container at all. If you want docker to listen on your host port of 80, you need to bind it when you run your container.

docker run -80:80 testdocker/api

For more description about mapping and exposing ports, you can read here: - https://www.ctl.io/developers/blog/post/docker-networking-rules/

Alternatively you can create your own service composition where you specify these details and specify this in a docker-compose.yml file

api:
  image: testdocker/api
  ports:
    - "80:80"

And then you can simply run with

docker-compose up 

More information is at:

Upvotes: 4

Related Questions