Creating WSL2 context on Windows

Moderator note by rimelek: previous discussion: Making Windows Image in Docker WSL2

Thanks Akos. A follow-up.

I am trying to create a wsl - specifically as mentioned in Docker in Windows 11 using WSL2. A free local Docker development… | by ferarias | Medium towards the end

docker context create wsl --docker "host=tcp://<YOUR_WSL_IP>:2375"
docker context use wsl
docker run --rm hello-world

I do the above and I get an error - see below for details. It seems I cannot connect to my wsl session. I verified using ifconfig in wsl that IP address is correct. Any advice ?

PS C:\windows\system32> &'C:\Program Files\docker\docker' context create wsl --docker "host=tcp://172.31.200.22:2375"
wsl
Successfully created context "wsl"
PS C:\windows\system32>  &'C:\Program Files\docker\docker' context use wsl
wsl
Current context is now "wsl"
PS C:\windows\system32>  &'C:\Program Files\docker\docker' info
Client:
 Version:    24.0.5
 Context:    wsl
 Debug Mode: false

Server:
ERROR: error during connect: Get "http://172.31.200.22:2375/v1.24/info": dial tcp 172.31.200.22:2375: connectex: No connection could be made because the target machine actively refused it.
errors pretty printing info
PS C:\windows\system32>

Does this mean you switch the focus from wanting to build Windows image to building Linux images?

If this is the case, please close this topic and create a new one.

It must be the case as otherwise you would not mention WSL2. WSL2 runs Linux, as such it can only build Linux images and run containers based on Linux images.

Yes- I want to build a Windows container in windows but sometimes I want to build a Linux container in linux env. I don’t have a linux server so I am using WSL2 (and stay in Windows server). I came across the link to do the context switch between the two (shown in earlier thread) I can create my wsl context but cannot connect to it using TCP. Wondering what else I need to do ? I will try to use ssh to see if I have better luck.

I moved wsl2 context related posts to this new topic and edited your post to add codeblocks and reference to the previous topic. Please, use the code block button next to the quote button above the message textarea when you share code, terminal outputs, commands or logs.

Regarding the error message: Did you change the Docker config in the WSL2 distribution to listen on a TCP socket? Also are you sure Docker is running in WSL2?

By the way I wouldn’t create a WSL context unless you could figure out how to make the IP address of WSL2 persistent. I couldn’t.

How do I change the docker config in WSL2 to listen to a TCP socket ? BTW, I was able to do what I wanted via ssh but I would prefer to do via TCP.
Yes, I am sure that I am running WSL2.
In the article link I posted, he does mention issue of persistence and shows a workaround. I think that can be scripted. So not too worried about that part.

Thx for changing to code. Not sure how you did that. Plz let me know of other docker etiquettes I would need to follow.

It is covered in the blog post by binding it to 0.0.0.0. Since ports bound inside a distribution are available from localhost on Windows, the docker socket should be reachable from localhost:2375. I am not really sure why the blog post uses the ip of the wsl distribution instead of localhost.

Good thing is that WSL2 distribution are not bound to any other Windows host ip than localhost/127.0.0.1. You will not be able to access it from any other host in your lan, unless you create a portforwarding rule on Windows that takes care of it. There are dozens of blog posts that discuss how its done.

WARNING: exposing the docker socket over http is a security risk! Everyone that can reach the http endpoint can control your docker engine. Accessing the docker engine through ssh at least adds (preferable key based) authentication.

1 Like

You are right, I didn’t notice it.