Skip to content

Commit

Permalink
Update the docs, use entrypoint.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
andy5995 committed Nov 20, 2024
1 parent 5d4010d commit 4f4ba59
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 13 deletions.
15 changes: 7 additions & 8 deletions .github/workflows/docker-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@ jobs:
run: docker build -t test-image -f docker/Dockerfile .

- name: Build Docker image
run: docker build -t test-image -f docker/Dockerfile ./docker
run: docker build -t test-image -f docker/Dockerfile docker

- name: Run commands in Docker container
run: |
docker run --rm -v ${{ github.workspace }}:/workspace --user root \
-e HOST_UID=$(id -u) -e HOST_GID=$(id -g) test-image /bin/bash -c "
usermod -u \$HOST_UID builder && groupmod -g \$HOST_GID builder &&
chown -R \$HOST_UID:\$HOST_GID /home/builder &&
su builder -l -c 'cd /workspace &&
docker run --rm -v ${{ github.workspace }}:/workspace \
-e HOSTUID=$(id -u) -e HOSTGID=$(id -g) test-image \
'/bin/bash -l -c "
cd /workspace &&
bun install &&
bun run codegen &&
bun run meson-setup.clang-release &&
bun run vs.example
'"
meson compile -C build/ vs:executable
"'
3 changes: 2 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ RUN \
echo "export PATH=\"\$BUN_INSTALL/bin:\$PATH\"" >> $HOME/.profile

USER root
CMD ["bash", "-l"]
ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
84 changes: 81 additions & 3 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Using docker-compose

You may build and test your changes by using the compose file:
You may build your changes by using the compose file:

docker-compose -f ./docker/docker-compose.yml up

Expand All @@ -11,8 +11,9 @@ You may build and test your changes by using the compose file:
While in the repository root, to enter the environment and be presented with a
shell:

docker run -it --rm -u builder \
-v $PWD:/workspace ghcr.io/KaruroChori/vs-fltk:build-env
docker run -it --rm \
-e HOSTUID=$(id -u) -e HOSTGID=$(id -g)\
-v $PWD:/workspace ghcr.io/KaruroChori/vs-fltk:build-env "bash -l"

This will mount your current directory as */workspace* inside the container.
Your username will be *builder*. By default, you will not have root privileges
Expand All @@ -28,4 +29,81 @@ You can pull the image manually:
If you use `docker-compose` or `docker run ...` the image will be pulled
automatically the first time.

## Running the app from the container

Note the following instructions were adapted from instructions generated by
ChatGPT. The X11 instructions were tested on Manjaro Linux. Use at your own
risk. Please update these docs if you have any information about accuracy or
security-related considerations.

To run an application from a Docker container that requires access to the host
machine's graphics (such as a GUI application), you'll need to share certain
resources from the host with the container. This involves passing through the
host's display (X11 or Wayland, depending on your system) and sharing the
necessary devices. Here's a general guide:

### For X11-based systems (e.g., most Linux desktop environments)

1. **Install dependencies on the host:**
Make sure you have `xhost` installed, which allows you to control access to
the X server.

sudo apt install x11-xserver-utils # For Ubuntu/Debian
sudo pacman -S xorg-xhost # For Arch-based


2. **Allow access to X server:**
Before running the container, allow Docker to access your X server:

xhost +local:docker


3. **Run the container with access to the display:**
When starting the container, pass the display environment variable
(`$DISPLAY`) and mount the X11 socket:

```bash
docker run -it --rm \
-v $PWD:/workspace \
--env="DISPLAY" \
--env="QT_X11_NO_MITSHM=1" \
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
ghcr.io/KaruroChori/vs-fltk:build-env
```

- `--env="DISPLAY"`: Passes the display from the host to the container.
- `--env="QT_X11_NO_MITSHM=1"`: Some applications may need this to avoid shared memory issues.
- `--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw"`: Shares the X11 socket.

4. **Run the graphical application inside the container:**
Once inside the container, running the app should open its GUI on your host's screen.

### For Wayland-based systems (e.g., some modern Linux environments):

1. **Share the Wayland display:**
If your system uses Wayland, you need to pass the Wayland display and devices:

```bash
docker run -it --rm -u builder \
-v $PWD:/workspace \
--env="WAYLAND_DISPLAY=$WAYLAND_DISPLAY" \
--volume="/run/user/$(id -u)/wayland:/run/user/$(id -u)/wayland" \
--device=/dev/dri \
ghcr.io/KaruroChori/vs-fltk:build-env
```

- `--env="WAYLAND_DISPLAY=$WAYLAND_DISPLAY"`: Passes the Wayland display variable.
- `--volume="/run/user/$(id -u)/wayland:/run/user/$(id -u)/wayland"`: Shares the Wayland display socket.
- `--device=/dev/dri`: Provides access to GPU devices (for rendering).

### Additional considerations:

- **GPU access**: If your application requires GPU acceleration (e.g.,
OpenGL), you may also need to pass through GPU devices to the container. For
example, with NVIDIA, you can use the NVIDIA Docker runtime:

docker run --gpus all -it ghcr.io/KaruroChori/vs-fltk:build-env

- **X11 access security**: Allowing Docker to connect to your X server with `xhost +local:docker` is not secure. After you're done, revoke access:

xhost -local:docker
4 changes: 3 additions & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ services:
volumes:
- ${PWD}:/workspace
working_dir: /workspace
user: builder
command: >
bash -l -c "
bun install &&
bun run codegen &&
bun run meson-setup.clang-release &&
meson compile -C build/ vs:executable
"
environment:
HOSTUID: $HOSTUID
HOSTUID: $HOSTGID
stdin_open: true # Allows interactive mode
tty: true # Allocate a pseudo-TTY
18 changes: 18 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

OLDPWD=$PWD

if [ -z "HOSTUID" ]; then
echo "HOSTUID is not set."
exit 1
fi

if [ -z "HOSTGID" ]; then
echo "HOSTGID is not set."
exit 1
fi

usermod -u $HOSTUID builder
groupmod -g $HOSTGID builder
chown -R $HOSTUID:$HOSTGID /home/builder
su builder -c "PATH=/home/builder/.local/bin:$PATH && cd $OLDPWD && $1"

0 comments on commit 4f4ba59

Please sign in to comment.