Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 46 additions & 13 deletions _common/docker.inc.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Common docker functions.
# Common container functions.

function get_docker_image_id() {
local IMAGE_NAME=$1
echo "$(docker images -q $IMAGE_NAME)"
echo "$($BUILDER_ENGINE images -q $IMAGE_NAME)"
}

function get_docker_container_id() {
local CONTAINER_NAME=$1
echo "$(docker ps -a -q --filter ancestor=$CONTAINER_NAME)"
echo "$($BUILDER_ENGINE ps -a -q --filter ancestor=$CONTAINER_NAME)"
}

function stop_docker_container() {
Expand All @@ -16,7 +16,7 @@ function stop_docker_container() {

local CONTAINER_ID=$(get_docker_container_id $CONTAINER_NAME)
if [ ! -z "$CONTAINER_ID" ]; then
docker container stop $CONTAINER_ID
$BUILDER_ENGINE container stop $CONTAINER_ID
else
builder_echo "No Docker container to stop"
fi
Expand All @@ -31,14 +31,14 @@ function clean_docker_container() {

local CONTAINER_ID=$(get_docker_container_id $CONTAINER_NAME)
if [ ! -z "$CONTAINER_ID" ]; then
docker container rm $CONTAINER_ID
$BUILDER_ENGINE container rm $CONTAINER_ID
else
echo "No Docker container to clean"
fi

local IMAGE_ID=$(get_docker_image_id $IMAGE_NAME)
if [ ! -z "$IMAGE_ID" ]; then
docker rmi $IMAGE_NAME
$BUILDER_ENGINE rmi $IMAGE_NAME
else
echo "No Docker image to clean"
fi
Expand All @@ -54,21 +54,26 @@ function build_docker_container() {
local IMAGE_NAME=$1
local CONTAINER_NAME=$2
local BUILDER_CONFIGURATION="release"
local FILE=
local TARGET=.
echo $CONTAINER_ENGINE
Comment thread
Linsner marked this conversation as resolved.
Outdated
if [[ $# -ge 3 ]]; then
BUILDER_CONFIGURATION=$3
fi
if [[ $# -ge 4 ]]; then
TARGET="-f $4 ."
FILE="-f $4 "
Comment thread
Linsner marked this conversation as resolved.
elif [[ "${CONTAINER_ENGINE}" == "docker" ]]; then
FILE="-f Dockerfile "
elif [[ "${CONTAINER_ENGINE}" == "podman" ]]; then
FILE="-f Podmanfile "
Comment on lines +64 to +67

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these really needed? Won't podman use Podmanfile by default just as docker uses Dockerfile by default?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Podman uses "Dockerfile" for default to be compatible. Usually you can run Podman with the same file as Docker uses but the Dockerfile must address specific requirements of Podman e.g. full path to images.

If you like you can test the build on Docker with the Podmanfile, it should work.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if we can get to the point where Podman and Docker can use the same file, that will definitely be better for maintenance. The differences seem pretty minimal at present, so can we get to a point where we can merge them?

@Linsner Linsner Jun 28, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, absolutely. But even if Docker works perfectly with the slightly modified version, I probably would stick to the current solution of the script. Just to have the ability to switch the files. For all projects where we are sure that we can use the same file, I would could create a symbolic link instead.

In case of the Dockerfile for help.keyman.com I think the switch user statement in ln. 18 is an error and will break the build in Docker, too. (Take note of the install command afterwards that will not run with the user "www-data".) But I didn't want to change anything on the existing files without the ability to test it.

fi

_verify_vendor_is_not_folder

builder_echo "Building using $BUILDER_CONFIGURATION configuration"


# Download docker image. --mount option requires BuildKit
DOCKER_BUILDKIT=1 docker build -t $IMAGE_NAME --build-arg BUILDER_CONFIGURATION="${BUILDER_CONFIGURATION}" $TARGET
DOCKER_BUILDKIT=1 $BUILDER_ENGINE build -t $IMAGE_NAME --build-arg BUILDER_CONFIGURATION="${BUILDER_CONFIGURATION}" $FILE $TARGET
}

function start_docker_container() {
Expand Down Expand Up @@ -122,7 +127,7 @@ function start_docker_container() {
ADD_HOST="--add-host host.docker.internal:host-gateway"
fi

docker run --rm -d -p $PORT:80 -v "${DOCKER_BINDING}" \
$BUILDER_ENGINE run --rm -d -p $PORT:80 -v "${DOCKER_BINDING}" \
-e S_KEYMAN_COM=localhost:$PORT_S_KEYMAN_COM \
-e API_KEYMAN_COM=localhost:$PORT_API_KEYMAN_COM \
--name $CONTAINER_DESC \
Expand All @@ -137,7 +142,7 @@ function start_docker_container() {
builder_die "Docker container appears to have failed to start in order to create link to vendor/"
fi

docker exec -i $CONTAINER_ID sh -c "ln -s /var/www/vendor vendor && chown -R www-data:www-data vendor"
$BUILDER_ENGINE exec -i $CONTAINER_ID sh -c "ln -s /var/www/vendor vendor && chown -R www-data:www-data vendor"
fi

# after starting container, we want to run an init script if it is present
Expand All @@ -149,8 +154,36 @@ function start_docker_container() {

cmd="./resources/init-container.sh ${BUILDER_CONFIGURATION}"
builder_echo green "cmd is ${cmd}"
docker exec -i $CONTAINER_ID sh -c "${cmd}"
$BUILDER_ENGINE exec -i $CONTAINER_ID sh -c "${cmd}"
fi

builder_echo green "Listening on http://$HOST:$PORT"
}
}

# Returns 0 if the specified container engine is available, 1 otherwise
_is_container_engine() {
local engine="$1"
if command -v "$engine" >/dev/null 2>&1; then
return 0
else
return 1
fi
}

_get_container_engine() {
export BUILDER_ENGINE
if _is_container_engine "docker"; then
BUILDER_ENGINE="docker"
elif _is_container_engine "podman"; then
BUILDER_ENGINE="podman"
else
builder_die "No supported container engine found. Please install Docker or Podman to run containerized builds."
fi
}

################################################################################
# Final initialization
################################################################################

_get_container_engine
builder_echo "Using container engine: $BUILDER_ENGINE"