feat: write auto-detected port mappings during a deploy
During an app build, we now auto-detect ports based on the source code. This is usually http:80:5000, with Dockerfile-based deploys having their ports extracted from the docker image or Dockerfile. Additionally, we add an https:443 mapping for any detected http:80 mapping when there is an ssl certificate, and all http port mappings are transformed to https mappings for Dockerfile-based deploys. While the ports aren't currently consumed, a future refactor will provide the ability to fallback to the new detected ports when there is no user-specified port mapping.
This commit is contained in:
@@ -41,6 +41,10 @@ trigger-builder-dockerfile-builder-build() {
|
||||
eval "$(config_export app "$APP")"
|
||||
"$DOCKER_BIN" image build "${DOCKER_BUILD_LABEL_ARGS[@]}" $DOKKU_GLOBAL_BUILD_ARGS "${ARG_ARRAY[@]}" ${DOKKU_DOCKER_BUILD_OPTS} -t $IMAGE .
|
||||
|
||||
set -x
|
||||
fn-builder-dockerfile-get-detect-port-map "$APP" "$IMAGE" "$SOURCECODE_WORK_DIR/Dockerfile"
|
||||
plugn trigger ports-set-detected "$APP" "$(fn-builder-dockerfile-get-detect-port-map "$APP" "$IMAGE" "$SOURCECODE_WORK_DIR/Dockerfile")"
|
||||
set +x
|
||||
plugn trigger post-build-dockerfile "$APP"
|
||||
}
|
||||
|
||||
|
||||
@@ -88,3 +88,47 @@ fn-builder-dockerfile-dockerfile-path() {
|
||||
|
||||
fn-plugin-property-get-default "builder-dockerfile" "$APP" "dockerfile-path" ""
|
||||
}
|
||||
|
||||
fn-builder-dockerfile-get-ports-from-dockerfile() {
|
||||
declare desc="return all exposed ports from passed file path"
|
||||
declare DOCKERFILE_PATH="$1"
|
||||
|
||||
suppress_output dos2unix "$DOCKERFILE_PATH"
|
||||
local ports="$(grep -E "^EXPOSE " "$DOCKERFILE_PATH" | awk '{ print $2 }' | xargs)" || true
|
||||
echo "$ports"
|
||||
}
|
||||
|
||||
fn-builder-dockerfile-get-ports-from-image() {
|
||||
declare desc="return all exposed ports from passed image name"
|
||||
declare IMAGE="$1"
|
||||
|
||||
local ports="$("$DOCKER_BIN" image inspect --format "{{range $key, $value := .Config.ExposedPorts}}{{$key}} {{end}}" "$IMAGE" | xargs)" || true
|
||||
echo "$ports"
|
||||
}
|
||||
|
||||
fn-builder-dockerfile-get-detect-port-map() {
|
||||
declare desc="extracts and echos a port mapping from the app"
|
||||
declare APP="$1" IMAGE="$2" DOCKERFILE_PATH="$3"
|
||||
|
||||
local detected_ports=$(fn-builder-dockerfile-get-ports-from-dockerfile "$DOCKERFILE_PATH")
|
||||
|
||||
if [[ -z "$detected_ports" ]]; then
|
||||
local detected_ports=$(fn-builder-dockerfile-get-ports-from-image "$IMAGE")
|
||||
fi
|
||||
|
||||
if [[ -n "$detected_ports" ]]; then
|
||||
local port_map=""
|
||||
for p in $detected_ports; do
|
||||
if [[ "$p" =~ .*udp.* ]]; then
|
||||
p=${p//\/udp/}
|
||||
port_map+="udp:$p:$p "
|
||||
else
|
||||
p=${p//\/tcp/}
|
||||
port_map+="http:$p:$p "
|
||||
fi
|
||||
done
|
||||
echo "$port_map" | xargs
|
||||
else
|
||||
echo "http:80:5000"
|
||||
fi
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user