Studies

Docker
Docker with CodingSoldier
	
First way:
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out


# Build runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "CodingSoldier.dll"]

bjohns@671201519NB001 MINGW64 /c/users/bjohns/source/repos/CodingSoldier/CodingSoldier
$ docker build -t codingsoldierwebapp .
bjohns@671201519NB001 MINGW64 /c/users/bjohns/source/repos/CodingSoldier/CodingSoldier
$ docker run -d -p 8080:2350 --name myapp codingsoldierwebapp



Second Way
FROM microsoft/aspnetcore
COPY ./bin/Debug/netcoreapp2.0/publish .
ENTRYPOINT ["dotnet", "CodingSoldier.dll"]

$ docker build -t myapp .
$ docker run -d -p 8000:80 myapp

	





Docker with CodingSoldier and Nginx in the same container

Reference:https://www.sep.com/sep-blog/2017/02/24/nginx-reverse-proxy-to-asp-net-core-same-container/

DockerFile content:
#aspnet core build image
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
#aspnet core runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
#nginx install
RUN apt-get update
RUN apt-get install -y nginx
#copy startup.sh with correct permissions
COPY ./startup.sh .
RUN chmod 755 /app/startup.sh
#copy nginx.conf
RUN rm /etc/nginx/nginx.conf
COPY nginx.conf /etc/nginx
#expose 2350 - aspnet core site will listen on this port
#expose 80 - nginx will listen on this port.
#Both ports are internal to the container. 
#While running the image, port mapping need to be mentioned: $ docker run -p 8080:80 codingsoldier-with-nginx
EXPOSE 2350 80
#copying output of "build-env" from "/app/out" to "."
COPY --from=build-env /app/out .
CMD ["sh", "/app/startup.sh"]
#Docker Commands:
#$ docker build -t codingsoldier-with-nginx .
#$ docker run -p 8080:80 codingsoldier-with-nginx
	
nginx.conf content:
worker_processes 4;

events { worker_connections 1024; }

http {
	sendfile on;
	
	upstream app_servers {
		server 127.0.0.1:2350;
	}

	server {
		listen 80;
		location / {
			proxy_pass		http://app_servers;
			proxy_redirect		off;
			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_set_header	X-Forwarded-Host $server_name;
		}
	}
}

startup.sh content:
#!/bin/bash
service nginx start
dotnet /app/CodingSoldier.dll

Beware of CRLF issues in startup.sh and nginx.conf files. Better to create them using vim in Linux itself instead of copy pasting from Windows environment.


After creating the image, we can publish it to docker repository:
$ export DOCKER_ID_USER="bnyjohns"
$ docker login
$ docker tag codingsoldier-with-nginx $DOCKER_ID_USER/codingsoldier-with-nginx
$ docker push $DOCKER_ID_USER/codingsoldier-with-nginx

The image gets pushed to docker repository. Then we can pull and run in any machine.
$ docker pull bnyjohns/codingsoldier-with-nginx:latest
$ docker run -p 8080:80 codingsoldier-with-nginx




Docker with CodingSoldier and Nginx in the different containers using Docker Compose
Reference: https://www.sep.com/sep-blog/2017/02/27/nginx-reverse-proxy-to-asp-net-core-separate-docker-containers/

docker-compose.yml content:
version: '3'

services:
  website:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Production
    expose:
      - "2350"
  reverseproxy:
    build:
      context: .
      dockerfile: NginxDockerfile
    links:
      - website
    ports:
      - "8080:80"

DockerFile content
#aspnet core build image
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
#Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

#Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

#aspnet core runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app

EXPOSE 2350

#copying output of "build-env" from "/app/out" to "."
COPY --from=build-env /app/out .
CMD ["dotnet", "/app/CodingSoldier.dll"]

Nginx.conf content:
worker_processes 4;

events { worker_connections 1024; }

http {
	sendfile on;
	
	upstream app_servers {
		server website:2350;
	}

	server {
		listen 80;
		location / {
			proxy_pass		http://app_servers;
			proxy_redirect		off;
			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_set_header	X-Forwarded-Host $server_name;
		}
	}
}


$ docker-compose build
$ docker-compose up


bjohns@671201519NB001 MINGW64 /c/GitRepositories/codingsoldier_aspnetcore_versio
n2/CodingSoldier (master)
$ docker-compose up
Starting codingsoldier_website_1 ...
Starting codingsoldier_website_1 ... done
Starting codingsoldier_reverseproxy_1 ...
Starting codingsoldier_reverseproxy_1 ... done
Attaching to codingsoldier_website_1, codingsoldier_reverseproxy_1


Now 2 containers are running. We can connect to those containers if we want to.
It is easy: $ docker exec -it codingsoldier_reverseproxy_1 bash
Now we are inside the container.
Linux
cd /c
cd /temp/anotherfolder => Now in C:/temp/anotherfolder

cd - => Now in C:/temp
ls => lists all directories under the current directory
mkdir => make directory


If you want to go back just one level, enter at the Linux command prompt:
cd ..

pwd => current path in terminal
clear => clear screen(similar to cls in windows)

groups => shows the groups the current user is a member of
whoami => shows the current user name
rm -r directory => removes the directory and the contents inside
touch filename.txt => to create a new file of name filename.txt
df -h => to see disk space usage


Installing DotNet Core Runtime in Ubuntu 16.04(https://www.microsoft.com/net/download/linux-package-manager/ubuntu16-04/runtime-2.0.0):
Download the package: sudo wget https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
Moving the package to /var/tmp: $ sudo mv packages-microsoft-prod.deb /var/tmp
cd /var/tmp
sudo dpkg -i packages-microsoft-prod.deb

sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-hosting-2.0.0




sudo usermod -G -a www-data   => adds username to a member of www-data group for pscp to work
sudo chown -R www-data:www-data /var/www/codingsoldier
sudo chmod -R 0775 /var/www/codingsoldier



Now copy the file from your machine to the linux VM using PSCP:
pscp -r -v -i C:\Dropbox\Work\SolarWinds\Boney\Study\EC2\BoneysEC2.ppk C:\Users\bjohns\source\repos\CodingSoldier\CodingSoldier\bin\Release\PublishOutput\* ubuntu@ec2-34-219-81-14.us-west-2.compute.amazonaws.com:/var/www/codingsoldier
We can start the kestrel server and verify if the site is up:
ubuntu@ip-172-31-31-235:/var/www/codingsoldier$ dotnet CodingSoldier.dll
If we browse to: http://34.219.81.14:2350, the site should be available.



Now install Nginx as reverse proxy:
sudo apt-get install nginx
sudo service nginx start

Nginx should be up now. Verify by going to: http://34.219.81.14/index.nginx-debian.html


To configure Nginx as a reverse proxy to forward requests to your ASP.NET Core app, modify /etc/nginx/sites-available/default. 
ubuntu@ip-172-31-31-235:/etc/nginx/sites-enabled$ sudo vim default
Edit the default file and save.

sudo vim /etc/nginx/sites-enabled/default
##########################################################
server {
        listen                      80;
        server_name codingsoldier.com *.codingsoldier.com;
	access_log off;
	location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                proxy_pass              http://localhost:2350;

            # The default minimum configuration required for ASP.NET Core
            # See https://docs.asp.net/en/latest/publishing/linuxproduction.html?highlight=nginx#configure-a-reverse-proxy-server
            proxy_cache_bypass      $http_upgrade;
            # Turn off changing the URL's in headers like the 'Location' HTTP header.
            proxy_redirect          off;
            # Forwards the Host HTTP header.
            proxy_set_header        Host $host;
            # The Kestrel web server we are forwarding requests to only speaks HTTP 1.1.
            proxy_http_version      1.1;
            proxy_set_header        Upgrade $http_upgrade;
            # Adds the 'Connection: keep-alive' HTTP header.
            proxy_set_header        Connection keep-alive;

            # Sets the maximum allowed size of the client request body.
            client_max_body_size    10m;
            # Sets buffer size for reading client request body.
            client_body_buffer_size 128k;
            # Defines a timeout for establishing a connection with a proxied server.
            proxy_connect_timeout   90;
            # Sets a timeout for transmitting a request to the proxied server.
            proxy_send_timeout      90;
            # Defines a timeout for reading a response from the proxied server.
            proxy_read_timeout      90;
            # Sets the number and size of the buffers used for reading a response from the proxied server.
            proxy_buffers           32 4k;
        }
}
##########################################################


Reload the config:
sudo nginx -t 
sudo nginx -s reload




Create the service file
sudo vim /etc/systemd/system/kestrel-codingsoldier.service
##########################################################
[Unit]
Description=CodingSoldier App running on Ubuntu

[Service]
WorkingDirectory=/var/www/codingsoldier
ExecStart=/usr/bin/dotnet /var/www/codingsoldier/CodingSoldier.dll
Restart=always
RestartSec=10  # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=codingsoldier
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target
##########################################################



sudo systemctl enable kestrel-codingsoldier.service


sudo systemctl start kestrel-codingsoldier.service
sudo systemctl status kestrel-codingsoldier.service

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-2.1&tabs=aspnetcore2x
Page 1 of 5