How to Deploy your .NET App on Railway using Docker

2 min read|

March 23, 2023

Share on TwitterShare on LinkedIn

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.

Before starting, make sure you have:

  • Installed .NET 7 SDK.
  • Installed Docker and ran its service. I recommend using Docker Desktop to get started with Docker quickly.
  • Installed Railway CLI
  • (Optional but recommended) Installing and using Rider (a Fast & powerful cross-platform .NET IDE by JetBrains) to use in this tutorial.

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();

Playing with Railway and its CLI

  1. Navigate to `/MySolution` folder in your command line shell.
  2. Run `railway login` and follow the instructions to login on your railway account.
  3. (optional) run `railway run dotnet run --project ./MyAppProject` and make sure that your app is running (this is on your local machine)
  4. On your railway, "Start a New Project" and then choose the "Empty project" from the dropdown menu.
Railway new Project
  1. Add a Service. and choose "Empty Service".
Railway add Service
  1. On you terminal, (staying in the `/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.)
  2. Finally, run `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.
  3. And maybe generate a new domain so that you can access your app via that domain.

Conclusion

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. 😊

References

Contact