Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SC-XXX: Improved speed of docker image building #449

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8e8689b
Improved speed of docker image building
sprymiker Jun 28, 2023
252ff73
Moved dockerfiles to match current structure and keep git history as …
sprymiker Jun 28, 2023
6e1a6e0
Adjusted default progress type to auto for more compatibility
sprymiker Jun 28, 2023
1c9721c
Replaced dependency on `ip` package in linux to use more common packages
sprymiker Jun 28, 2023
fcecee2
Fixed wrong path appeared after merging with master
sprymiker Jun 28, 2023
59d5a9f
Adjusted bake configuration by separating targets in export process a…
sprymiker Jun 29, 2023
c0bc1ba
- Added fallback for cases when buildx docker plugin is not installed
sprymiker Jun 29, 2023
8c4fe3c
- Reduced requirement for buildx version as it is confirmed working o…
sprymiker Jun 29, 2023
ba082cb
- Fixed mount cache permissions and wrong id
sprymiker Jun 30, 2023
a69258f
Fixed git cleanup command (mostly for nonsplit)
sprymiker Jun 30, 2023
31715fb
Fixed git cleanup command (mostly for nonsplit). Part 2
sprymiker Jun 30, 2023
1787de8
Deep speed, number of layers and image size optimizations. Restructur…
sprymiker Jul 2, 2023
f833e16
Speeding up dev composer run
sprymiker Jul 2, 2023
a418643
Fixed assets build process that does not build
sprymiker Jul 2, 2023
728d8dd
Excluding tests/dd.php to be skipped for web app images
sprymiker Jul 2, 2023
c8dcc4b
Reduced number of layers in web service images
sprymiker Jul 3, 2023
ff820d6
Fixed autoload issue with spryker/spryker
sprymiker Jul 3, 2023
1ec23b4
Reduced pipeline image by excluding git folders in vendor, but keepin…
sprymiker Jul 3, 2023
5e7cb85
Fixed resolver cache for cli/pipeline
sprymiker Jul 3, 2023
293a8fd
Fixed cli and scheduler does not work properly
sprymiker Jul 3, 2023
a91406d
Fixed permission issue in cli
sprymiker Jul 3, 2023
d321fb0
Optimized pipeline build speed by reusing application build results
sprymiker Jul 3, 2023
2f22727
Optimized pipeline build speed by reusing application build results […
sprymiker Jul 3, 2023
8d7a792
Fixed lack of files in root folder for FE build in suite-nonsplit (an…
sprymiker Jul 4, 2023
dd734ce
Improved docker build structure and hopefully speed be using --mount=…
sprymiker Jul 4, 2023
a60b3fd
Refactored Dockerfiles to use newest build features
sprymiker Aug 3, 2023
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
1 change: 1 addition & 0 deletions .dockerignore.default
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.git*
**/.git
.idea
.DS_Store
node_modules
Expand Down
9 changes: 3 additions & 6 deletions bin/command/build/baked/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ function Command::build() {
subCommand=${1}
case ${subCommand} in
images | image)
Images::buildApplication --force
Assets::build
Images::buildFrontend --force
Images::build --force
;;
assets | asset)
Images::build --force
Assets::build --force
Images::buildFrontend --force
;;
'')
Images::buildApplication --force
Images::build --force
Assets::build --force
Images::buildFrontend --force
;;
*)
Console::error "Unknown build target '${subCommand}' is occurred. No action." >&2
Expand Down
48 changes: 18 additions & 30 deletions bin/command/build/baked/export.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Registry::addCommand "export" "Command::export"

Registry::Help::command -c "export images" -a "[-t <tag>]" "Builds prod-like images (Yves, Zed, Glue, Frontend)."
Registry::Help::command -c "export images" -a "[-t <tag>] [-d ecr]" "Builds prod-like images (Yves, Zed, Glue, Frontend)."
Registry::Help::command -c "export assets" -a "[-t <tag>] [-p <path>]" "[DEPRECATED] Builds assets and export as archives stored by given path."

function _assertDestinationDirectory() {
Expand All @@ -13,10 +13,12 @@ function _assertDestinationDirectory() {
}

function Command::export() {
local OPTIND=0
local opt
local subCommand=''
local tag=${SPRYKER_DOCKER_TAG}
local destinationPath='./'
local pushDestination=''
local pushDestination='print'

subCommand=${1}
shift || true
Expand All @@ -32,14 +34,6 @@ function Command::export() {
;;
d)
pushDestination=${OPTARG}
local pushDestinationPath="sdk/images/baked/${pushDestination}.sh"
local pathToFile="${DEPLOYMENT_PATH}/bin/${pushDestinationPath}"
if [ ! -f "${pathToFile}" ]; then
Console::error "\nUnknown export images destination - '${OPTARG}'."
exit 1
fi

import ${pushDestinationPath}
;;
# Unknown option specified
\?)
Expand All @@ -60,33 +54,27 @@ function Command::export() {
done
shift $((OPTIND - 1))

local pushDestinationPath="sdk/images/destination/${pushDestination}.sh"
if [ ! -f "${DEPLOYMENT_PATH}/bin/${pushDestinationPath}" ]; then
Console::error "\nUnknown export images destination - '${pushDestination}'."
exit 1
fi

import ${pushDestinationPath}

case ${subCommand} in
asset | assets)
Console::warn 'This command is DEPRECATED. Please, use just "export".'
Console::warn 'This command is DEPRECATED. Please, use just "export images".'
_assertDestinationDirectory "${destinationPath}"
Images::buildApplication --force
Assets::build --force
Images::buildFrontend --force
Assets::export "${tag}" "${destinationPath}"
Images::export "${tag}" "print"
Assets::export "${tag}" "print" "${destinationPath}"
;;
image | images)
Console::verbose "${INFO}Build and export images${NC}"
Images::buildApplication --force
Images::tagApplications "${tag}"
Assets::build --force
Images::buildFrontend --force
Images::tagFrontend "${tag}"

if [ -n "${pushDestination}" ]; then
Images::push "${tag}"
fi

if [ -z "${pushDestination}" ]; then
Images::printAll "${tag}"
fi
Images::export "${tag}" "${pushDestination}"
Images::print "${tag}" "${pushDestination}"
;;
*)
Console::error "Unknown export '${subCommand}' is occurred. No action. Usage: ${HELP_SCR}${SELF_SCRIPT} export images [-t <tag>]" >&2
Console::error "Unknown export '${subCommand}' is occurred. No action. Usage: ${HELP_SCR}${SELF_SCRIPT} export images [-t <tag>] [-d ecr]" >&2
exit 1
;;
esac
Expand Down
9 changes: 2 additions & 7 deletions bin/command/build/mount/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,18 @@ function Command::build() {
subCommand=${1}
case ${subCommand} in
images | image)
Images::buildApplication --force
Codebase::build
Assets::build
Images::buildFrontend --force
Images::build --force
;;
codebase | code)
Codebase::build --force
;;
assets | asset)
Assets::build --force
Images::buildFrontend --force
;;
'')
Images::buildApplication --force
Images::build --force
Codebase::build --force
Assets::build --force
Images::buildFrontend --force
;;
*)
Console::error "Unknown build target '${subCommand}' is occurred. No action." >&2
Expand Down
9 changes: 7 additions & 2 deletions bin/command/install/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Registry::Help::command -s -c "bootstrap | boot" -a "[-v] <project-yml-file>" "P
Registry::Help::command -s -c "bootstrap | boot" -a "[-v]" "Prepares all the files to run the application based on ${HELP_HIGH}deploy.local.yml${HELP_DESC} or ${HELP_HIGH}deploy.yml${HELP_DESC}."

function Command::bootstrap() {
local OPTIND=0
local opt

while getopts ":vsx" opt; do
case ${opt} in
Expand Down Expand Up @@ -42,6 +44,7 @@ function Command::bootstrap() {
shift $((OPTIND - 1))

local gitHash=$(git rev-parse --verify HEAD 2>/dev/null || true)
local SPRYKER_SDK_REVISION="$(cd ${SOURCE_DIR} && git rev-parse --short HEAD 2>/dev/null || echo '')"
local tmpDeploymentDir="${SOURCE_DIR}/deployment/_tmp"
local defaultProjectYaml=$([ -f "./deploy.local.yml" ] && echo -n "./deploy.local.yml" || echo -n "./deploy.yml")
local projectYaml=${1:-${defaultProjectYaml}}
Expand Down Expand Up @@ -75,11 +78,12 @@ function Command::bootstrap() {
Console::info "Using ${projectYaml}"

local USER_FULL_ID=$(Environment::getFullUserId)
local USER_UID="${USER_FULL_ID%%:*}"

Console::verbose::start "Building generator..."
docker build -t spryker_docker_sdk \
-f "${SOURCE_DIR}/generator/Dockerfile" \
--build-arg="USER_UID=${USER_FULL_ID%%:*}" \
--build-arg="USER_UID=${USER_UID}" \
-q \
"${SOURCE_DIR}/generator" >/dev/null
Console::end "[DONE]"
Expand All @@ -103,12 +107,13 @@ function Command::bootstrap() {

# To support root user
local userToRun=("-u" "${USER_FULL_ID}")
if [ "${USER_FULL_ID%%:*}" != '0' ]; then
if [ "${USER_UID}" != '0' ]; then
userToRun=()
fi
docker run -i --rm "${userToRun[@]}" \
-e SPRYKER_PLATFORM_IMAGE="${SPRYKER_PLATFORM_IMAGE:-""}" \
-e SPRYKER_DOCKER_SDK_PLATFORM="${_PLATFORM}" \
-e SPRYKER_SDK_REVISION="${SPRYKER_SDK_REVISION}" \
-e SPRYKER_DOCKER_SDK_DEPLOYMENT_DIR="${DESTINATION_DIR}" \
-e VERBOSE="${VERBOSE}" \
-v "${tmpDeploymentDir}":/data/deployment:rw \
Expand Down
5 changes: 3 additions & 2 deletions bin/command/install/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ function Command::config() {
Console::info "Using ${projectYaml}"

local USER_FULL_ID=$(Environment::getFullUserId)
local USER_UID="${USER_FULL_ID%%:*}"

Console::verbose::start "Building generator..."
docker build -t spryker_docker_sdk \
-f "${SOURCE_DIR}/generator/deploy-file-generator/Dockerfile" \
--build-arg="USER_UID=${USER_FULL_ID%%:*}" \
--build-arg="USER_UID=${USER_UID}" \
-q \
"${SOURCE_DIR}/generator" >/dev/null

Expand All @@ -42,7 +43,7 @@ function Command::config() {

# To support root user
local userToRun=("-u" "${USER_FULL_ID}")
if [ "${USER_FULL_ID%%:*}" != '0' ]; then
if [ "${USER_UID}" != '0' ]; then
userToRun=()
fi
docker run -i --rm "${userToRun[@]}" \
Expand Down
8 changes: 6 additions & 2 deletions bin/environment/docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
export DOCKER_BUILDKIT=1

require docker tr
require:linux ip grep awk
require:linux grep awk
require:macos ipconfig
require:windows tail cut

Expand Down Expand Up @@ -87,7 +87,11 @@ function Environment::getHostIp() {
case ${_PLATFORM} in
linux)
if ! Environment::isWSL; then
myIp=$(ip route get 1 | sed 's/^.*src \([^ ]*\).*$/\1/;q')
if command -v ip &> /dev/null; then
myIp=$(ip route get 1 | sed 's/^.*src \([^ ]*\).*$/\1/;q')
else
myIp=$(hostname -i | awk '{ print $1 }')
fi
fi
;;
macos)
Expand Down
1 change: 1 addition & 0 deletions bin/registry/help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function Registry::Help::separator() {

function Registry::Help::command() {
local OPTIND=0
local opt
local OPTARG=''
local OPTERR=''
local envs=''
Expand Down
75 changes: 21 additions & 54 deletions bin/sdk/assets/baked.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

function Assets::export() {
# deprecated
local tag=${1}
local destinationPath=${2%/}
local TARGET_TAG=${1:-${SPRYKER_DOCKER_TAG}}
local destination=${2}
local destinationPath=${3%/}

local builderAssetsImage=$(Assets::getImageTag)
local builderAssetsImage
local dockerAssetsTmpDirectory="/_tmp"
local projectDockerAssetsTmpDirectory=${DEPLOYMENT_DIR}${dockerAssetsTmpDirectory}

Expand All @@ -19,6 +20,18 @@ function Assets::export() {

Console::start "Preparing assets archives..."

source ${DEPLOYMENT_PATH}/images/export/${destination}.sh

local targetData
for targetData in "${TARGET_TAGS[@]}"; do
eval "${targetData}"

if [ "$TARGET" == 'frontend' ]; then
builderAssetsImage="${TAGS[0]}"
break
fi
done

# To support root user
local userToRun=("-u" "${USER_FULL_ID}")
if [ "${USER_FULL_ID%%:*}" != '0' ]; then
Expand All @@ -29,7 +42,7 @@ function Assets::export() {
-v "${DEPLOYMENT_DIR}/bin:/data/standalone" \
-v "${projectDockerAssetsTmpDirectory}:/data${dockerAssetsTmpDirectory}" \
--entrypoint='' \
--name="${SPRYKER_DOCKER_PREFIX}_builder_assets" \
--name="${SPRYKER_DOCKER_PREFIX}_frontend" \
"${builderAssetsImage}" \
sh -c "${command}" 2>&1

Expand All @@ -50,58 +63,12 @@ function Assets::export() {
rm -rf "${projectDockerAssetsTmpDirectory}"
}

function Assets::getImageTag() {
echo -n "${SPRYKER_DOCKER_PREFIX}_builder_assets:${SPRYKER_DOCKER_TAG}-${SPRYKER_REPOSITORY_HASH}"
}

function Assets::areBuilt() {
Console::start "Checking assets are built..."

local builderAssetsImage=$(Assets::getImageTag)

if docker image inspect "${builderAssetsImage}" >/dev/null 2>&1; then
Console::end "[BUILT]"
return "${TRUE}"
fi

return "${FALSE}"
# Do nothing as everything is built before. Could be changed once assets are separated
:
}

function Assets::build() {

local force=''
if [ "$1" == '--force' ]; then
force=1
shift || true
fi

if [ -z "${force}" ] && Assets::areBuilt; then
return "${TRUE}"
fi

Console::start "Cleaning old assets..."

# ${XARGS_NO_RUN_IF_EMPTY} must be without quotes
# shellcheck disable=SC2086
docker images --filter "reference=${SPRYKER_DOCKER_PREFIX}_builder_assets:${SPRYKER_DOCKER_TAG}*" --format "{{.ID}}" | xargs ${XARGS_NO_RUN_IF_EMPTY} docker rmi -f

Console::end "[DONE]"
Console::start "Building assets..."

local builderAssetsImage=$(Assets::getImageTag)
local cliImage="${SPRYKER_DOCKER_PREFIX}_cli:${SPRYKER_DOCKER_TAG}"
local mode=${SPRYKER_ASSETS_MODE:-development}

docker build \
-t "${builderAssetsImage}" \
-f "${DEPLOYMENT_PATH}/images/baked/assets/Dockerfile" \
--progress="${PROGRESS_TYPE}" \
--build-arg "SPRYKER_PARENT_IMAGE=${cliImage}" \
--build-arg "SPRYKER_ASSETS_MODE=${mode}" \
--build-arg "SPRYKER_PIPELINE=${SPRYKER_PIPELINE}" \
--build-arg "SPRYKER_BUILD_HASH=${SPRYKER_BUILD_HASH:-"current"}" \
--build-arg "SPRYKER_BUILD_STAMP=${SPRYKER_BUILD_STAMP:-""}" \
. 1>&2

Console::end "[DONE]"
# Do nothing as everything is built before. Could be changed once assets are separated
:
}
4 changes: 0 additions & 4 deletions bin/sdk/assets/mount.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ function Assets::export() {
return "${FALSE}"
}

function Assets::getImageTag() {
echo -n "${SPRYKER_DOCKER_PREFIX}_cli:${SPRYKER_DOCKER_TAG}"
}

function Assets::areBuilt() {
Console::start "Checking assets are built..."

Expand Down
3 changes: 1 addition & 2 deletions bin/sdk/compose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,9 @@ function Compose::up() {
Compose::cleanSourceDirectory
fi

Images::buildApplication ${noCache} ${doBuild}
Images::build ${noCache} ${doBuild}
Codebase::build ${noCache} ${doBuild}
Assets::build ${noCache} ${doAssets}
Images::buildFrontend ${noCache} ${doBuild}
Compose::run --build
Compose::command restart frontend gateway

Expand Down
Loading
Loading