This should list the networkIDs try running the docker build
docker build --network=<networkid> tag=<tag>
try the above with all the network listed it will work with one, that’s the host network.
(In my case, i used the ID of the NAT Network
Anyhow, temporary solutions are temporary, so i kept looking for something better.
And according to this article on CodeBucket the reason for this problem is that the docker DNS is broken, and it’s possible to set the DNS server in the docker configuration.
You’ll need to open /etc/docker/daemon.json on LINUX machines and alternatively open C:\ProgramData\docker\config\daemon.json on Windows machines. When using Docker for Windows, you can alternatively just use the Docker for Windows UI by right clicking on the Docker Icon in the Taskbar, opening settings and navigating to “Docker Engine”
Then you’ll need to hard-config your DNS Servers by adding this line into the JSON:
“dns”: [“10.1.2.3”, “220.127.116.11”]
A fresh install with this line added will look something like this
Or: How do i work in a modern, efficient and really really nice way?
The goal of this article is to set up a system where you will be running an angular web application in WSL2 and access it via a Browser on the Windows Host. It’s pretty straightforward, because i prepared a little something.
Prepare following docker-compose.yml file (which you want to place somewhere on the same file-system level as your application, or above it, not below!)
You WILL have to fix the indentation to match yml standards.
You might notice that we are using an existing image from the docker hub. It’s actually a repository that i manage and maintain with a friend. If you check it out in detail, you will notice that this images matches the latest angular-web-development standards as per november of 2021 (nearing the end of the corona crisis, probably, maybe).
The image basically uses Node:14 as a base image, containing NPM and YARN, and installs Angular-CLI 13 and Angular-Core 13 on a local and global level, then exposes port 4200.
THIS IMAGE ASSUMES THAT THE DOCKER COMPOSE FILE IS ON THE SAME LEVEL AS THE APPLICATION DIRECTORY & THE APPLICATION DIRECTORY IS NAMED “MY-ANGULAR-APP”
Of course, you can easily modify the “ports” section of the compose file to change the port you want to serve to, the LEFT side is the host side.
That means that if you run this compose file with a valid application, you will be able to reach your application from a browser on the windows host via “http://localhost:4200”. If you change the LEFT element from 4200 to 4201, the address will be “http://localhost:4201” independently from the actual EXPOSE command in the dockerfile providing the container.
This command assumes you have a proxy.config.json in the application directory, remove the –proxy-config proxy.config.json part if you do not have a backend running on the windows host, else, create a proxy.config.json and configure it to lead to the windows host.
The npm command will install all dependencies whenever the docker-compose has been run.
Now, if you also want to be developing in visual studio code, install it in your WSL and run “code path-to-your-app-directory/. “ it will open VS code in this directory, vs code will automagically understand that this is an angular application and will give you some prompts depending on its complexity and components.
If you now have a valid application in the “my-angular-app” directory, you can have fun and start developing.
While working on a project employing a Client in WSL 2 (Windows Subsystem for Linux) and a Web-Api running on the Windows Host i noticed that there are several complex issues to resolve to get the communication going. The main issue was that the connection between these two instances was hard to establish and needed lots of maintenance after each reboot or network reconnect of the machine which the ecosystem was running on.
It’s hard to set up a connection between WSL2 Docker and Windows Host/Windows Host Docker, a solution is to employ a Reverse proxy on the Client in WSL2 Docker and prompt the Windows Host Server to listen to the WSL Adapter IP Address.
TALK FROM LINUX
(SUBSYSTEM FOR WINDOWS)
The first task is to set up the WSL Adapter IP Address for your reverse proxy client-side in wsl.
Run a WSL2 Terminal, from your current (home/default) directory, run sudo nano .bashrc
Now, when you run your docker container in WSL, pass the environment variable WSL_WINDOWS_HOST
for example using this parameter (or a docker compose): -e APIHOST=$WSL_WINDOWS_HOST
Inside the container, you just have to configure the proxy to use
http://$APIHOST:[YOURPORT] (or https of course)
Well, that’s done, now the WSL Container wants to talk to Windows, but nobody is listening, story of my life, let’s fix that up.
LISTEN FROM WINDOWS
The second task to get this running was to notify the Application about the WSL Adapter IP Address. If you want to see the WSL Adapter IP Address just type “ipconfig” in powershell and find the WSL Adapter IPv4 Address.
You would want to inform your Server Application on the WSL Adapter IP Address, it needs to listen to this to receive connections from WSL.
To employ this IPv4 Address (make the Server listen to it) you want to change your launchSettings.json to listen to “http://[YOUR WSL ADAPTER IP ADDRESS]:[YOUR PORT]”
BUT: The WSL Adapter IP Address changes on every Windows Host reconnect to any network and consequently on every reboot and/or occasionally on wake-ups of the Host system (from Sleep or Hibernate). Normally, the IP-Address to listen to has to be configured in your (server) application, that means that you have to set it before the server application starts. If the IP constantly changes, it can be time consuming to look up the WSL Adapter IP Address every time and change the server settings accordingly.
Well that’s where Task Scheduler and Powershell play together, you will want to create a powershell script that reads the WSL Adapter IP Address and stores it in an environment variable, this environment variable can then be used by your server application.
Luckily, i took the time to figure out the Powershell and corresponding Task Scheduler settings:
This is the powershell script, here called “wsl_adapter_ip_to_env.ps1” and located in “C:/Sources/Scripts_Autorun” on the Windows host:
Create this file and open up the Task scheduler (Windows Key + R, then “taskschd.msc”)
Create a folder in the Task Scheduler Library (for better Script management), i created “Task Scheduler Library/WSL_DEV”
Create a new Task (NOT a Basic Task) with following Settings: GENERAL TAB: > Description: “Create ENV Variable WSL_ADAPTER_IP with WSL ADAPTER IP Address” > Security Options: Run only when user is logged in (your user) > Security Options: Run with highest privilegues
TRIGGERS TAB: > NEW TRIGGER: At logon (your user), Delay: 1 minute, enabled (this will update the WSL Adapter IP variable on logon) > NEW TRIGGER: On an event (basic), Log: Microsoft-Windows-NetWorkProfile/Operational, Source: NetworkProfile, Event ID: 10000, Enabled (this will update the WSL Adapter IP on every Windows Host Network reconnect, no matter which adapter or network type)
ACTIONS TAB: > NEW ACTION, START A PROGRAM Program/Script: powershell Add Arguments: -windowstyle hidden -ExecutionPolicy Bypass -file C:\Sources\Scripts_Autorun\wsl_adapter_ip_to_env.ps1
That’s it, now you will have an updated WSL Adapter IP Address Environment variable every time it changes.
There are multiple approaches to use this environment variable in a .Net API, currently i’m doing this in “Program.cs”
public static IHostBuilder CreateHostBuilder(string args)
string wslAdapterIp = Environment.GetEnvironmentVariable("WSL_ADAPTER_IP");
Console.WriteLine("WSL_ADAPTER_IP Environment variable is set, using.");
Note that you will need to pass this environment variable into the container via the “-e” flag or a docker-compose file, this example will work directly on the host using the env var that has been set by the task scheduler.
Well then, happy coding, leave a like, comment or whatever, until next time and don’t forget to bash in the bell icon and call the foo fighters. Bye!
(As usual, i’m open for constructive criticism, fixes and anything else that you might want to contribute)
If WSL-Integration is active in Docker Desktop, none of this will work. Disable WSL-Integration under Docker-Desktop Settings -> General
This also implies that you install docker on your WSL (2) the same way as you would on a seperate machine.
Another important finding!
This setup worked for me for a few weeks, then suddenly stopped working.
I put some hours of figuring-out into it and noticed another problem:
It seems that the network settings are scrambled/broken when Docker Desktop starts before WSL2 – Docker.
That means that if Docker Desktop is started on Windows Startup as usual, WSL Docker will be unable to talk to the Windows Host.
To fix this very strange behavior (please somebody take the time to explain this to me?) i have 2 workarounds:
Disable Docker Desktop autostart and run only after WSL 2 Docker has been started.
Please note that the bashrc script on WSL will only run if WSL is starting, this means that if the WSL Adapter IP Changes during runtime, you need to restart WSL by closing all Windows and opening a new one or by rebooting the machine if you prefer.
Please note that Windows works in strange ways, for example, if you open a command line window or start a process, on start, the process will load all environment variables and not update them (ever, again), this means that if you are running Visual Studio or a Command Line and your Adapter IP Environment variable changes, you might want to restart Visual Studio or the Command window.
These behaviors cannot be influenced by me and are operating system dependant quirks, i’m already looking into some kind of force-reload mechanisms, but other than working with temporary files (which is NOT a good way) i could not yet come up with a solution for VS or WSL (though i found a force-reload for cmd/ps).
It is easy to add a jar file to the java classpath by using the command java -cp path/to/jar myjar.jar
The problem with this approach is that the “-cp” or “-classpath” command will overwrite your default classpath, meaning that you will loose all of the other references that existed in there before, you only have the jars that you reference within the classpath command parameter.
In my case, i wanted to APPEND multiple jars during container startup so they can be referenced by my products plug-ins, so i had to figure out how to open up my jar to enable this , after that, it was very easy to append jars. In the extracted jar directory structure, you will find that you have a “BOOT-INF\lib” directory.
Any of the lib directories contents are automatically added to the classpath when the program is run.
So we want to create another directory inside the lib directory called “external_lib”
This folder can now be mounted in a docker compose or run command, the folder in the host filesystem can contain any additional jar files that you want to append to the classpath.
To run a java program from an image, you still need to provide the fat jar in the container. You then want to extract the fat jar (jar -extract or unzip) into a separate folder. You should now have a “BOOT-INF” “META-INF” and maybe other folders depending on your codebase.
Skip extracting the namespace if you already know the exact namespace of your main class.
Find the textfile “MANIFEST.MS” in META-INF and open it up, check to find the “MainClass” definition at the end of the file, keep this value in mind. If your java project uses the spring boot framework (at least at the 2.x versions) you have to substitute the main class found in the MANIFEST.MS with org.springframework.boot.loader.PropertiesLauncher
This is because the spring framework uses its own means and structure to pack jar files and bootstraps them with their propertieslauncher.
Now the only step left to do is to call the entrypoint correctly. ENTRYPOINT java namespace.to.my.mainclass
An example for a correctly build dockerfile calling such a jar looks like so:
FROM adoptopenjdk15_hotspot as builder WORKDIR /extracted_jar COPY hostDir/myjar.jar ./ RUN jar -xf myjar.jar ENTRYPOINT java my.main.class