Deploying in .NET Core with Docker

On Heroku and AWS

Hi! Here is our new post about .NET Core. In the previous post, apply SCSS for styles Using Gulp and Sass.

In this post, we are going to show you how to use Docker in a .NET Core project and how we can deploy an application on Heroku and AWS. We are going to use the sample project from the previous posts to add docker and deploy the web app. So, let’s do this!


Personal project experience

At Wolox we were working with a client who did not the host where we were going to deploy the application, with 4 possibilities to do this. There were 4 possibilities to do this, meaning we had to deploy the application in potentially 4 different types of servers. As a solution we found Docker. It allowed us to have a deployment mechanism in a single container, which adapts to any of the 4 options we had. Therefore, it let us work in an agile manner and allowed our clients to choose the server that best suits their needs.

Docker

With Docker we can create lightweight and portable containers so that software applications can run on any machine with Docker installed. This way, Docker allows putting all the things that my application needs to be executed and the application itself in a container. Ultimately facilitating the deployments.

To use Docker in our project we need to install it. Once Docker is installed we need to create a Dockerfile.

We are going to use this for our project

When we have our Dockerfile ready, all we need to do is run the following command to create the docker container

docker build -t netcore-project.

Heroku

First, we need to Install Heroku CLI and login to Heroku with the following commands

heroku login

heroku container:login

Then, create the heroku app with heroku apps:create net-core-deploy-heroku and tag the Heroku target image with this command docker tag netcore-project registry.heroku.com/net-core-deploy-heroku/web (where nectore-project is our docker image name and net-core-deploy-heroku is the heroku app name)

And finally, push the docker image to Heroku with this command

docker push registry.heroku.com/net-core-deploy-heroku/web

AWS

AWS has the possibility of directly building a Docker image, allowing us to push our code (with a valid Dockerfile).

First, we need to download the Elastic Beanstalk Client. To do this we can follow the User guide installionon and the EB Cli3 install guide.

Then, we need to configure your access credentials, creating a file in your root folder ~/.aws/credentials. It should look like this:

[profile_name]
aws_access_key_id = your_access_key
aws_secret_access_key = your_access_key_secret

Create a Dockerrun.aws.json file in the root directory of your application. It should look like this:

We need to initialize Elastic Beanstalk in our application with eb init--profile profile_name

Here, we will be required to select the AWS region you will be deploying to, along with the EB container. We may also be required to select the Docker Version you will be using.

We can configure our environment variables from the AWS console, by going to Configuration, then Software. Keep in mind that once you click on Apply, our environment will be restarted.

Finally, we can deploy our application with eb deploy (If you are required to select an environment, you can list them using eb list), and deploy using eb deploy environment_name

Awesome!

We saw how to use Docker and how to deploy our .NET Core application on Heroku and AWS! 
In the next post, we are going to see how to Testing in .NET Core 2.0.