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

[build] create a clean_docker_images profile #13067

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@ clean reset showtag docker-cleanup sonic-slave-build sonic-slave-bash :
# Freeze the versions, see more detail options: scripts/versions_manager.py freeze -h
freeze:
@scripts/versions_manager.py freeze $(FREEZE_VERSION_OPTIONS)

clean_docker_images:
@scripts/clean_docker_images.sh
41 changes: 41 additions & 0 deletions scripts/clean_docker_images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/sh

DOCKER_CMD=docker
if [ ! `which $DOCKER_CMD` ]
then echo "no docker command available" >&2
exit 1
fi
#if "docker ps" cannot be run without error, prepend sudo
if ( ! $DOCKER_CMD ps >/dev/null 2>&1 );then
echo "docker command only usable as root, using sudo" >&2
DOCKER_CMD="sudo docker"
fi

cd $(dirname $0)
cd ..
IMAGES_LIST=$($DOCKER_CMD image ls "sonic*" | sed 's/ */\t/g' | cut -f3 | tail -n +2)

if [ -z $IMAGES_LIST ]; then
echo "No docker image to delete"
exit 0
fi

echo -n "Run '$DOCKER_CMD image rm -f $(echo $IMAGES_LIST) ' (y/n)?"
initial_stty_cfg=$(stty -g)
stty raw -echo
answer=$( while ! head -c 1 | grep -i '[yn]' ;do true ;done )
stty $initial_stty_cfg
if echo "$answer" | grep -iq "^y" ;then
$DOCKER_CMD image rm -f $(echo $IMAGES_LIST)
else
echo "Nothing done"
exit 0
fi
echo -n "Run '$DOCKER_CMD image prune -af' (y/n)?"
initial_stty_cfg=$(stty -g)
stty raw -echo
answer=$( while ! head -c 1 | grep -i '[yn]' ;do true ;done )
stty $initial_stty_cfg
if echo "$answer" | grep -iq "^y" ;then
$DOCKER_CMD image prune -af
fi