From 154df81be5891f9a9c79b1b176f6ca4da60b4c23 Mon Sep 17 00:00:00 2001 From: andy5995 Date: Thu, 21 Nov 2024 01:15:05 -0600 Subject: [PATCH] Simplify --- .github/workflows/docker-test.yml | 22 +++---- .gitignore | 5 +- docker/Dockerfile | 3 +- docker/README.md | 76 +++++++++++++++++-------- docker/default-entry.sh | 12 ++++ docker/docker-compose.yml | 40 +++++++++---- docker/{entrypoint.sh => main_entry.sh} | 8 ++- docker/shell-entry.sh | 6 ++ 8 files changed, 120 insertions(+), 52 deletions(-) create mode 100755 docker/default-entry.sh rename docker/{entrypoint.sh => main_entry.sh} (62%) create mode 100755 docker/shell-entry.sh diff --git a/.github/workflows/docker-test.yml b/.github/workflows/docker-test.yml index 7d8563b3..cf2a8112 100644 --- a/.github/workflows/docker-test.yml +++ b/.github/workflows/docker-test.yml @@ -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 diff --git a/.gitignore b/.gitignore index 04ae98a5..9aac128b 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,7 @@ subprojects/* /bun.lockb /private /logs -/.flatpak-builder \ No newline at end of file +/.flatpak-builder + +# Contains variables specific for the user +/docker/.env diff --git a/docker/Dockerfile b/docker/Dockerfile index d01e751e..4bf43172 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -38,5 +38,4 @@ RUN \ USER root WORKDIR / -ADD entrypoint.sh /entrypoint.sh -ENTRYPOINT ["/entrypoint.sh"] +CMD ["bash", "-l"] diff --git a/docker/README.md b/docker/README.md index d7b79a39..4ad86142 100644 --- a/docker/README.md +++ b/docker/README.md @@ -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 + -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 @@ -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. @@ -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. @@ -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: diff --git a/docker/default-entry.sh b/docker/default-entry.sh new file mode 100755 index 00000000..c241bad6 --- /dev/null +++ b/docker/default-entry.sh @@ -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 + '" diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index c46c9417..cee1a415 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -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 diff --git a/docker/entrypoint.sh b/docker/main_entry.sh similarity index 62% rename from docker/entrypoint.sh rename to docker/main_entry.sh index 9368715d..b4e773eb 100755 --- a/docker/entrypoint.sh +++ b/docker/main_entry.sh @@ -1,13 +1,16 @@ #!/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 @@ -15,4 +18,3 @@ 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" diff --git a/docker/shell-entry.sh b/docker/shell-entry.sh new file mode 100755 index 00000000..b38010b2 --- /dev/null +++ b/docker/shell-entry.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +set -e +/docker/main_entry.sh + +su builder -l -c "cd /workspace && bash"