Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
andy5995 committed Nov 21, 2024
1 parent fb5f142 commit 154df81
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 52 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/docker-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ on:
jobs:
build-test-env-image:
runs-on: ubuntu-latest
env:
SOURCE_ROOT: ${{ github.workspace }}
IMAGE: test-image
ENTRYPOINT: ${{ github.workspace }}/docker/default-entry.sh
steps:
- uses: actions/checkout@v4

- name: Install dependencies
run: sudo apt install -y docker-compose

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

- name: Run commands in Docker container
run: |
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 &&
meson compile -C build/ vs:executable
"'
export HOSTGID=$(id -u)
export HOSTGID=$(id -g)
docker-compose -f docker/docker-compose.yml run --rm dev
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ subprojects/*
/bun.lockb
/private
/logs
/.flatpak-builder
/.flatpak-builder

# Contains variables specific for the user
/docker/.env
3 changes: 1 addition & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ RUN \

USER root
WORKDIR /
ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["bash", "-l"]
76 changes: 53 additions & 23 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,57 @@

## Using docker-compose

You may build your changes by using the compose file:
You may build your changes by using the compose file. First create an `.env`
file in /docker. The contents should be similar to:

```text
HOSTUID=1000
HOSTGID=1000
IMAGE="ghcr.io/KaruroChori/vs-fltk:build-env"
ENTRYPOINT:$PWD/docker/default-entry.sh
SOURCE_ROOT=$PWD
```

Replace the values for HOSTUID and HOSTGID with your system uid and gid. Then,
from the source root:

docker-compose -f docker/docker-compose.yml run --rm dev

This will start the container and build the app.

To enter the build environment without actually building the app:

export ENTRYPOINT=$PWD/docker/shell-entry.sh
docker-compose -f docker/docker-compose.yml run --rm dev

Remember to unset ENTRYPOINT when you wish to use the default again.

docker-compose -f ./docker/docker-compose.yml up
<!--
This doesn't work unless the files are "mounted"
To run the image without using compose:
## Entering the build environment
```sh
docker run -it --rm -v \
--entrypoint=$PWD/docker/default-entry.sh \
-v $PWD:/workspace \
ghcr.io/KaruroChori/vs-fltk:build-env
```
-->

While in the repository root, to enter the environment and be presented with a
shell:
All of the above methods will mount your current directory as */workspace*
inside the container. Your username will be *builder*. By default, you will
not have root privileges (which are not necessary to build and test). However,
you can use `sudo` if you need to run `apt` or any other commands that require
root access.

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

This will mount your current directory as */workspace* inside the container.
Your username will be *builder*. By default, you will not have root privileges
(which are not necessary to build and test). However, you can use `sudo` if
you need to run `apt` or any other commands that require root access.
In the examples above, we've included `--rm` as an argument. This normally
removes the container after it's exited. `docker ps -a` displays containers
that still exist so you may periodically want to make sure you don't have
unused or unwanted containers. See the official [Docker docs] for more
information about working with containers. You may, for example, want to
"reuse" a container, in which case, simply omit the `--rm`.

## Getting the image

Expand Down Expand Up @@ -60,15 +94,14 @@ necessary devices. Here's a general guide:

3. **Run the container with access to the display:**
When starting the container, pass the display environment variable
(`$DISPLAY`) and mount the X11 socket:
(`$DISPLAY`) and mount the X11 socket. Instead of *dev" for the last
argument of `docker-compose` (see above), use *xgui*, which essentially
adds these three arguments (through the compose configuration):

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

- `--env="DISPLAY"`: Passes the display from the host to the container.
Expand All @@ -83,13 +116,10 @@ docker run -it --rm \
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 \
```sh
--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.
Expand All @@ -102,7 +132,7 @@ docker run -it --rm -u builder \
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
--gpus all -it

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

Expand Down
12 changes: 12 additions & 0 deletions docker/default-entry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

set -e
/docker/main_entry.sh

su builder -c "bash -l -c '\
cd /workspace && \
bun install &&
bun run codegen &&
bun run meson-setup.clang-release &&
meson compile -C build/ vs:executable
'"
40 changes: 28 additions & 12 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
version: '3.8'

services:
dev:
image: ghcr.io/KaruroChori/vs-fltk:build-env
image: $IMAGE
container_name: vs-fltk-env
entrypoint: $ENTRYPOINT
volumes:
- ${PWD}:/workspace
- $SOURCE_ROOT:/workspace
- $ENTRYPOINT:$ENTRYPOINT
- ${PWD}/docker/main_entry.sh:/docker/main_entry.sh
environment:
HOSTUID: $HOSTUID
HOSTGID: $HOSTGID
ENTRYPOINT: $ENTRYPOINT
working_dir: /workspace
command: >
bash -l -c "
bun install &&
bun run codegen &&
bun run meson-setup.clang-release &&
meson compile -C build/ vs:executable
"
stdin_open: true # Allows interactive mode
tty: true # Allocate a pseudo-TTY
restart: no

xgui:
image: $IMAGE
container_name: vs-fltk-gui
entrypoint: $ENTRYPOINT
volumes:
- $SOURCE_ROOT:/workspace
- $ENTRYPOINT:$ENTRYPOINT
- ${PWD}/docker/main_entry.sh:/docker/main_entry.sh
- /tmp/.X11-unix:/tmp/.X11-unix:rw
environment:
HOSTUID: $HOSTUID
HOSTUID: $HOSTGID
HOSTGID: $HOSTGID
ENTRYPOINT: $ENTRYPOINT
DISPLAY: $DISPLAY
QT_X11_NO_MITSHM: 1
working_dir: /workspace
stdin_open: true # Allows interactive mode
tty: true # Allocate a pseudo-TTY
restart: no
8 changes: 5 additions & 3 deletions docker/entrypoint.sh → docker/main_entry.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#!/bin/bash
# main_entry.sh

set -e

OLDPWD=$PWD

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

if [ -z "HOSTGID" ]; then
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"
6 changes: 6 additions & 0 deletions docker/shell-entry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

set -e
/docker/main_entry.sh

su builder -l -c "cd /workspace && bash"

0 comments on commit 154df81

Please sign in to comment.