From a29e6388895b875879ae0234e0baebe9e2f5d746 Mon Sep 17 00:00:00 2001 From: Sebastian Ziebell Date: Fri, 6 May 2022 13:49:34 +0200 Subject: [PATCH 1/6] Reduce size of Docker image * split commands to install Rust and use cargo * install Rust with minimal profile * use `slim` base image * remove installation from a few packages --- .devcontainer/Dockerfile | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 172b851b..e15f9114 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,23 +1,29 @@ -FROM debian:bullseye +FROM debian:bullseye-slim ENV DEBIAN_FRONTEND=noninteractive ENV LC_ALL=C.UTF-8 ENV LANG=C.UTF-8 ARG CONTAINER_USER=esp ARG CONTAINER_GROUP=esp ARG NIGHTLY_VERSION=nightly-2022-03-10 + RUN apt-get update \ - && apt-get install -y vim nano git curl gcc ninja-build cmake libudev-dev \ - python3 python3-pip libusb-1.0-0 libssl-dev pkg-config libtinfo5 clang \ - && apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts + && apt-get install -y git curl ninja-build clang libudev-dev \ + python3 python3-pip libusb-1.0-0 libssl-dev pkg-config libtinfo5 \ + && apt-get clean -y \ + && rm -rf /var/lib/apt/lists/* /tmp/library-scripts + RUN adduser --disabled-password --gecos "" ${CONTAINER_USER} USER ${CONTAINER_USER} WORKDIR /home/${CONTAINER_USER} ENV PATH=${PATH}:$HOME/.cargo/bin + RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- \ - --default-toolchain ${NIGHTLY_VERSION} -y \ + --default-toolchain ${NIGHTLY_VERSION} -y --profile minimal \ && $HOME/.cargo/bin/rustup component add rust-src --toolchain ${NIGHTLY_VERSION} \ - && $HOME/.cargo/bin/rustup target add riscv32i-unknown-none-elf \ - && $HOME/.cargo/bin/cargo install cargo-generate cargo-espflash espmonitor bindgen ldproxy + && $HOME/.cargo/bin/rustup target add riscv32i-unknown-none-elf + +RUN $HOME/.cargo/bin/cargo install cargo-generate cargo-espflash espmonitor bindgen ldproxy + ENV ESP_BOARD=esp32c3 ENV ESP_IDF_VERSION=release/v4.4 RUN mkdir -p .espressif/frameworks/ \ @@ -27,4 +33,5 @@ RUN mkdir -p .espressif/frameworks/ \ && .espressif/frameworks/esp-idf-v4.4/install.sh ${ESP_BOARD} \ && rm -rf .espressif/dist \ && rm -rf .espressif/frameworks/esp-idf-v4.4/docs + CMD "/bin/bash" \ No newline at end of file From ef9e504977f91a893984e090843e977eabcb0c01 Mon Sep 17 00:00:00 2001 From: Sebastian Ziebell Date: Fri, 13 May 2022 12:39:55 +0200 Subject: [PATCH 2/6] Remove package install & delete examples / tools --- .devcontainer/Dockerfile | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index e15f9114..d3d0b435 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -5,6 +5,8 @@ ENV LANG=C.UTF-8 ARG CONTAINER_USER=esp ARG CONTAINER_GROUP=esp ARG NIGHTLY_VERSION=nightly-2022-03-10 +ARG ESP_IDF_VERSION=release/v4.4 +ARG ESP_BOARD=esp32c3 RUN apt-get update \ && apt-get install -y git curl ninja-build clang libudev-dev \ @@ -22,16 +24,21 @@ RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- \ && $HOME/.cargo/bin/rustup component add rust-src --toolchain ${NIGHTLY_VERSION} \ && $HOME/.cargo/bin/rustup target add riscv32i-unknown-none-elf -RUN $HOME/.cargo/bin/cargo install cargo-generate cargo-espflash espmonitor bindgen ldproxy +RUN $HOME/.cargo/bin/cargo install cargo-espflash espmonitor bindgen ldproxy -ENV ESP_BOARD=esp32c3 -ENV ESP_IDF_VERSION=release/v4.4 RUN mkdir -p .espressif/frameworks/ \ && git clone --branch ${ESP_IDF_VERSION} -q --depth 1 --shallow-submodules \ --recursive https://github.com/espressif/esp-idf.git \ .espressif/frameworks/esp-idf-v4.4 \ + && python3 .espressif/frameworks/esp-idf-v4.4/tools/idf_tools.py install cmake \ && .espressif/frameworks/esp-idf-v4.4/install.sh ${ESP_BOARD} \ && rm -rf .espressif/dist \ - && rm -rf .espressif/frameworks/esp-idf-v4.4/docs + && rm -rf .espressif/frameworks/esp-idf-v4.4/docs \ + && rm -rf .espressif/frameworks/esp-idf-v4.4/examples \ + && rm -rf .espressif/frameworks/esp-idf-v4.4/tools/esp_app_trace \ + && rm -rf .espressif/frameworks/esp-idf-v4.4/tools/test_idf_size -CMD "/bin/bash" \ No newline at end of file +ENV IDF_TOOLS_PATH=/home/${CONTAINER_USER}/.espressif +RUN echo "source /home/${CONTAINER_USER}/.espressif/frameworks/esp-idf-v4.4/export.sh > /dev/null 2>&1" >> ~/.bashrc + +CMD "/bin/bash" From 90a8ead1cf1adf6dd7a31ac141f4fd438f233ad1 Mon Sep 17 00:00:00 2001 From: Sebastian Ziebell Date: Tue, 17 May 2022 10:46:39 +0200 Subject: [PATCH 3/6] Refine Github workflow to test Docker image --- .github/workflows/dockerimage.yml | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/.github/workflows/dockerimage.yml b/.github/workflows/dockerimage.yml index 58896d71..aab18849 100644 --- a/.github/workflows/dockerimage.yml +++ b/.github/workflows/dockerimage.yml @@ -2,22 +2,19 @@ name: Build Docker Image on: - pull_request: - paths: - - '!book/**' - branches: - - main push: - paths: - - '!book/**' - branches: - - main + branches: [main] + pull_request: + branches: [main] + paths-ignore: + - "book/" jobs: # To avoid uploading the Docker image to a registry yet # the image is built here & immediately used to test all exercises in. # TODO at a later point split that into separate jobs build_image_and_test: + name: Build & test Docker image runs-on: ubuntu-latest steps: - name: Checkout the repository From db48bb47cfbc7d5c7ab93af5469b7e5a5c2217bf Mon Sep 17 00:00:00 2001 From: Sebastian Ziebell Date: Tue, 17 May 2022 11:06:39 +0200 Subject: [PATCH 4/6] Fix Docker command to remove TTY Github Actions do not support a shell using TTY, therefore this option is removed from the shell command. --- .github/workflows/dockerimage.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/dockerimage.yml b/.github/workflows/dockerimage.yml index aab18849..267723d4 100644 --- a/.github/workflows/dockerimage.yml +++ b/.github/workflows/dockerimage.yml @@ -1,5 +1,5 @@ --- -name: Build Docker Image +name: Docker Image on: push: @@ -14,7 +14,7 @@ jobs: # the image is built here & immediately used to test all exercises in. # TODO at a later point split that into separate jobs build_image_and_test: - name: Build & test Docker image + name: Build & test image runs-on: ubuntu-latest steps: - name: Checkout the repository @@ -26,4 +26,4 @@ jobs: - name: Test code examples in Docker image run: | docker run --user esp --mount type=bind,source="$(pwd)",target=/home/esp/workspace,consistency=cached \ - --rm -it esp:latest /bin/bash /home/esp/workspace/.devcontainer/test.sh \ No newline at end of file + --rm -i esp:latest /bin/bash /home/esp/workspace/.devcontainer/test.sh \ No newline at end of file From 91b570ea156c296612bef171b5e1f68093982dcf Mon Sep 17 00:00:00 2001 From: Sebastian Ziebell Date: Tue, 17 May 2022 13:42:59 +0200 Subject: [PATCH 5/6] Fix Docker image for VS Code & Github CI The `Dockerfile` is built with the default `root` user. This is to support a successful Docker build on Github CI. Github Actions have a few restrictions, for example `USER` & `WORKDIR` directives should not be used. * refactor Dockerfile, remove `USER` & `WORKDIR` directives * fix VS Code devcontainer config * use `root` user * fix workspace folder location * update extensions * code formating * adjust Github CI config to build Docker image * update book instructions on Docker --- .devcontainer/Dockerfile | 7 +++---- .devcontainer/devcontainer.json | 14 +++++++------- .devcontainer/test.sh | 2 +- .github/workflows/dockerimage.yml | 4 ++-- book/src/02_2_software.md | 4 ++-- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index d3d0b435..92280f48 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,9 +1,11 @@ +# There are a few Dockerfile restrictions when using Github Actions +# See: https://docs.github.com/en/actions/creating-actions/dockerfile-support-for-github-actions + FROM debian:bullseye-slim ENV DEBIAN_FRONTEND=noninteractive ENV LC_ALL=C.UTF-8 ENV LANG=C.UTF-8 ARG CONTAINER_USER=esp -ARG CONTAINER_GROUP=esp ARG NIGHTLY_VERSION=nightly-2022-03-10 ARG ESP_IDF_VERSION=release/v4.4 ARG ESP_BOARD=esp32c3 @@ -14,9 +16,6 @@ RUN apt-get update \ && apt-get clean -y \ && rm -rf /var/lib/apt/lists/* /tmp/library-scripts -RUN adduser --disabled-password --gecos "" ${CONTAINER_USER} -USER ${CONTAINER_USER} -WORKDIR /home/${CONTAINER_USER} ENV PATH=${PATH}:$HOME/.cargo/bin RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- \ diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 642c1e0a..88f2359c 100755 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -4,7 +4,7 @@ "dockerfile": "Dockerfile", "args": { "NIGHTLY_VERSION": "nightly-2022-03-10", - "CONTAINER_USER": "esp" + "CONTAINER_USER": "root" } }, "settings": { @@ -18,17 +18,17 @@ }, "rust-analyzer.checkOnSave.command": "clippy", "[rust]": { - "editor.defaultFormatter": "matklad.rust-analyzer" - }, + "editor.defaultFormatter": "rust-lang.rust" + } }, "extensions": [ - "matklad.rust-analyzer", + "rust-lang.rust-analyzer", "tamasfe.even-better-toml", "vadimcn.vscode-lldb", "serayuzgur.crates", "mutantdino.resourcemonitor", - "yzhang.markdown-all-in-one", + "yzhang.markdown-all-in-one" ], - "workspaceMount": "source=${localWorkspaceFolder},target=/home/esp/workspace,type=bind,consistency=cached", - "workspaceFolder": "/home/esp/workspace/" + "workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind,consistency=cached", + "workspaceFolder": "/workspace" } \ No newline at end of file diff --git a/.devcontainer/test.sh b/.devcontainer/test.sh index efc233e6..f39cb51c 100644 --- a/.devcontainer/test.sh +++ b/.devcontainer/test.sh @@ -2,7 +2,7 @@ set -ef -WORK_DIR=$HOME/workspace +WORK_DIR=/workspace echo "Compiling all exercises & library crates" for file in $(find ${WORK_DIR} -name "Cargo.toml") diff --git a/.github/workflows/dockerimage.yml b/.github/workflows/dockerimage.yml index 267723d4..583d2e80 100644 --- a/.github/workflows/dockerimage.yml +++ b/.github/workflows/dockerimage.yml @@ -25,5 +25,5 @@ jobs: - name: Test code examples in Docker image run: | - docker run --user esp --mount type=bind,source="$(pwd)",target=/home/esp/workspace,consistency=cached \ - --rm -i esp:latest /bin/bash /home/esp/workspace/.devcontainer/test.sh \ No newline at end of file + docker run --mount type=bind,source="$(pwd)",target=/workspace,consistency=cached \ + --rm esp:latest /bin/bash /workspace/.devcontainer/test.sh \ No newline at end of file diff --git a/book/src/02_2_software.md b/book/src/02_2_software.md index 1bbfb61a..4d6d3651 100644 --- a/book/src/02_2_software.md +++ b/book/src/02_2_software.md @@ -94,11 +94,11 @@ Building the image takes a while depending on the OS & hardware (20-30 minutes). To start the new Docker container run: ```console -$ docker run --user esp --mount type=bind,source="$(pwd)",target=/home/esp/workspace,consistency=cached -it esp /bin/bash +$ docker run --mount type=bind,source="$(pwd)",target=/workspace,consistency=cached -it esp /bin/bash ``` This starts an interactive shell in the Docker container. It also mounts the local repository to a folder -named `/home/esp/workspace` inside the container. Changes to the project on the host system are reflected inside the container & vice versa. +named `/workspace` inside the container. Changes to the project on the host system are reflected inside the container & vice versa. Using this Docker setup requires certain commands to run inside the container, while other have to be executed on the host system. It's recommended to keep two terminals open, one connected to the Docker container, one on the host system. From f5bb136d5094a5c016e847c85faf01ec6cc67fda Mon Sep 17 00:00:00 2001 From: Sebastian Ziebell Date: Tue, 17 May 2022 14:56:59 +0200 Subject: [PATCH 6/6] Trying to fix VS Code devcontainer build * remove `bindgen` from Dockerfile --- .devcontainer/Dockerfile | 7 +++---- .devcontainer/devcontainer.json | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 92280f48..6a275278 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -5,7 +5,6 @@ FROM debian:bullseye-slim ENV DEBIAN_FRONTEND=noninteractive ENV LC_ALL=C.UTF-8 ENV LANG=C.UTF-8 -ARG CONTAINER_USER=esp ARG NIGHTLY_VERSION=nightly-2022-03-10 ARG ESP_IDF_VERSION=release/v4.4 ARG ESP_BOARD=esp32c3 @@ -23,7 +22,7 @@ RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- \ && $HOME/.cargo/bin/rustup component add rust-src --toolchain ${NIGHTLY_VERSION} \ && $HOME/.cargo/bin/rustup target add riscv32i-unknown-none-elf -RUN $HOME/.cargo/bin/cargo install cargo-espflash espmonitor bindgen ldproxy +RUN $HOME/.cargo/bin/cargo install cargo-espflash espmonitor ldproxy RUN mkdir -p .espressif/frameworks/ \ && git clone --branch ${ESP_IDF_VERSION} -q --depth 1 --shallow-submodules \ @@ -37,7 +36,7 @@ RUN mkdir -p .espressif/frameworks/ \ && rm -rf .espressif/frameworks/esp-idf-v4.4/tools/esp_app_trace \ && rm -rf .espressif/frameworks/esp-idf-v4.4/tools/test_idf_size -ENV IDF_TOOLS_PATH=/home/${CONTAINER_USER}/.espressif -RUN echo "source /home/${CONTAINER_USER}/.espressif/frameworks/esp-idf-v4.4/export.sh > /dev/null 2>&1" >> ~/.bashrc +ENV IDF_TOOLS_PATH=/root/.espressif +RUN echo "source /root/.espressif/frameworks/esp-idf-v4.4/export.sh > /dev/null 2>&1" >> ~/.bashrc CMD "/bin/bash" diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 88f2359c..3aa6075a 100755 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -3,8 +3,7 @@ "build": { "dockerfile": "Dockerfile", "args": { - "NIGHTLY_VERSION": "nightly-2022-03-10", - "CONTAINER_USER": "root" + "NIGHTLY_VERSION": "nightly-2022-03-10" } }, "settings": { @@ -18,7 +17,7 @@ }, "rust-analyzer.checkOnSave.command": "clippy", "[rust]": { - "editor.defaultFormatter": "rust-lang.rust" + "editor.defaultFormatter": "rust-lang.rust-analyzer" } }, "extensions": [ @@ -29,6 +28,7 @@ "mutantdino.resourcemonitor", "yzhang.markdown-all-in-one" ], + "remoteUser": "root", "workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind,consistency=cached", "workspaceFolder": "/workspace" } \ No newline at end of file