Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem launching Windows processes from inside WSL2 Docker #10883

Closed
1 of 2 tasks
nrclark opened this issue Dec 6, 2023 · 8 comments
Closed
1 of 2 tasks

Problem launching Windows processes from inside WSL2 Docker #10883

nrclark opened this issue Dec 6, 2023 · 8 comments
Assignees
Labels

Comments

@nrclark
Copy link

nrclark commented Dec 6, 2023

Windows Version

Microsoft Windows [Version 10.0.19045.3448]

WSL Version

2.0.9.0

Are you using WSL 1 or WSL 2?

  • WSL 2
  • WSL 1

Kernel Version

5.15.133.1-1

Distro Version

Ubuntu 22.04

Other Software

Docker CE, version 24.0.7. Installed on WSL2 from Docker's apt repo.
Notepad.exe

Repro Steps

I am working on a project that needs to run Windows applications from inside of a WSL2 Docker container. When I try to launch a Windows application directly (notepad.exe used as an example), I get a strange failure. The failure doesn't occur if I launch it from inside of a shell instead.

Inside of WSL2 (with dockerd running as sudo in another tab), this command fails:

docker run --rm -it --privileged --mount type=bind,source=/mnt,target=/mnt \
    --mount type=bind,source=/run/WSL,target=/run/WSL \
    --mount type=bind,source=/etc/passwd,target=/etc/passwd,readonly \
    --mount type=bind,source=/etc/group,target=/etc/group,readonly \
    --mount "type=bind,source=$HOME,target=$HOME" \
    --workdir "$HOME" \
    -u $(id -u):$(id -g) \
    --env WSLENV="${WSLENV}" \
    --env WSL_DISTRO_NAME="${WSL_DISTRO_NAME}" \
    --env WSL_INTEROP="${WSL_INTEROP}" \
    --env WT_PROFILE_ID="${WT_PROFILE_ID}" \
    --env WT_SESSION="${WT_SESSION}" \
    ubuntu \
    '/mnt/c/Windows/system32/notepad.exe'

with the following error:

<3>WSL (1) ERROR: ConfigInitializeEntry:1621: Failed to mount (null) at /dev as devtmpfs 1

but this one succeeds OK:

docker run --rm -it --privileged --mount type=bind,source=/mnt,target=/mnt \
    --mount type=bind,source=/run/WSL,target=/run/WSL \
    --mount type=bind,source=/etc/passwd,target=/etc/passwd,readonly \
    --mount type=bind,source=/etc/group,target=/etc/group,readonly \
    --mount "type=bind,source=$HOME,target=$HOME" \
    --workdir "$HOME" \
    -u $(id -u):$(id -g) \
    --env WSLENV="${WSLENV}" \
    --env WSL_DISTRO_NAME="${WSL_DISTRO_NAME}" \
    --env WSL_INTEROP="${WSL_INTEROP}" \
    --env WT_PROFILE_ID="${WT_PROFILE_ID}" \
    --env WT_SESSION="${WT_SESSION}" \
    ubuntu \
    sh -c '/mnt/c/Windows/system32/notepad.exe'

Interestingly, I also see that bash -c '/mnt/c/Windows/system32/notepad.exe' fails in the same way, even though sh -c '/mnt/c/Windows/system32/notepad.exe' works just fine. I can also make a script with the contents of:

#!/bin/bash
/mnt/c/Windows/system32/notepad.exe "$@"

and run that instead of a shell, and it works as expected.
What's going on here? Can anybody help me out with a workaround?

Expected Behavior

In all cases, notepad should launch identically.

Actual Behavior

Launching notepad.exe from sh/dash works, as does putting it in a shell-script that runs under bash. Launching it directly fails, as does launching it from bash (even though that works when placed in a script).

All failures produce this message on stderr:

<3>WSL (1) ERROR: ConfigInitializeEntry:1621: Failed to mount (null) at /dev as devtmpfs 1

Diagnostic Logs

I couldn't think of any useful dianostic logs. Let me know if there's anything specific that you'd like to see.

Copy link

github-actions bot commented Dec 6, 2023

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@OneBlue
Copy link
Collaborator

OneBlue commented Dec 7, 2023

/logs

@nrclark
Copy link
Author

nrclark commented Dec 8, 2023

Logfiles are attached here. During recording, I captured one successful launch (asking Docker to run sh -c '/mnt/c/Windows/system32/notepad.exe') and one failed launch (asking Docker to run /mnt/c/Windows/system32 directly) WslLogs-2023-12-08_00-30-10.zip

@OneBlue
Copy link
Collaborator

OneBlue commented Dec 8, 2023

Thank you @nrclark. The issue is that the executable that runs windows executables has special logic when its pid is 1, and inside a docker container it will behave that if it's the first process.

This is a bug, but until it's fixed you can work around it like this:

docker run --rm -it --privileged --mount type=bind,source=/mnt,target=/mnt \
    --mount type=bind,source=/run/WSL,target=/run/WSL \
    --mount type=bind,source=/etc/passwd,target=/etc/passwd,readonly \
    --mount type=bind,source=/etc/group,target=/etc/group,readonly \
    --mount "type=bind,source=$HOME,target=$HOME" \
    --workdir "$HOME" \
    -u $(id -u):$(id -g) \
    --env WSLENV="${WSLENV}" \
    --env WSL_DISTRO_NAME="${WSL_DISTRO_NAME}" \
    --env WSL_INTEROP="${WSL_INTEROP}" \
    --env WT_PROFILE_ID="${WT_PROFILE_ID}" \
    --env WT_SESSION="${WT_SESSION}" \
    ubuntu \
    bash -c '/mnt/c/Windows/system32/notepad.exe && echo done'

@OneBlue OneBlue self-assigned this Dec 8, 2023
@OneBlue OneBlue added the bug label Dec 8, 2023
@nrclark
Copy link
Author

nrclark commented Dec 9, 2023

Thanks @OneBlue. I also noticed something else that might be related. When I have systemd enabled in wsl.conf, I can't run Windows processes from inside of Docker at all. Running this:

docker run --rm -it --privileged --mount type=bind,source=/mnt,target=/mnt \
    --mount type=bind,source=/run/WSL,target=/run/WSL \
    --mount type=bind,source=/etc/passwd,target=/etc/passwd,readonly \
    --mount type=bind,source=/etc/group,target=/etc/group,readonly \
    --mount "type=bind,source=$HOME,target=$HOME" \
    --workdir "$HOME" \
    -u $(id -u):$(id -g) \
    --env WSLENV="${WSLENV}" \
    --env WSL_DISTRO_NAME="${WSL_DISTRO_NAME}" \
    --env WSL_INTEROP="${WSL_INTEROP}" \
    --env WT_PROFILE_ID="${WT_PROFILE_ID}" \
    --env WT_SESSION="${WT_SESSION}" \
    ubuntu \
    sh -c '/mnt/c/Windows/system32/notepad.exe'

gives me a different error than before. Instead of that /dev error, I get this interesting one instead:

sh: 1: /mnt/c/Windows/system32/notepad.exe: not found.

Note that if I run sh -c 'ls /mnt/c/Windows/system32/notepad.exe' instead, the executable is -detected- OK, so it's definitely still getting mounted into my Docker container without issue.

This error occurs when:

  1. Systemd is enabled and running.

  2. Docker is installed and active. It doesn't matter which Docker version I use (Ubuntu's packages or Docker.io's packages), or whether I run the service using systemctl start docker or manually using service docker start. When using service docker start, it also doesn't matter whether I do it from inside of a shell or whether I do it via wsl.exe from Windows via cmd.exe.

  3. I try to run any Windows process from inside of a Docker image, even through methods that work when systemd is disabled. This fails every time, with an stderr that it can't find the file. Note that I can see the file on the filesystem OK.

I took a log capture of this happening and attached it to this ticket.
WslLogs-2023-12-08_22-14-56.zip

@ndreys
Copy link

ndreys commented Dec 10, 2023

@nrclark does adding

-v /init:/init:ro

or equivalent --mount incantation to your docker run help? I think I might have just had the pleasure of running into exactly the same issue and from what I can tell the problem is that WSLInterop registers /init as interpreter:

cat /proc/sys/fs/binfmt_misc/WSLInterop
enabled
interpreter /init
flags: PF
offset 0
magic 4d5a

but /init isn't present in the container AKA ENOENT/"file not found". Adding explicit bind-mount resolved the problem in my case.

I could swear this used to work without, but now I'm starting to doubt my memory. 🤷

@nrclark
Copy link
Author

nrclark commented Dec 11, 2023

@ndreys thanks for the pro-tip! You're right, that got .exe execution working under Docker when systemd is active. It's interesting that .exe execution works in non-systemd Docker without the explicit /init binding - maybe some more PID1 trickery.

The main issue in my ticket is still active though, and is unrelated to the secondary issue that @ndreys addressed.

@benhillis
Copy link
Member

Fixed with https://github.com/microsoft/WSL/releases/tag/2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants