1

Trying to wrap my head around Docker, WSL2, Distros, Images and Containers. What is the difference between a WSL distro and a Docker image? Looking at the following two snapshots, it looks like those are different things:

List of installed distros in WSL:

List of installed distros in WSL

List of images in Docker Desktop:

Images in Docker Desktop

Alpine and Ubuntu are listed in the list of additional distros but do not show up in the list of images.

List of additional distros

How am I supposed to run one of the installed WSL distros (Alpine or Ubuntu) as a Container and get to its terminal? Lastly, can I launch Ubuntu's desktop UI from within that container?

dotNET
  • 33,414
  • 24
  • 162
  • 251
  • To a first approximation, you can't run a GUI application from a container; [Can you run GUI applications in a Linux Docker container?](https://stackoverflow.com/questions/16296753/can-you-run-gui-applications-in-a-linux-docker-container) has extremely Linux-specific instructions. A container only runs one process and not a full OS/display system/desktop environment. – David Maze Feb 19 '22 at 15:22
  • *What is the difference between a WSL distro and a Docker image?* This seems like the wrong question. You are trying to compare apples and oranges. – super Feb 19 '22 at 15:28
  • I've provided an answer below that will hopefully help you understand the difference between the two, and how to use each. If your goal is to run a desktop UI, though, stick with just the WSL2 distribution. As mentioned above, running GUI applications (let alone desktops) inside a container is an advanced topic. Honestly, even running a desktop environment in WSL2 is an advanced topic. I have a few answers over on [Ask Ubuntu](https://askubuntu.com) that are related to this, but I don't recommend it if you are just starting out with WSL. – NotTheDr01ds Feb 19 '22 at 15:49
  • @super: Not necessarily apples and oranges. I had the feeling that these two were very similar at some level. NotTheDr01ds's answer below confirms that was the case. – dotNET Feb 19 '22 at 17:07
  • @dotNET Similar has meaning in a context. Is a football similar to a whale? They are both physical things with a mass and some form of content. Depends on what the discussion is about I guess. When it comes to running a WSL distro as a container in docker and opening a Ubuntu desktop UI we have already made such a wild mix of concepts that it's hard to even know where to begin. The length of the answer kind of shows my point. – super Feb 19 '22 at 17:32

1 Answers1

2

Docker images and WSL distributions are two completely different things from the usage standpoint.

In the context of over-simplifying to compare and explain the two:

  • WSL Distributions contain the tools that you use to interact with and develop applications using Linux. This includes your shell (bash by default in Ubuntu) and the docker client (provided by Docker Desktop).

  • Docker images are what you use as a starting point for your Docker containers.

The third screenshot you provided is Settings dialog that allows you to choose which WSL images should be integrated with Docker. Try the following:

  • Turn off Ubuntu in that setting
  • Apply and Restart Docker Desktop
  • Start your Ubuntu WSL distribution by running it from the Start Menu (or preferably Windows Terminal, if you have it installed).
  • Try running the docker command

You should find it isn't there.

  • Turn Ubuntu back on in Docker Desktop's settings
  • Apply and Restart Docker Desktop
  • You don't need to restart Ubuntu, but the docker command should now be there.

Docker desktop actually injects a link for the docker command:

  • From the docker-desktop distribution
  • Into the "user" distributions you select in that Settings option.

In general, it's fine just to leave it on for all distributions.

Now I know your next question:

So how do I run a Docker container based on Alpine or Ubuntu docker images?

You need to actually pull the Docker images onto your computer first:

  • Make sure you've enabled all WSL2 distributions again (not necessarily required, but you don't want to leave any off by mistake).
  • Start your Ubuntu distribution
  • Run:
    docker run --rm -it alpine
    

Docker will detect that you don't have the Docker Alpine image installed, pull it, and run it. This is a bit of Docker shorthand for two steps, actually:

docker pull alpine
docker run --rm -it alpine

The -it options are for "interactive" and "terminal".

At this point, you'll be at the BusyBox shell prompt (Alpine's default) that is running inside your Ubuntu WSL distribution.

Before exiting, go back to Docker Desktop and examine the list of containers and images. You'll see:

  • An "alpine" image
  • A randomly-named container that is running based on the alpine image

If you start another terminal with Ubuntu open, you can run:

  • docker ps to show you the container information
  • docker images to show you the image information

This is essentially the same info that you see in Docker Desktop.

Go back to the first Ubuntu WSL2 terminal, which is running the Alpine container with the BusyBox prompt. Try running docker there -- It won't work, because this is a container based on the Docker Alpine image, not your WSL Alpine distribution.

Type exit or Ctrl+D to exit that prompt, and you'll now be back at the bash prompt for Ubuntu.

At this point, you'll notice that your Docker container is now gone, since we specified the --rm option that removes it when the process ends. If we hadn't done that, it would still show as a "Stopped" container in Docker.

You'll find that the Alpine Docker image, however, is still there. Once pulled onto your machine, Docker images stay until you remove them.

Docker images and containers can take a little bit to understand, and I can understand the added confusion in WSL distributions. Play around with them a bit, read some Docker tutorials, and the information above will start to make sense shortly.


Side note: Come back and read this after the above makes sense.

Docker containers and WSL2 distributions shared one big similarity in their architecture, at least -- they are both container technologies, just different.

WSL2 distributions are actually containers running in their own namespace inside the (hidden) WSL2 Hyper-V virtual machine. They share the same kernel and network, but each WSL2 distribution/instance has its own, separate, isolated PID and user namespaces.

This is, at the core, the same concept that Docker containers use. So with WSL2 and Docker, we are really running:

  • A WSL2 distribution "container" inside the WSL2 VM
  • A Docker container inside the WSL2 distribution container
NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
  • +1 for the effort alone. Honestly this is quite a bit of stuff to digest. I've read it and probably understand the gist, but need time to do these experiments one by one. I'll accept it once I'm done. – dotNET Feb 19 '22 at 17:04
  • Right. I started experimenting. So I launched Alpine from the Terminal using `wsl -d Alpine` and then disabled and enabled Alpine in Docker settings (3rd screenshot above). This doesn't make any difference. Alpine doesn't understand `docker` command (`no such file or directory`). Surely I'm doing something wrong. – dotNET Feb 19 '22 at 18:05
  • @dotNET Well, for starters, brain fart on my part. I might have expected that Alpine was going to have trouble with the `docker` command from Docker Desktop. It's just too "simplified" a distribution. There are [steps to make it work](https://alexanderallen.medium.com/successfully-connect-alpine-wsl-2-to-docker-desktop-2-2-620e135340df), apparently. But really, Alpine isn't really meant for general use like this anyway, so I wouldn't bother. Let me update my directions above -- I'm just going to switch from using Alpine for that particular step to using Ubuntu. – NotTheDr01ds Feb 19 '22 at 18:40
  • Yeah, it works in Ubuntu. But, surprisingly, not in `docker-desktop` distro either, which I thought was the "home" distro for Docker. – dotNET Feb 19 '22 at 18:43
  • @dotNET If you want to install a third distribution (like Debian or openSUSE Tumbleweed), those will work -- And I tested and confirmed this time, rather than assuming as I did in my answer. Just Alpine is going to have this issue, for the most part. The key problem, I believe, is that it uses [musl instead of glibc](https://stackoverflow.com/q/33382707/11810933). And the docker in Docker Desktop is compiled against glibc. – NotTheDr01ds Feb 19 '22 at 18:44
  • `docker-desktop` itself too is Alpine-based? Is there a way from within a container to know what linux distribution it is? – dotNET Feb 19 '22 at 18:47
  • @dotNET Actually, `docker-desktop` isn't really supposed to be used by the end-user. It's the distro that's actually running the docker daemon. The `docker` client command is there, but it's not in the path. Give me a few minutes, and I think I can find it. – NotTheDr01ds Feb 19 '22 at 18:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242182/discussion-between-notthedr01ds-and-dotnet). – NotTheDr01ds Feb 19 '22 at 18:51
  • @NotTheDr01ds - given that it's possible to open Windows applications from WSL terminal (https://superuser.com/a/1633647), is it possible to render these applications inside GUI-enabled WSL? Obviously this would be a significant departure from intended "container-style" usage, but that paradigm is already somewhat broken by allowing the WSL container to call EXEs on the Windows host. – jrbe228 Oct 27 '22 at 14:02
  • @JeremyBeale Hmm - I have some thoughts on that, but want to make sure I understand the question. Are you asking whether or not you can run a Windows graphical app (e.g. Notepad) inside a *Linux* desktop running under WSLg? That doesn't seem to be very related to the question above, so I'm not quite sure if that's what you are asking or not. You might want to post a question with more details on [Super User](https://superuser.com), [Unix & Linux](https://unix.stackexchange.com), or (if it's in the context of Ubuntu on WSL) [Ask Ubuntu](https://askubuntu.com) – NotTheDr01ds Oct 27 '22 at 15:21
  • @NotTheDr01ds - you summarized my question exactly, "running a Windows graphical app (e.g. Notepad) inside a Linux desktop running under WSLg". I've messed around with Wine but find it to be very limited. Hoping to take advantage of Windows / WSL interoperability. I'll post a new question soon and link it here. – jrbe228 Oct 27 '22 at 16:10
  • @NotTheDr01ds - https://stackoverflow.com/q/74226087/7991646 – jrbe228 Oct 27 '22 at 17:32
  • 1
    @JeremyBeale Oops! Lol - I specifically mentioned the *other* Stack Exchange sites since your question is off-topic for Stack Overflow! – NotTheDr01ds Oct 27 '22 at 17:52
  • @JeremyBeale And just for clarity, this particular question is *borderline* on-topic (IMHO) since it is about Docker, which I consider a programming tool. However, in retrospect, this one probably should have been on Super User as well. – NotTheDr01ds Oct 27 '22 at 17:55
  • @NotTheDr01ds - new link - https://superuser.com/q/1749875/1669377 You were right, my question got reflexively closed by the SO mob. I hoped to stay on SO because ultimately Docker (and other script-heavy tools) can be part of the solution. I should have added 15 lines of semi-related code to discourage close votes ¯`\_(ツ)_/¯ – jrbe228 Oct 27 '22 at 18:25