container-run.sh (1792B)
1 #!/bin/sh 2 3 # A simple script to run commands (e.g. build commands) in a Docker container. 4 # It can be used for testing on platforms different from the host. For example, 5 # the command 6 # 7 # ./container-run.sh arm ./build test 8 # 9 # builds nissy and runs the unit tests in an ARM container. 10 # 11 # The containers are based on Alpine Linux and the contain the necessary tools 12 # to build the main library and the C++ and Python examples. They DO NOT 13 # contain the emscripten compiler, so they cannot be used to build the web 14 # version (e.g. ./build web or ./buidl webtest). 15 # The images are given a tag starting with 'localhost/nissy/'. 16 # 17 # See below for a list of options. 18 19 usage() { 20 echo "$1" 21 echo "" 22 echo "usage: $0 PLATFORM [COMMAND]" 23 echo "" 24 echo "If COMMAND is unspecified, an interactive shell will be opened." 25 echo "" 26 echo "Available platforms:" 27 echo "x86 (equivalent to: x86_64, amd64)" 28 echo "arm (equivalent to: arm64)" 29 echo "" 30 echo "Examples:" 31 echo "$0 ram ./build test # Run unit tests in arm container" 32 exit 1 33 } 34 35 if [ -z "$1" ]; then 36 usage "No platform given." 37 fi 38 39 case "$1" in 40 x86|x86_64|amd64) 41 p=amd64 42 shift 43 ;; 44 arm|arm64) 45 p=arm64 46 shift 47 ;; 48 *) 49 usage "Platform '$1' not available." 50 ;; 51 esac 52 53 image="localhost/nissy/alpine-$p" 54 platform="--platform=linux/$p" 55 mount="--mount type=bind,src=./,dst=/nissy-core" 56 flags="--rm --privileged" 57 58 dockerfile="$(mktemp)" 59 cat > "$dockerfile" << EOF 60 FROM alpine:3.22.1 61 RUN apk update 62 RUN apk add gcc g++ clang python3 python3-dev 63 #COPY . ./nissy-core 64 RUN mkdir /nissy-core 65 WORKDIR /nissy-core 66 USER 1000:1000 67 EOF 68 69 docker build "$platform" -f "$dockerfile" -t "$image" . 70 rm "$dockerfile" 71 72 if [ -z "$1" ]; then 73 docker run $flags $mount "$platform" -it "$image" /bin/sh 74 else 75 docker run $flags $mount "$platform" -t "$image" $@ 76 fi