From 6a831eb2bd18d5e4c28a420b95c20c5c9c89b10b Mon Sep 17 00:00:00 2001 From: Cody Littley <56973212+cody-littley@users.noreply.github.com> Date: Wed, 11 Sep 2024 08:02:12 -0500 Subject: [PATCH] Lightnode docker (#726) Signed-off-by: Cody Littley --- .gitignore | 3 ++ lightnode/Makefile | 30 +++++++++++++++++++ lightnode/docker/Dockerfile | 48 ++++++++++++++++++++++++++++++ lightnode/docker/build.sh | 13 ++++++++ lightnode/docker/clean.sh | 5 ++++ lightnode/docker/debug.sh | 13 ++++++++ lightnode/docker/default-args.sh | 5 ++++ lightnode/docker/run.sh | 12 ++++++++ lightnode/docker/setup-data-dir.sh | 13 ++++++++ lightnode/main/main.go | 8 +++++ 10 files changed, 150 insertions(+) create mode 100644 lightnode/Makefile create mode 100644 lightnode/docker/Dockerfile create mode 100755 lightnode/docker/build.sh create mode 100755 lightnode/docker/clean.sh create mode 100755 lightnode/docker/debug.sh create mode 100644 lightnode/docker/default-args.sh create mode 100755 lightnode/docker/run.sh create mode 100755 lightnode/docker/setup-data-dir.sh create mode 100644 lightnode/main/main.go diff --git a/.gitignore b/.gitignore index 32971ffeb..dce8677fa 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,7 @@ coverage.* contracts/broadcast +lightnode/docker/build-info.txt +lightnode/docker/args.sh + .idea diff --git a/lightnode/Makefile b/lightnode/Makefile new file mode 100644 index 000000000..70f0218ff --- /dev/null +++ b/lightnode/Makefile @@ -0,0 +1,30 @@ +SHELL := /bin/bash + +# Clean the light node build files. +clean: + rm -rf ./bin + +# Build the light node. +build: clean + go mod tidy + go build -o ./bin/lnode ./main + +# Run the light node. +run: build + ./bin/lnode + +# Delete the docker images for the light node. +clean-docker: + ./docker/clean.sh + +# Build the docker images for the light node. +build-docker: + ./docker/build.sh + +# Run the docker image for the light node. +run-docker: + ./docker/run.sh + +# Open an interactive bash shell inside the light node docker container. Useful for debugging issues with the image. +debug-docker: + ./docker/debug.sh diff --git a/lightnode/docker/Dockerfile b/lightnode/docker/Dockerfile new file mode 100644 index 000000000..11f89b42e --- /dev/null +++ b/lightnode/docker/Dockerfile @@ -0,0 +1,48 @@ +FROM golang:1.21.12-bookworm AS base + +# Install core dependencies +RUN apt update +RUN apt install -y build-essential + +# Set up lnode user +RUN useradd -m -s /bin/bash lnode +USER lnode +WORKDIR /home/lnode +# Remove default crud +RUN rm .bashrc +RUN rm .bash_logout +RUN rm .profile + +# Copy go.mod and download dependencies without copying the entire repository. +# Prevents us from downloading dependencies every time we make a change to the repository. +RUN mkdir -p /home/lnode/eigenda +COPY --chown=lnode go.mod /home/lnode/eigenda/go.mod +COPY --chown=lnode go.sum /home/lnode/eigenda/go.sum +WORKDIR /home/lnode/eigenda +RUN go mod download + +# Copy the remainder of the eigenda repository +COPY --chown=lnode . /home/lnode/eigenda + +# Download all go dependencies and build the binary. +WORKDIR /home/lnode/eigenda/lightnode +RUN make build + +# In order to equip this image with a shell for debugging, +# swap out the "FROM scratch" below with "FROM golang:1.21.12-bookworm" +FROM scratch AS final + +# Copy over files needed for lnode user. +COPY --from=base /usr/share/zoneinfo /usr/share/zoneinfo +COPY --from=base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=base /etc/passwd /etc/passwd +COPY --from=base /etc/group /etc/group + +USER lnode +WORKDIR /home/lnode + +# Copy the executable binary. +COPY --from=base /home/lnode/eigenda/lightnode/bin/lnode /home/lnode/lnode + +# Run the light node when the container starts. +CMD ["/home/lnode/lnode"] \ No newline at end of file diff --git a/lightnode/docker/build.sh b/lightnode/docker/build.sh new file mode 100755 index 000000000..ed4ecc0fc --- /dev/null +++ b/lightnode/docker/build.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +# The location where this script can be found. +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +cd "${SCRIPT_DIR}" +REPO_ROOT=$(git rev-parse --show-toplevel) +cd "${REPO_ROOT}" + +docker build \ + --build-arg="REPO_ROOT=${REPO_ROOT}" \ + -f "lightnode/docker/Dockerfile" \ + --tag lnode:latest \ + . diff --git a/lightnode/docker/clean.sh b/lightnode/docker/clean.sh new file mode 100755 index 000000000..997b34be8 --- /dev/null +++ b/lightnode/docker/clean.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +# Cleans the docker image and all cached steps. +docker image rm lnode 2> /dev/null || true +docker builder prune -f diff --git a/lightnode/docker/debug.sh b/lightnode/docker/debug.sh new file mode 100755 index 000000000..5f354f0ac --- /dev/null +++ b/lightnode/docker/debug.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +# Starts the container with an interactive bash shell. This is useful for debugging the container. + +# Do setup for the data directory. This is a directory where data that needs +# to persist in-between container runs is stored. +source ./docker/setup-data-dir.sh + +docker container run \ + --mount "type=bind,source=${DATA_PATH},target=/home/lnode/data" \ + --rm \ + -it \ + lnode bash diff --git a/lightnode/docker/default-args.sh b/lightnode/docker/default-args.sh new file mode 100644 index 000000000..fde5b97e0 --- /dev/null +++ b/lightnode/docker/default-args.sh @@ -0,0 +1,5 @@ +# Default arguments for building the docker image. +# To override these values locally, create a file named `args.sh` in the same directory. 'args.sh' is ignored by git. + +# The location on the host file system where light node data will be stored. +export DATA_PATH=~/.lnode-data diff --git a/lightnode/docker/run.sh b/lightnode/docker/run.sh new file mode 100755 index 000000000..8076de8c6 --- /dev/null +++ b/lightnode/docker/run.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +# Starts the container and runs the light node. + +# Do setup for the data directory. This is a directory where data that needs +# to persist in-between container runs is stored. +source ./docker/setup-data-dir.sh + +docker run \ + --rm \ + --mount "type=bind,source=${DATA_PATH},target=/home/lnode/data" \ + lnode diff --git a/lightnode/docker/setup-data-dir.sh b/lightnode/docker/setup-data-dir.sh new file mode 100755 index 000000000..3f0b371c6 --- /dev/null +++ b/lightnode/docker/setup-data-dir.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +# Sets up the data directory for the light node container. + +# Default arguments. +source docker/default-args.sh +# Local overrides for arguments. +source docker/args.sh + +# Create the data directory if it doesn't exist. +mkdir -p $DATA_PATH + +echo "Using data directory $DATA_PATH" diff --git a/lightnode/main/main.go b/lightnode/main/main.go new file mode 100644 index 000000000..31b237a54 --- /dev/null +++ b/lightnode/main/main.go @@ -0,0 +1,8 @@ +package main + +import "fmt" + +// main is the entrypoint for the light node. +func main() { + fmt.Println("Hello world") +}