move making image name to a function

support try using build environment file
refactor Dockerfile to use heredoc and RUN --mount instead of COPY
refactor Dockerfile init RUN to copy /bin and env directories from source always
user build.env and run.env for additional build and run environment
bake run.env into image
master
David Kebler 2023-04-03 14:14:17 -07:00
parent e5a57361dc
commit 16bc3bd929
20 changed files with 122 additions and 105 deletions

View File

@ -2,15 +2,13 @@
ARG BASE_IMAGE ARG BASE_IMAGE
FROM $BASE_IMAGE FROM $BASE_IMAGE
ARG BASE_IMAGE ARG BASE_IMAGE
ARG KEEP
ARG SYSADMIN_PW ARG SYSADMIN_PW
ARG LINUX_DISTRO=alpine ARG LINUX_DISTRO=alpine
ARG BUILD_DIR=/build WORKDIR /build
WORKDIR $BUILD_DIR
COPY .src/packages ./packages
# PACKAGES # PACKAGES
RUN <<eot RUN --mount=type=bind,source=.src/packages,target=/build/packages \
<<eot
echo -e "\n ************************************************* \n" echo -e "\n ************************************************* \n"
echo "Building Image from Base: $BASE_IMAGE" echo "Building Image from Base: $BASE_IMAGE"
echo "Distro: $LINUX_DISTRO; *****" echo "Distro: $LINUX_DISTRO; *****"
@ -20,17 +18,41 @@ echo -e "\n********************************************************"
eot eot
# END PACKAGES # END PACKAGES
COPY .src/init ./init
# INITIALIZATION # INITIALIZATION
RUN echo -e "\n ************************************************* \n" \ RUN --mount=type=bind,source=.src/init,target=/build/init \
echo "****** Initializing Image "; \ <<eot
cd init; \ echo -e "\n ************************************************* \n"
chmod -R +x .; \ echo "****** Initializing Image with build source ******"
pwd; ls -la; \ cd init
echo " ---- running init script ---"; \ pwd; ls -la
/bin/bash ./init.sh; \ if [[ -d env/ ]]; then
echo "copying env/ to /opt/env"
/bin/cp -R -p env/. /opt/env
ls -la /opt/env
fi
if [[ -f env/build.env ]]; then
source /opt/env/build.env
rm -f /opt/env/build.env
echo sourced /opt/env/build.env and deleted
fi
if [[ -d bin/ ]]; then
echo "copying bin/ to /opt/bin"
/bin/cp -R -p bin/. /opt/bin
chmod -R +x /opt/bin
ls -la /opt/bin
fi
echo " ---- running init script init.sh ---"
/bin/bash ./init.sh
echo "############## Finished run init build script(s) ###########################"
if [[ -f /opt/env/run.env ]]; then
echo "sourcing /opt/env/run.env from /etc/profile"
echo "&&&&&& last 10 of /etc/profile &&&&&"
echo -e "# added from image build\nsource /opt/env/run.env" >> /etc/profile
tail /etc/profile
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%"
fi
echo -e "\n ************* End Initialzation ************************" echo -e "\n ************* End Initialzation ************************"
eot
# END INITIALIZATION # END INITIALIZATION
# default command # default command

36
build
View File

@ -16,7 +16,7 @@ source $BDIR/lib/load.sh
case "$1" in case "$1" in
try) try)
shift 1 shift 1
popd > /dev/null || return 2 # type try_container
try_container "$@" try_container "$@"
return $? return $?
;; ;;
@ -49,11 +49,10 @@ exit_abnormal() { # Function: Exit with error.
return ${1:-1} return ${1:-1}
} }
[[ -z "$PS1" ]] || no_prompt=true [[ -z "$PS1" ]] || no_prompt=true
overwrite=true overwrite=true
while getopts 'g:e:b:d:t:ncr:u:plhs:w:akvoi' OPTION; do while getopts 'g:e:b:d:t:ncr:u:plhs:avo' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND} # echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in case "$OPTION" in
e) e)
@ -69,14 +68,6 @@ while getopts 'g:e:b:d:t:ncr:u:plhs:w:akvoi' OPTION; do
# automated - script is to be run without prompt (non-interactive) # automated - script is to be run without prompt (non-interactive)
no_prompt=true no_prompt=true
;; ;;
k)
# keep the build scripts in the image, by default they are removed
KEEP=true
;;
w)
# the work directory to put the build scripts in container (default is /opt/build)
BUILD_DIR=$OPTARG
;;
b) b)
# CUSTOM BASE IMAGE # CUSTOM BASE IMAGE
BASE_IMAGE=$OPTARG BASE_IMAGE=$OPTARG
@ -192,25 +183,9 @@ LINUX_DISTRO=$(docker_image_distro $BASE_IMAGE)
BASE_IMAGE=$LINUX_DISTRO BASE_IMAGE=$LINUX_DISTRO
fi fi
if [[ $1 ]]; then IMAGE_NAME=$(make_image_name $@)
[[ $NAME ]] && echo changing image name from $NAME to $1
NAME=$1
fi
if [[ $2 ]]; then
[[ $RUSER ]] && echo changing image username from $RUSER to $2
RUSER=$2
fi
if [[ $NAME ]]; then
[[ $append_distro ]] && NAME=$NAME-${LINUX_DISTRO}
else
echo no image name supplied using distro name $LINUX_DISTRO
NAME=${LINUX_DISTRO}
fi
IMAGE_NAME=$([[ $RUSER ]] && echo ${RUSER}/)${NAME}
# TODO writing to existing tag untags existing image so write a new tag to that image then continue # TODO writing to existing tag untags existing image so write a new tag to that image then continue
# retag existing image and remove former tag # retag existing image and remove former tag
if [[ $(image_exists $IMAGE_NAME) ]]; then if [[ $(image_exists $IMAGE_NAME) ]]; then
if [[ $overwrite ]]; then if [[ $overwrite ]]; then
@ -223,16 +198,13 @@ if [[ $(image_exists $IMAGE_NAME) ]]; then
fi fi
fi fi
# BASE_IMAGE=$([[ $BASE_IMAGE == *:* ]] && echo $BASE_IMAGE || echo $BASE_IMAGE:latest)
#$([[ ! $BASE_IMAGE == *:* ]] && echo :latest)
ARCH=$(get_arch) ARCH=$(get_arch)
export BASE_IMAGE export BASE_IMAGE
export TAG export TAG
export IMAGE_NAME export IMAGE_NAME
export LINUX_DISTRO export LINUX_DISTRO
export BUILD_DIR export BUILD_SRC
export KEEP export KEEP
export SYSADMIN_PW export SYSADMIN_PW
export ARCH export ARCH

View File

@ -5,25 +5,18 @@ variable "TAG" {
variable "LINUX_DISTRO" { variable "LINUX_DISTRO" {
// default = "alpine" // default = "alpine"
} }
variable "BUILD_DIR" {
default = ""
}
variable "IMAGE_NAME" { variable "IMAGE_NAME" {
// default = "alpine" // default = "alpine"
} }
variable "BASE_IMAGE" { variable "BASE_IMAGE" {
// default = "alpine" // default = "alpine"
} }
variable "KEEP" {
default = ""
}
variable "SYSADMIN_PW" { variable "SYSADMIN_PW" {
default = "" default = ""
} }
variable "ARCH" { variable "ARCH" {
default = "" default = ""
} }
function "tag" { function "tag" {
params = [suffix] params = [suffix]
result = [format("${IMAGE_NAME}%s:${TAG}", notequal("", suffix) ? "-${suffix}" : "")] result = [format("${IMAGE_NAME}%s:${TAG}", notequal("", suffix) ? "-${suffix}" : "")]
@ -54,8 +47,6 @@ target "amd64" {
LINUX_DISTRO = "${LINUX_DISTRO}" LINUX_DISTRO = "${LINUX_DISTRO}"
BASE_IMAGE = "${BASE_IMAGE}" BASE_IMAGE = "${BASE_IMAGE}"
TAG = "${TAG}" TAG = "${TAG}"
SCRIPTS = "${BUILD_DIR}"
KEEP = "${KEEP}"
SYSADMIN_PW = "${SYSADMIN_PW}" SYSADMIN_PW = "${SYSADMIN_PW}"
} }
tags = tag("") tags = tag("")

View File

@ -9,21 +9,24 @@
try_container () { try_container () {
declare -A arch=( ["x86_64"]="" ["aarch64"]="-arm64") declare -A arch=( ["x86_64"]="" ["aarch64"]="-arm64")
local mp;local cuser; local hmp; local bind; local vname; local prod; local priv local mp;local cuser; local hmp; local vname; local prod; local priv
local dkpath; local hostmp; local evar; local hostmap; local cleanup; local efile local hostmp; local efile; local entrypoint; local evar
local envf; local image; local append_distro; local options
[[ $# -lt 1 ]] && echo "image name required to try" && return 1
declare OPTION; declare OPTARG; declare OPTIND declare OPTION; declare OPTARG; declare OPTIND
OPTIND=0 OPTIND=0
while getopts 'f:o:dpr:t:u:m:h:ke:s:' OPTION; do while getopts 'lf:o:dpr:t:u:m:h:kv:e:s:' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND} # echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in case "$OPTION" in
s) s)
entrypoint="--entrypoint $OPTARG" entrypoint="--entrypoint $OPTARG"
;; ;;
e)
if ! source_env_file $OPTARG; then return 2; fi
efile=true
;;
f) f)
efile"--env-file $OPTARG" envf="--env-file $OPTARG"
;; ;;
d) d)
dryrun="echo " dryrun="echo "
@ -31,6 +34,10 @@ try_container () {
u) u)
cuser=$OPTARG cuser=$OPTARG
;; ;;
l)
# append distro name to image name
append_distro=true
;;
k) k)
keep=true keep=true
;; ;;
@ -46,7 +53,7 @@ try_container () {
h) h)
hmp=$OPTARG hmp=$OPTARG
;; ;;
e) v)
evar="-e $OPTARG" evar="-e $OPTARG"
;; ;;
t) t)
@ -67,11 +74,13 @@ try_container () {
shift $((OPTIND - 1)) shift $((OPTIND - 1))
[[ ! $1 ]] && echo must supply an image to try && return 1 [[ ! $efile ]] && source_env_file
# user=${2:-$ruser} image=$(make_image_name $@)
image=$1
shift 1 echo image name to try: $image
[[ ! $image ]] && echo must supply an image to try && return 1
if [[ $prod ]]; then if [[ $prod ]]; then
echo removing any local copy of image $image echo removing any local copy of image $image
@ -110,10 +119,9 @@ try_container () {
echo starting container with image: $image, and name $name echo starting container with image: $image, and name $name
echo at container prompt type \'exit\' to exit from shell and remove trial container echo at container prompt type \'exit\' to exit from shell and remove trial container
# --entrypoint /opt/scripts/entrypoint.sh \ # --entrypoint /opt/scripts/entrypoint.sh \
$dryrun docker run -i -t --rm $priv $evar $hostmap $options \ $dryrun docker run -i -t --rm $priv $evar $options \
${entrypoint} ${efile} \ ${entrypoint} ${evnf} \
$([[ $cuser ]] && echo "--user $cuser") \ $([[ $cuser ]] && echo "--user $cuser") \
--name try-$name --hostname try-$host-$name \ --name try-$name --hostname try-$host-$name \
$([[ $mp ]] && echo "-v $vname:/$mp") \ $([[ $mp ]] && echo "-v $vname:/$mp") \

View File

@ -56,6 +56,7 @@ env_file () {
} }
read_env_file() { read_env_file() {
local evar
while read line; do while read line; do
evar=$(echo $line | cut -d '=' -f1) evar=$(echo $line | cut -d '=' -f1)
if [[ ! ${!evar} ]]; then if [[ ! ${!evar} ]]; then
@ -74,7 +75,7 @@ read_env_file() {
} }
source_env_file () { source_env_file () {
local default local default; local efile
if efile=$(env_file $1); then if efile=$(env_file $1); then
[[ $efile == ".env" ]] && default=true [[ $efile == ".env" ]] && default=true
[[ ! $(isAbsPath $efile) ]] && efile=$(realpath $efile) [[ ! $(isAbsPath $efile) ]] && efile=$(realpath $efile)
@ -114,4 +115,28 @@ echo $(cat $temp | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|ce
rm $temp rm $temp
} }
(return 0 2>/dev/null) || docker_image_distro $@ make_image_name () {
# echo making image name $@
# echo name: $NAME
# echo user: $RUSER
# echo distro $LINUX_DISTRO
if [[ $1 ]]; then
# [[ $NAME ]] && echo changing image name from $NAME to $1
NAME=$1
fi
if [[ $2 ]]; then
# [[ $RUSER ]] && echo changing image username from $RUSER to $2
RUSER=$2
fi
if [[ $NAME ]]; then
[[ $append_distro ]] && NAME=$NAME-${LINUX_DISTRO}
else
# echo no image name supplied using distro name ${LINUX_DISTRO:-alpine}
NAME=${LINUX_DISTRO:-alpine}
fi
echo $([[ $RUSER ]] && echo ${RUSER}/)${NAME}
}

View File

@ -7,3 +7,4 @@ for f in $libdir/cmds/*; do
source "$f" source "$f"
done done

View File

@ -1,11 +1,8 @@
#!/bin/bash #!/bin/bash
# echo arguments in start script: "$@" # echo arguments in start script: "$@"
source $SCRIPTS_DIR/container.env source /opt/env/run.env
source $SCRIPTS_DIR/host-id-map.sh source /opt/bin/host-id-map.sh
case "$1" in case "$1" in
shell_update)
echo updating shell repos
;;
cmd) cmd)
shift 1 shift 1
"$@" "$@"
@ -13,7 +10,7 @@ case "$1" in
script) script)
shift 1 shift 1
module_load path module_load path
script=$([[ $(isAbsPath $1) ]] && echo $1 || echo $SCRIPTS_DIR/$1) script=$([[ $(isAbsPath $1) ]] && echo $1 || echo opt/bin/$1)
shift 1 shift 1
/bin/bash $script "$@" /bin/bash $script "$@"
;; ;;

1
src/init/bin/entrypoint Symbolic link
View File

@ -0,0 +1 @@
base-entrypoint

View File

@ -0,0 +1,7 @@
#!/bin/bash
if [[ $HOST_MAP ]]; then
echo changing ownership of $VOLUME_DIRS to $HOST_MAP
declare usesudo
[[ ! $EUID -eq 0 ]] && usesudo=sudo
$usesudo chown -R $HOST_MAP $VOLUME_DIRS
fi

1
src/init/bin/start Symbolic link
View File

@ -0,0 +1 @@
base-start

View File

@ -20,14 +20,12 @@ export SHELL=/bin/bash
mkdir -p /home/sysadmin/shell mkdir -p /home/sysadmin/shell
/bin/bash /shell/base/install/install.sh sysadmin /bin/bash /shell/base/install/install.sh sysadmin
echo "******** uci shell install complete **********" echo "******** uci shell install complete **********"
/bin/bash -l -c "module_load dir; dir_copy scripts /opt"
echo installed scripts in /opt/scripts
ls -la /opt/scripts
source /opt/scripts/container.env
# make essential directories # make essential directories
mkdir -p $SHARED_DIRS /opt/scripts /opt/bin mkdir -p $VOLUME_DIRS /opt/bin
chown -R host:host /home/sysadmin/shell $SHARED_DIRS /opt/scripts /opt/bin chown -R :host /home/sysadmin/shell
chmod -R g+rw /home/sysadmin/shell $SHARED_DIRS /opt/scripts /opt/bin chown -R host:host $VOLUME_DIRS
chmod -R g+rw /home/sysadmin/shell $VOLUME_DIRS
ls -la /home/sysadmin/shell $VOLUME_DIRS
echo installing and running image distro info echo installing and running image distro info
/bin/bash install-info.sh /bin/bash install-os-info.sh
/opt/scripts/info /opt/bin/os-info

View File

@ -1,8 +1,7 @@
#!/bin/bash #!/bin/bash
wget --quiet -O /opt/scripts/info https://git.io/vaHfR > /dev/null wget --quiet -O /opt/bin/os-info https://git.io/vaHfR > /dev/null
if [[ -f /opt/scripts/info ]]; then if [[ -f /opt/bin/os-info ]]; then
chmod +x /opt/scripts/info chmod +x /opt/bin/os-info
ln -sf /opt/scripts/info /opt/bin/
else else
echo Failed to download echo Failed to download
echo "https://raw.githubusercontent.com/KittyKatt/screenFetch/master/screenfetch-dev" echo "https://raw.githubusercontent.com/KittyKatt/screenFetch/master/screenfetch-dev"

View File

@ -1,2 +0,0 @@
export SHARED_DIRS="/shell /opt /data"
export INITIAL_DIR=/opt/scripts

View File

@ -1,6 +0,0 @@
#!/bin/bash
source $SCRIPTS_DIR/container.env
if [[ $HOST_MAP ]]; then
echo changing ownership of $SHARED_DIRS to $HOST_MAP
$([[ ! $EUID -eq 0 ]] && echo sudo) chown -R $HOST_MAP $SHARED_DIRS
fi

4
src/init/env/build.env vendored Normal file
View File

@ -0,0 +1,4 @@
# anything in here will be sourced during build
# allows easy custom environment variables
source /opt/env/run.env
# export SOMEVAR="something"

3
src/init/env/run.env vendored Normal file
View File

@ -0,0 +1,3 @@
# added /opt/run.env to the end of /etc/profile
export VOLUME_DIRS="/shell /opt /data"
export INITIAL_DIR=/opt

View File

@ -1,5 +1,5 @@
#!/bin/sh #!/bin/sh
echo "entry init.sh script in $BUILD_DIR" echo "running base init.sh script in /build"
# remove other distro files # remove other distro files
# find $PWD -maxdepth 1 -type d ! -path $PWD ! -name ${LINUX_DISTRO} ! -name common -exec rm -rf {} + # find $PWD -maxdepth 1 -type d ! -path $PWD ! -name ${LINUX_DISTRO} ! -name common -exec rm -rf {} +
cd ${LINUX_DISTRO} || exit 1 cd ${LINUX_DISTRO} || exit 1
@ -19,8 +19,3 @@
echo returned from ${LINUX_DISTRO} post common script echo returned from ${LINUX_DISTRO} post common script
fi fi
cd .. cd ..
if [ -z $KEEP ]; then
echo removing $BUILD_DIR directory used for build
cd /opt && rm -rf $BUILD_DIR
fi

View File

@ -3,4 +3,5 @@ curl
git git
rsync rsync
sudo sudo
nano nano
coreutils