devcontainers / features

A collection of Dev Container Features managed by Dev Container spec maintainers. See https://github.com/devcontainers/feature-starter to publish your own
https://containers.dev/features
MIT License
870 stars 347 forks source link

[desktop-lite] Entrypoint script does not use VNC_RESOLUTION defined by containerEnv #945

Open Huoleit opened 4 months ago

Huoleit commented 4 months ago

Summary

As written in the desktop-init.sh , the entrypoint script will use user-defined VNC_RESOLUTION if provided. But even if I include the env definition in both containerEnv and remoteEnv, the entrypoint script always uses the default setting.

Screenshot

Screenshot from 2024-04-15 15-10-01

Steps to reproduce

.devcontainer used.

{
    "name": "Ubuntu",
    "image": "mcr.microsoft.com/devcontainers/base:jammy",
    "features": {
        "ghcr.io/devcontainers/features/desktop-lite:1": {}
    },
    "containerEnv": {
        "VNC_RESOLUTION": "1920x1080x32"
    },
    "remoteEnv": {
        "VNC_RESOLUTION": "1920x1080x32"
    },
    "forwardPorts": [
        5901,
        6080
    ],
    "containerUser": "vscode"
}

Log

[2024-04-15T13:06:53.341Z] Start: Run: docker run --sig-proxy=false -a STDOUT -a STDERR --mount type=bind,source=/home/docker_test,target=/workspaces/docker_test --mount type=volume,src=vscode,dst=/vscode -e VNC_RESOLUTION=1920x1080x32 -u vscode --init --entrypoint /bin/sh vsc-docker_test-07c641cc479b026b86df51ee3bad1935608dd8988b8d00265001f60892599974-features-uid -c echo Container started
Huoleit commented 4 months ago

After investigating, it appears that docker run doesn't pass environment variables to a non-interactive shell. One workaround is setting those variables in the Dockerfile using ENV. However, since VNC_RESOLUTION is user-specific and I prefer not to expose it in the Dockerfile, is there a better approach to configure it in devcontainer.json?

samruddhikhandale commented 4 months ago

Ideally, using containerEnv to update VNC_RESOLUTION in devcontainer.json should work out of the box. It's strange that it's not working for you.

Please note that changes to the devcontainer.json file will not take effect in an existing container. You'll need to rebuild the container for the changes to take effect. You can do this by running the "Remote-Containers: Rebuild Container" command in Visual Studio Code.

docker run doesn't pass environment variables to a non-interactive shell.

Docker does pass environment variables to non-interactive shells. When you use the -e or --env option with docker run, the environment variables are set for all processes in the container, including non-interactive shells.

docker run --sig-proxy=false -a STDOUT -a STDERR --mount type=bind,source=/home/docker_test,target=/workspaces/docker_test --mount type=volume,src=vscode,dst=/vscode -e VNC_RESOLUTION=1920x1080x32

From the logs, it looks right to me ^

@gauravsaini04 Can you help investigate this issue? To start with, can you see if you can repro the issue as described by @Huoleit ? Thanks!

Huoleit commented 4 months ago

I finally found the issue. Variables defined inside heredocs (see here) are not escaped. Instead, they are evaluated or assigned default values during build time. That is why variables passed by docker run -e VNC_RESOLUTION do not work, but defining them in the Dockerfile does.

Inspecting the final entrypoint script also proves my thoughts. They are evaluated during build time.

#!/bin/bash

user_name="root"
group_name="root"
LOG=/tmp/container-init.log

export DBUS_SESSION_BUS_ADDRESS="autolaunch:"
export DISPLAY=":1"
export VNC_RESOLUTION="1440x768x16" 
export LANG="en_US.UTF-8"
export LANGUAGE="en_US.UTF-8"
...
gauravsaini04 commented 3 months ago

Hi, I could check the value of env var set inside the container to be the same as what's in .devcontainer.json file i.e. 1920x1080x32 Got the following output for this command :

docker exec 6c32dfa6ec6b10cba6b54379cf9a69e6e97919d89e4a78450e6d7d91f399bf7a bash -c 'echo "$VNC_RESOLUTION"'
1920x1080x32
Huoleit commented 3 months ago

Hi, I could check the value of env var set inside the container to be the same as what's in .devcontainer.json file i.e. 1920x1080x32 Got the following output for this command :

docker exec 6c32dfa6ec6b10cba6b54379cf9a69e6e97919d89e4a78450e6d7d91f399bf7a bash -c 'echo "$VNC_RESOLUTION"'
1920x1080x32

I am not referring to it. Env, of course, works as expected if it is passed to docker run -e. Please take a look at /usr/local/share/desktop-init.sh inside the container. All variables are substituted because they are not escaped.

user_name="vscode"
group_name="vscode"
LOG=/tmp/container-init.log

export DBUS_SESSION_BUS_ADDRESS="autolaunch:"
export DISPLAY=":1"
export VNC_RESOLUTION="1440x768x16" 
export LANG="en_US.UTF-8"
export LANGUAGE="en_US.UTF-8"

Echo VNC_RESOLUTION in this case will print the correct user-defined value.

# echo $VNC_RESOLUTION                 
1920x1080x32

But during the build, the user-defined VNC_RESOLUTION is not passed to docker build. VNC_RESOLUTION defined inside /usr/local/share/desktop-init.sh will always be substituted by the default value 1440x768. When starting the container, it always use the default 1440x768 no matter what is given to docker run -e. It can be verified by

# ps -x -ww | grep Xtigervnc
110 ?        Ss     0:00 /usr/bin/Xtigervnc :1 -localhost=1 -desktop fluxbox -rfbport 5901 -SecurityTypes None -auth /home/vscode/.Xauthority -geometry 1440x768 -depth 16 -dpi 96
gauravsaini04 commented 2 months ago

Added pr #1024 as a solution to this problem..

gauravsaini04 commented 1 month ago

PR was merged

samruddhikhandale commented 3 weeks ago

@gauravsaini04 The PR was causing issues as reported in https://github.com/devcontainers/features/issues/1085. Hence, I have reverted your changes.

Can you reopen the issue with the fix for this issue while ensuring desktoplite starts fine post container startup?

gauravsaini04 commented 1 week ago

Have raised pr #1112 where VNC_RESOLUTION has been provided as a container environment variable