Using the Windows Subsystem for Linux (WSL)

Sharing files between Windows Subsystem for Linux and Windows

Start WSL

  • In the Windows start menu select the icon of the Linux operating system you have installed when you set up WSL. The default is Ubuntu.
  • A Linux terminal opens

Access the Windows files from Linux

  • Start WSL
  • In the Linux terminal navigate to the /mnt directory
cd /mnt
  • The drives are mounted to sub-directories
root@P70:/mnt# ls -l
total 0
drwxrwxrwx  c
drwxrwxrwx  d
  • To access the C: Windows drive, navigate to
cd /mnt/c

Access the Linux files from Windows

To start from Windows

  • Open Windows Explorer in Windows 11
  • Navigate to \wsl.localhost\Ubuntu

To start from Linux

  • In the Linux terminal navigate to the directory you want to open in Explorer
  • In the terminal execute the following command. DOnt forget to add the dot to the end to specify the current directory.
explorer.exe .

Managing packages in Linux

To update the Linux packages in Ubuntu and Debian, execute the command

sudo apt update && sudo apt upgrade

Copy paste

If you have ever worked on a Macintosh, (macOS) you had the luxury to have a separate key combination for Copy (Command+C) and cancel (CTRL+C). As Windows does not have a Command key, the Windows terminal helps us with a smart feature:

  • If text is highlighted in the terminal, CTRL+C executes Copy,
  • If no text is highlighted in the terminal, CTLR+C cancels the operation.

Project file location

Place the project files in to the file system you will use during development. Files can be accessed across operating systems, but the access speed can be significantly slower.

Linux projects

For Linux projects place the files under the Linux user directory at

\\wsl$\<DistroName>\home\<UserName>\Project

Windows projects

For windows projects place the project files under the Windows user directory at

C:\Users\<UserName>\Project or /mnt/c/Users/<UserName>/Project$

Useful commands

Get the list of running WSL instances

  • In Windows open a PowerShell terminal and execute
    wsl.exe -l -v
  • You may should see two instances.
    • The star shows the default Ubuntu WSL instance, you open with a WSL terminal without arguments,
    • The second instance was created by Docker Desktop to run Linux containers on the Windows machine.
PS C:\WINDOWS\system32> wsl.exe -l -v
  NAME              STATE           VERSION
* Ubuntu            Running         2
  docker-desktop    Running         2

Build a Docker container image for your application

Create the image

A Docker container image is one of the best formats to distribute your application. The container provides an immutable environment, so the application runs the same way on your workstation and on any server.

We will use the simple Go application we have created in the Your first Go application post. In the main application directory open the text editor and create the Dockerfile

code Dockerfile

We will create a multi-stage build script. The advantage is that the final image is very small, as it only contains the compiled Go application, not the source code, not even the Go compiler. Enter the code

FROM golang:alpine as builder

WORKDIR /src

COPY . /src

RUN go build -o myapp cmd/myapp/*

FROM alpine:3.11.3

 WORKDIR /app

 COPY --from=builder /src/myapp /app

 ENTRYPOINT ["/app/myapp"]

Build the image

docker build -t myapp .

Check the image. myapp should be the image on the top

docker images

The output should be something like this

REPOSITORY        TAG        IMAGE ID            CREATED           SIZE
myapp             latest     267e26010d88        2 minutes ago     7.66MB

Run the image to test it.

docker run --rm -it myapp

If you want to run the image with Docker Compose create a compose file

code docker-compose.yml

Enter the code

version: '3'

services:
  myapp:
    image: myapp

Run the image in Docker Compose

docker-compose up

The output should be

Creating network “myapp_default” with the default driver
Creating myapp_myapp_1 … done Attaching to myapp_myapp_1
myapp_1 | Application config
myapp_1 | MyPackage running
myapp_myapp_1 exited with code 0

Runt the image in Docker Swarm

Initialize the Docker Swarm

docker swarm init

Run the container in the swarm

docker stack deploy -c docker-compose.yml myapp

The output should be

Creating network myapp_default
Creating service myapp_myapp

Check if the stack is running

docker stack ls

The output should be

NAME                SERVICES            ORCHESTRATOR
myapp               1                   Swarm

Check if the service is running

docker service ls

Remove the stack

docker stack rm myapp

Could not create an instance of type ‘Microsoft.AspNetCore.Http.HttpContext’

The new version of dotnet core cannot handle the HttpContext in the MVC model.

An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Could not create an instance of type ‘Microsoft.AspNetCore.Http.HttpContext’. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, set the ‘HttpContext’ property to a non-null value in the ‘…’ constructor.

Solution

Remove the line from the model:

public HttpContext HttpContext { get; set; }

Remove the line from the controller:

model.HttpContext = HttpContext;

Cities: Skylines undo

The Cities: Skylines game does not have a usual undo function, but by pressing the F1 key we can save the current state into a “Quick Save” file before a change.

To load the state from the Quick Save file, select it in the list of the Load Game menu item

The files are saved on your hard drive at

C:\Users\YOUR_USER_NAME\AppData\Local\Colossal Order\Cities_Skylines\Saves

Error: Failed to instantiate provider “aws” to obtain schema: Incompatible API version with plugin. Plugin version: 4, Client versions: [5]

When a provider has been updated outside of Terraform, (for example in Terraformer) Terraform can get out of sync with that provider version. terraform init works, but terraform plan throws the error message:

Error: Failed to instantiate provider “aws” to obtain schema: Incompatible API version with plugin. Plugin version: 4, Client versions: [5]

To update Terraform to be able to use the new provider execute

terraform init -upgrade

Cities: Skylines game frequently crashes

The Cities: Skylines game keeps objects in memory even if the object is not used. Even a 16GB system can crash frequently if the memory usage is not managed.

“Oops! The game crashed

The memory usage is high, even when the map is not too complex.

Enable enough Virtual Memory

Make sure Windows has enough Virtual Memory configured. I have set up 4096 MB minimum and maximum Virtual Memory for my 16 GB Toshiba laptop.

Install the Loading Screen mod

This page explains the issue and how to solve the problem: https://steamcommunity.com/app/255710/discussions/0/1488866180604646672/

This is the summary of the steps you can take to stabilize the game on your computer

Get jstack and other Java utilities without installing the Java JDK

  • Open a Windows command prompt
  • Create a directory for the volume and start the openjdk container
md c:\windows\temp\openjdk
docker run -it --rm -v c:\windows\temp\openjdk:c:\temp openjdk:8
  • In the Java container copy the contents of the Java bin directory to the mapped volume
cd openjdk-8\bin
copy *.* c:\temp

Docker in Windows 10 via VMware Fusion on a MacBook Pro

To run Windows Docker containers on your Macintosh computer, use the following setup. This will introduce a performance hit, because you run virtualization in virtualization.

Besides the usual setup process, you need to enable hypervisor applications to avoid the error

Error response from daemon: container … encountered an error during CreateContainer: failure in a Windows system call:  No hypervisor is present on this system

  • Install VMware Fusion on your MacBook Pro
  • Create a Windows 10 virtual machine
  • Open the Settings of the Windows 10 virtual machine
  • Select Processors & Memory
  • In the Advanced region check Enable hypervisor applications in this virtual machine

Turn off the GDPR cookie notification in a dotnet core MVC web application

The dotnet core MVC web application template adds the GDPR cookie acceptance message to the header of the page template. When the site is hosted with HTTP the accept button does not clear the message resulting an infinite loop, when the user cannot log in and the site continuously asks for the acceptance.

When the site is hosted in a Docker container it is also simpler to instal the SSL certificate on the load balancer and host the site with HTTP on port 80.

We use cookies. To be able to use this site please click the Accept button.

To remove the feature delete the

<partial name="_CookieConsentPartial" />

line from the Views\Shared\Layout.cshtml file.

Without the acknowledgement the dotnet core framework will not save any non essential cookies in the browser, so the session state will stop working.

To tell the dotnet core framework, that the application does not require the user to allow the saving of the cookies set the value of CheckConsentNeeded to false in the ConfigureServices() method, services.Configure<CookiePolicyOptions> { section

options.CheckConsentNeeded = context => false;

To make sure the session cookie is always saved even if the GDPR confirmation is enabled, declare it to essential in the ConfigureServices() method, services.AddSession(options => { section

options.Cookie.IsEssential = true;