How to deploy and host an ASP.NET Core app on Linux for $5 a month on DigitalOcean
I originally wrote this as a single post to document how I deployed an ASP.NET Core application on DigitalOcean. I also setup a web server using Nginx and setup SSL for free using Lets Encrypt and finally added some basic security with Fail2ban.
I soon realised, it became rather large, so I have broken it down into individual sections:
- How to setup a Linux machine with Docker on DigitalOcean
- How to deploy and host an ASP.NET Core app on Linux for $5 a month - this post
- How to add SSL for free for a ASP.NET Core app on Linux using Nginx and Lets Encrypt
- How to secure and protect Nginx on Linux with Fail2ban
In this post, I will detail the steps involved in actually deploying an ASP.NET Core application from a local machine onto a hosted Linux machine. In this example, I am using .NET Core 2.1 and GitLab's free Container Registry. I am going to demonstrate how to build a docker image, push and upload it to my personal GitLab Container Registry. Then I will download this docker image from GitLab to a DigitalOcean droplet in order to run it.
If you want to get started on DigitalOcean you can get some free credits using this link. At the time of writing its $100 free credit! And you can follow this guide on how to setup a Linux machine with Docker on DigitalOcean
If you've not heard or come across GitLab, its very similar to Github. It offers git source control as well as a whole host of devops, deployment and CI pipeline tools. It also offers a free Docker Container Registry. What does this mean? It means that we can upload our personal docker images into GitLab and download them whenever we need them.
Summary of steps
- Create a Container Registry in GitLab.
- Build and push a docker image into GitLab.
- Deploy the docker image into DigitalOcean.
Create a free GitLab Container Registry account
Create a GitLab account if you don't have one already. You are not restricted to use GitLab for this walk-through. Any Docker Container Registry will work.
Once a GitLab account is created, create a project as shown below:
Then you should be able to access the Registry section within the project, like so:
Make a note of this, as this will be used later.
Create a Docker image using Docker Compose
Here we will create a docker image on a local machine for preparation for pushing into a GitLab's Docker Container Registry.
I will be using a sample app which I created on Github.
Clone the DockerAspNetCoreDemo Repo from Github.
In the same directory, create a docker compose file which is able to build the docker image. Or you can use the one that is already provided
Something like this:
version: '3.4' services: dockeraspnetcoredemo: build: context: . dockerfile: DockerAspNetCoreDemo/Dockerfile image: dockeraspnetcoredemo
- Run the command below to build a docker image
docker-compose -f docker-compose-build.yml build
Now lets list the docker images to verify it has been built.
REPOSITORY TAG IMAGE ID CREATED SIZE dockeraspnetcoredemo latest 52c42756d5d6 6 seconds ago 258MB
- Now, tag the docker image as shown below.
docker tag dockeraspnetcoredemo registry.gitlab.com/ch-lee/aspnetcoredemo:latest
docker images we should see the original image and the tagged image.
REPOSITORY TAG IMAGE ID CREATED SIZE registry.gitlab.com/ch-lee/aspnetcoredemo latest 52c42756d5d6 About a minute ago 258MB dockeraspnetcoredemo latest 52c42756d5d6 About a minute ago 258MB
Pushing into a Docker Registry
Now that we have the docker image built and tagged, we can push this into the Container Registry.
- Push the tagged image into GitLab. If its the first time, you will need to login into GitLab by executing the following command:
docker login registry.gitlab.com
- Push the image into GitLab:
docker push registry.gitlab.com/ch-lee/aspnetcoredemo:latest
- Confirm that it is present in GitLab.
Deploying on Linux
In this example, I'm using a Linux machine on DigitalOcean, however, this principle applies to any Linux machine that has Docker installed.
- Connect to the Linux machine using ssh.
ssh [email protected]
- In a preferred directory, I just created a folder called
/home/deployCreate a docker compose file called
- Create a docker compose file as shown below:
version: '3' services: aspnetcoredemo: image: registry.gitlab.com/ch-lee/aspnetcoredemo:latest ports: - "80:80"
Notice that the image specified is the one that was uploaded to GitLab. Also, the
ports mapping has been specified. This is in the format of "Host:Container". This means that the host is on port 80 and the container is also on 80.
- Execute the docker compose command
docker-compose -f /home/deploy/docker-compose-release.yml up -d
- Verify that the docker container is up and running
- You should see something like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5b680bd8e631 registry.gitlab.com/ch-lee/aspnetcoredemo:latest "dotnet DockerAspNet…" 22 minutes ago Up 22 minutes 0.0.0.0:80->80/tcp deploy_aspnetcoredemo_1
- Verify that you can browse to the app from a browser and you should see something like this:
Summary and next steps
As demonstrated above, you can see that an ASP.NET core web application can be deployed relatively easily on a Linux machine using Docker and GitLab's Container Registry.
To take it further, for instance, you may have a side project that you would like to show the world, you may want to add https and also assign a domain name against it.
In order to this, you can checkout my other post on how to add SSL for free for a ASP.NET Core app on Linux using Nginx and Lets Encrypt