Deploy .NET App Without Dockerfile or Docker Build
In this article
This guide shows how to deploy a .NET application to a server or cloud service without using a Dockerfile or the docker build process. Instead, you'll use the .NET SDK's built-in publish command to create a self-contained or framework-dependent deployment package, then transfer it to the target machine and run it directly. This approach is faster for simple deployments, avoids Docker overhead, and works in environments where Docker isn't available.
What You'll Need
- A .NET application (any version, but the steps assume .NET 6 or later; adjust commands for older versions).
- .NET SDK installed on your development machine (download from the official .NET site).
- Access to the target server or machine (SSH, FTP, or a shared folder).
- If deploying to a Linux server, ensure the target has the necessary runtime if you choose a framework-dependent deployment; for self-contained, no runtime is needed.
Step-by-Step
- Open a terminal in your project directory. Navigate to the folder containing your .csproj file.
- Choose a deployment mode. Decide between framework-dependent (requires .NET runtime on the server) or self-contained (includes runtime, larger but standalone). For simplicity, this guide uses self-contained for Linux, but you can adapt.
- Run the publish command. Execute the following in the terminal:
dotnet publish -c Release -r linux-x64 --self-contained true -o ./publish
Replacelinux-x64with your target runtime (e.g.,win-x64,osx-x64). The-ospecifies the output folder. - Verify the output. Check the
publishfolder. It should contain your app's executable (e.g.,MyAppon Linux,MyApp.exeon Windows) and all necessary DLLs. - Transfer the publish folder to the target machine. Use SCP, FTP, rsync, or a shared drive. For example, on Linux:
scp -r ./publish user@server:/var/www/myapp
Ensure the destination folder exists and has appropriate permissions. - Set up the application on the server. On Linux, make the executable executable:
chmod +x /var/www/myapp/MyApp
If you're using a framework-dependent deployment, install the .NET runtime on the server first (see FAQ). - Run the application. Execute it directly:
/var/www/myapp/MyApp
For a web app, it will start listening on the configured port (e.g., 5000). To keep it running after you log out, use a process manager likesystemd(create a service file) ornohup. - Configure a reverse proxy (optional). For production web apps, set up Nginx or Apache to forward requests to your app's port. This is beyond this guide but recommended for security and performance.
Common Mistakes
- Wrong runtime identifier: Publishing for the wrong OS/architecture (e.g., publishing for win-x64 but deploying to Linux) causes a "not a valid executable" error.
- Forgetting to set permissions: On Linux, the executable bit must be set; otherwise, you get "Permission denied".
- Framework-dependent without runtime: If you choose framework-dependent and the server lacks the .NET runtime, the app fails with a missing runtime error.
- Not including all files: The publish folder must be transferred as a whole; missing DLLs cause runtime errors.
- Port conflicts: The app's configured port (e.g., 5000) might be in use; check with
netstatorss.
Tips
- Use
--self-contained falseif you want a smaller package and can install the runtime on the server. - For production, set the
ASPNETCORE_ENVIRONMENTenvironment variable toProductionto disable developer exception pages. - Consider using a publish profile (
dotnet publish -p:PublishProfile=FolderProfile) for repeatable builds. - For web apps, configure Kestrel to listen on a specific port via
appsettings.jsonor environment variables. - Test the published app locally before deploying to catch issues early.
Troubleshooting
- App won't start: "No such file or directory" - This often means the executable bit is missing or the runtime identifier is wrong. Re-publish with the correct RID and set permissions.
- App starts but immediately exits with an error - Check the logs. For framework-dependent apps, ensure the runtime is installed. For self-contained, the app should run without external dependencies.
- Port already in use - Change the port in your app's configuration or stop the conflicting process.
- Missing DLL errors - Ensure you transferred the entire publish folder, not just the executable.
- Permission denied when running - Run
chmod +xon the executable. - App runs but doesn't respond to requests - Check firewall rules and ensure the app is listening on the correct interface (e.g.,
0.0.0.0instead oflocalhost).
Frequently Asked Questions
Can I deploy without the SDK on the server?
Yes, if you publish a self-contained deployment, the server doesn't need the SDK or runtime. For framework-dependent, you need the .NET runtime installed.
What is the difference between self-contained and framework-dependent?
Self-contained includes the .NET runtime with your app, making it larger but independent. Framework-dependent requires the runtime to be installed on the server, resulting in a smaller package.
How do I install the .NET runtime on a Linux server?
Use the package manager (e.g., sudo apt install aspnetcore-runtime-8.0 for Ubuntu) or download the binary from the official .NET site.
Can I deploy to a Windows server using this method?
Yes, publish with -r win-x64 and transfer the folder to the Windows server. You may need to install the .NET runtime if framework-dependent.
How do I keep the app running after I close the terminal?
Use a process manager like systemd on Linux, or run it as a Windows service. For quick tests, use nohup.
Is it possible to publish for multiple platforms from one machine?
Yes, you can publish for different RIDs (e.g., linux-x64, win-x64) from the same machine, as long as you have the SDK installed.