.Net Core and Docker

Building .net core applications into Docker containers. I've spent a bit of time lately moving some of my older applications into docker containers in dotnet core 2.0 and 2.1.

I've only started exploring docker with dot net core and what we can do and so far it really has changed how I think about what I'm doing and the way I am building things.

Demo site: https://dotnetdocker.serversncode.com/ Source code for this site is available https://github.com/mordin/Serversncode-Docker-demo

With .Net Core being on the edge things change. I've started this post and the https://dotnetdocker.serversncode.com as a way to play around with docker and understand it more.

I've hosted the site on a linode server. For more on setting up linode with docker check out setting up docker on linode and nginx reverse proxy for docker.

Working with Docker in .Net Core

With a dotnet core app we need to add a dockerfile to the project. This will do the build of our application and load it into the docker container and build it.

Docker File

.Net Core 2.0

While 2.0 is not going to be supported in the long term it's where I'll start with that.

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY ServersncodeDemo.Web/ServersncodeDemo.Web.csproj .Web/
RUN dotnet restore

COPY . .
WORKDIR /src/ServersncodeDemo.Web
RUN dotnet build ServersncodeDemo.Web.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish ServersncodeDemo.Web.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "ServersncodeDemo.Web.dll"]

.Net Core 2.1

Next we move to .net core 2.1 which will be supported long term.

FROM microsoft/dotnet:2.1.402-sdk-alpine3.7 AS build
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.sln .
COPY ServersncodeDemo.Web/*.*.csproj ./ServersncodeDemo.Web/
COPY ServersncodeDemo.Data/*.*.csproj ./ServersncodeDemo.Data/
RUN dotnet restore

# copy everything else and build app
COPY ServersncodeDemo.Web/. ./ServersncodeDemo.Web/
COPY ServersncodeDemo.Data/. ./ServersncodeDemo.Data/
WORKDIR /app/ServersncodeDemo.Web
RUN dotnet publish -c Release -o out

FROM microsoft/dotnet:2.1.4-aspnetcore-runtime-alpine3.7  AS runtime
WORKDIR /app
COPY --from=build /app/ServersncodeDemo.Web/out ./
ENTRYPOINT ["dotnet", "ServersncodeDemo.Web.dll"]

Docker Build

I have a blog post https://serversncode.com/docker-commands/ that covers common commands.

First up we build our docker image and give it a name.

docker build -t NAME .

Next we run it locally.

docker run --name NAME -d -p 8080:80 NAME

That's it, I've run a container and pointed the port 8080 to the default port of 80. In my docker file I never changed the port from 80. To change the port of your docker file use the

EXPOSE 8040

command to tell it to use a different port than 80.

What next

Next I'll start getting into the details of the docker file how it's created and what it all means.

Building .net core applications into Docker containers. I've spent a bit of time lately moving some of my older applications into docker containers in dotnet core 2.0 and 2.1.I've only started exploring docker with dot net core and what we can do and so far it really…

Read More

Docker Commands

As I dig deeper and deeper into Docker. I'm finding some really useful commands that I have to keep using and reusing. As I use and learn more I will add to this guide.

Docker build command

docker build -t webapp .

Docker Tag and Push

docker tag webapp *.azurecr.io/webapp
docker push *.azurecr.io/webapp

Docker container management

Pull the latest image

docker pull *.azurecr.io/webapp

Run Container

docker run --name webapp -d -p 8090:80 *.azurecr.io/webapp

Run a container with a name of webapp, expose port 8090 and route to port 80 on the container.

Running containers

docker ps

Stop container

docker stop a456721

Remove container

docker rm a456721

This can only be run on stopped containers.

As I dig deeper and deeper into Docker. I'm finding some really useful commands that I have to keep using and reusing. As I use and learn more I will add to this guide.Docker build commanddocker build -t webapp .Docker Tag and Pushdocker tag webapp *.azurecr.io/webapp docker…

Read More

NGINX Reverse Proxy for Docker

This post is a guide on setting the NGINX Config to work as a reverse proxy to our web application inside a docker container. When you use docker to host your web applications and services. It's handy to be able to use a reverse proxy to pass the traffic on port 80 and route it to the right container.

One cool thing about NGINX is we can running docker and static sites on the same linux box. It's just a case of having different NGINX config files.

I've setup a web application built in .net core and running inside a container. On my linux server I create a docker instance and set it to run on port 8080.

docker run --name some-app1 -d -p 8080:80 some-app1

Once the docker container is running we can go ahead and create a NGINX config for the reverse proxy.

Reverse Proxy Config

First in the folder we need to create our config.

/etc/nginx/site-available

The following is our reverse proxy config for a website I have called 'touroperator.io'

server {
  listen 80;

  listen [::]:80;

  server_name touroperator.io www.touroperator.io;

   set $upstream 127.0.0.1:8080;

 location / {
 
 proxy_pass_header Authorization;
 proxy_pass http://$upstream;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_http_version 1.1;
 proxy_set_header Connection "";
 proxy_buffering off;
 client_max_body_size 0;
 proxy_read_timeout 36000s;
 proxy_redirect off;
 }
}

The config is doing a number of things.

  • Listen for traffic on Port 80
  • Only deal with requests for the touroperator.io or www.touroperator.io domain name
  • Upstream we set it to 127.0.0.1 and port 8080 so it's sending traffic to it's own network card on port 8080. This is the same port our docker image is running on.
  • Next in Location we pass a number of bits of information up the pipe to the website.

Note: this type of config can be used for load balancing and other things. As I explore this deeper more posts will follow.

Next we need to create a short cut in the 'site-enabled' folder.

ln -s /etc/nginx/sites-available/touroperator.io /etc/nginx/sites-enabled/touroperator.io

Remember to change the file name to your own.

Lastly reload the NGINX server

nginx -s reload

And point your DNS and you are running. The traffic will be routed to the docker container.

This post is a guide on setting the NGINX Config to work as a reverse proxy to our web application inside a docker container. When you use docker to host your web applications and services. It's handy to be able to use a reverse proxy to pass the traffic on…

Read More

Setting up Docker on Linode

A quick guide to setting up Docker on a Linode Ubuntu server. As I go deeper into exploring docker and building applications and services in new ways, I wanted a post that acts as a starting point.

Installing docker

$ sudo apt-get update

First, add the GPG key for the official Docker repository to the system:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Add the Docker repository to APT sources:

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Then Install Docker

sudo apt-get install -y docker-ce

Check the Docker Status

sudo systemctl status docker

That's it docker is now running on our Linux server. I will be adding more posts to the blog as I explore docker more.

A quick guide to setting up Docker on a Linode Ubuntu server. As I go deeper into exploring docker and building applications and services in new ways, I wanted a post that acts as a starting point. Installing docker $ sudo apt-get update First, add the GPG key for the official…

Read More

Docker Explained

Docker
What is docker

Docker containers wrap up a piece of software in a complete file system that contains everything it needs to run: code, runtime, system tools, system libraries – anything you can install on a server. This guarantees that it will always run the same, regardless of the environment it is running in.

When you develop with docker development, test and production can all have the same container. Which means if it works for development it will work in production. Makes it easy to update, test and change.

Docker runs on the linux kernal which means it can not run windows inside a container but you can get Docker to run on a windows server with either virtual box or HyperV

Docker VS virtual machines

Docker looks like a virtual machine but it isn't a virtual machine. With Docker think in terms of PAAS.

Virtual machines have a full OS with its own memory management installed with the associated overhead of virtual device drivers. In a virtual machine, valuable resources are emulated for the guest OS and hypervisor, which makes it possible to run many instances of one or more operating systems in parallel on a single machine (or host). Every guest OS runs as an individual entity from the host system.

On the other hand Docker containers are executed with the Docker engine rather than the hypervisor. Containers are therefore smaller than Virtual Machines and enable faster start up with better performance, less isolation and greater compatibility possible due to sharing of the host’s kernel.

More information

For Windows or MAC or Linux under install find your flavour.

You can also run Docker on the cloud

Docker What is docker Docker containers wrap up a piece of software in a complete file system that contains everything it needs to run: code, runtime, system tools, system libraries – anything you can install on a server. This guarantees that it will always run the same, regardless of the environment…

Read More