In this tutorial, we will be deploying a .NET 7 app on Railway using the Railway CLI and a Dockerfile. We will be using the .NET 7 SDK to build a minimal API app and then deploy it on Railway.
Imagining this is our app's source code:
/MySolution --/MyAppProject ----/[*Your MyAppProject project source code*] --/MySolution.sln
Here's our Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base WORKDIR /app EXPOSE 80 EXPOSE 443 FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build WORKDIR /src COPY ["MyAppProject/MyAppProject.csproj", "MyAppProject/"] RUN dotnet restore "MyAppProject/MyAppProject.csproj" COPY . . WORKDIR "/src/MyAppProject" RUN dotnet build "MyAppProject.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "MyAppProject.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "MyAppProject.dll"]
If you are having your solution and project in the same folder, here's how your Dockerfile should almost look like 👇:
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base WORKDIR /app EXPOSE 80 EXPOSE 443 FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build WORKDIR /src COPY ["BallApp.csproj", "./"] RUN dotnet restore "BallApp.csproj" COPY . . WORKDIR "/src/" RUN dotnet build "BallApp.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "BallApp.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "BallApp.dll"]
Now, in order for Railway CLI to be aware of our dockerfile and the start command, we need to make a `railway.toml`
file with this content
under our `/MySolution`
folder:
[build] builder = "dockerfile" dockerfilePath = "./MyAppProject/Dockerfile" [deploy] startCommand = "dotnet MyAppProject.dll" restartPolicyType = "never"
Go to your `Program.cs`
and add these two lines before `var app = builder.Build();`
line.
var port = Environment.GetEnvironmentVariable("PORT") ?? "8081"; builder.WebHost.UseUrls($"http://*:{port}");
This will allow Railway to
inject the port value from the environment variable `PORT`
to your app. (Railway will set the `PORT`
environment variable for you.)
You can now access your app on `http://localhost:8081`
if you haven't set any `PORT`
Environment Variable.
For development purpose, comment out the line that forces HTTPS redirection in your `Program.cs`
.
// app.UseHttpsRedirection();
`/MySolution`
folder in your command line shell.`railway login`
and follow the instructions to login on your railway account.`railway run dotnet run --project ./MyAppProject`
and make sure that your app is running (this is on your local machine)`/MySolution`
working directory), run `railway link`
and then choose your project that you want to link your .NET app to. (There will be a list of your railway projects by their names.)`railway up`
and wait until your deployment builds, and deploys on railway. You can checkout the deployment starting on your Railway project (service) deployment page.Although there are many other platform that allow you to deploy a .NET app, Railway is one of the few that simplifies the deployment process and makes it easy for a developer to instantly host their app so that they can share it with others and get feedback on it as quick as possible.
No more headaches with configuring complex deployment pipelines such as AWS, Azure, GCP, etc. 🤦
I hope you found this tutorial helpful. If you have any questions, feel free to DM me on Twitter. I'll be happy to help. Or just come and say hi. 😊