docs icon indicating copy to clipboard operation
docs copied to clipboard

Dockerfile example dockerfile work with $port

Open nguyenloki258 opened this issue 4 years ago • 2 comments

Hi, i'm trying deploy using Dockerfile. I dont know how to mapping port . Please help

nguyenloki258 avatar Feb 11 '22 04:02 nguyenloki258

I think this is a valid concern and we have been getting these questions a lot. We can address this in an upcoming PR.

ndneighbor avatar Feb 18 '22 01:02 ndneighbor

If you are using Railway-Provided Domain (haven't yet tested it on custom domains) then the mapping is 443:$PORT

For example, if you Railway-Provided Domain is "foo.up.railway.app" and you successfully deployed a github repository with a Dockerfile then all requests to "https://foo.up.railway.app:443" will be transferred to container port defined by $PORT.

You can define $PORT in Varables and use it in Dockerfile to configure your dotnet core app like this:

Lets say: PORT environment variable is 7001 Railway-Provided Domain is "foo.up.railway.app"

and Dockerfile looks like this:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app

COPY . ./

RUN dotnet restore
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .

ARG PORT
ENV PORT $PORT
ENV ASPNETCORE_URLS=http://+:$PORT

EXPOSE $PORT/tcp

RUN echo "ASPNETCORE_URLS: [$ASPNETCORE_URLS]"
RUN echo "PORT: [$PORT]"

ENTRYPOINT ["dotnet", "my-project.dll"]

When the project with this Dockerfile is deployed you should see Deploy Logs like this:

info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://[::]:7001
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /app/

So now all requests to "https://foo.up.railway.app:443" will hit your app inside the container on port 7001

TadijaB avatar Sep 25 '22 08:09 TadijaB