├── tmp └── .git_keeper ├── .gitignore ├── docker_retag.sh ├── docker_regen.sh ├── docker_reclaim.sh ├── LICENSE ├── README.md └── _regen_qcow2.sh /tmp/.git_keeper: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /docker_retag.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # docker retag your images 4 | # eg. docker tag 2603180bbc4a ubuntu:16.04 5 | #docker tag 96da9143fb18 ubuntu:16.04 -------------------------------------------------------------------------------- /docker_regen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export TMP_DIR=$(pwd)/tmp 4 | echo running ./_regen_qcow2.sh `docker images | awk '{print $3}' | uniq | sed "1 d" | paste -sd " " -` 5 | ./_regen_qcow2.sh `docker images | awk '{print $3}' | uniq | sed "1 d" | paste -sd " " -` -------------------------------------------------------------------------------- /docker_reclaim.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -x 4 | 5 | docker system df -v 6 | 7 | docker system prune 8 | 9 | docker run --rm --privileged --pid=host docker/desktop-reclaim-space 10 | 11 | docker run --rm -it --privileged --pid=host walkerlee/nsenter -t 1 -m -u -i -n fstrim /var/lib/docker 12 | 13 | ls -klsh ~/Library/Containers/com.docker.docker/Data/vms/0/Docker.qcow2 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 wanliqun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # macos_docker_toolkit 2 | mac os toolkit 3 | 4 | As a developer, I use docker to do my development work a lot on my Mac Air laptop. It's quite annoying that once I pulled some docker images and kicked off some docker containers, the consuming disk space keeps growing even after I try to delete some files to free some space. 5 | 6 | So I googled around, then based on the following references, I write some shell scripts to work it out. It did work for me, and I hope it work for you too. 7 | - [(David Scott's Blog) Docker for Mac: reducing disk space](https://djs55.github.io/jekyll/update/2017/11/27/docker-for-mac-disk-space.html)) 8 | - [(StackOverflow) Docker is filling up my disk space](https://stackoverflow.com/questions/39878939/docker-is-filling-up-my-disk-space) 9 | 10 | ## **docker_regen.sh** 11 | This script will backup your docker images to tar archive files, then delete docker's single writable disk image`Docker.raw` (or `Docker.qcow2`), and finally restore the docker images from the previous backup files (and optionally remove the backup files). 12 | You can change the docker preference settings to adjust the maximum docker image size during the process. 13 | 14 | ## **docker_reclaim.sh** 15 | This script will do a docker system prune, then trim & reclaim the docker disk image size, and output the pre-reclaim & post -reclaim disk space usage. 16 | 17 | ## **docker_retag.sh** 18 | This script will re-tag the docker image from image id after restoring the docker images from backup file. 19 | -------------------------------------------------------------------------------- /_regen_qcow2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2017 Théo Chamley 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | # this software and associated documentation files (the "Software"), to deal in the Software 5 | # without restriction, including without limitation the rights to use, copy, modify, merge, 6 | # publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons 7 | # to whom the Software is furnished to do so, subject to the following conditions: 8 | # 9 | # The above copyright notice and this permission notice shall be included in all copies or 10 | # substantial portions of the Software. 11 | # 12 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 13 | # BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 15 | # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM. 16 | 17 | # Array to hold image IDs. Note that it is not necessary to provide the entire 18 | # image ID. Just the first few characters is sufficient. 19 | # Use "docker images" at the command prompt to find image IDs 20 | # Image IDs are entered on the command line separate by a space. 21 | 22 | # Changelog 23 | # Added instructional language prior to script execution - DB 24 | 25 | # Added ability to exit the script in the event that user failed to include 26 | # a key requirement - DB 27 | 28 | # Change code to use an environment variable for location to write image archives -DB 29 | 30 | # Added option to not delete archived docker images after Docker reset 31 | IMAGES=$@ 32 | clear 33 | 34 | echo "This script is designed to help address the growth of the Docker.qcow2 file by:" 35 | echo "1. backing up the image IDs you identify at the command line to a temp directory" 36 | echo "2. removing any containers and images NOT listed below" 37 | echo "3. deleting the Docker.qcow2 file" 38 | echo "4. restoring your image backups" 39 | echo "5. and offering to delete the those backup files." 40 | echo 41 | echo "****You will loose all of your images if you do not provide the IMAGE IDs on the command line.****" 42 | echo "The script will use the directory identified by the TMP_DIR environment variable," 43 | echo "or you may set this variable using the 'EXPORT TMP_DIR=' command prior to running the script." 44 | echo "If you need to exit this script for any reason please do so now." 45 | read -p "Exit? [yes/no] " -n 1 -r 46 | echo 47 | if [[ ! $REPLY =~ ^[Nn]$ ]] 48 | then 49 | exit 50 | else 51 | 52 | echo "Backup and restore the following images:" 53 | echo ${IMAGES} 54 | read -p "Are you sure? [yes/no] " -n 1 -r 55 | echo 56 | if [[ ! $REPLY =~ ^[Yy]$ ]] 57 | then 58 | exit 59 | fi 60 | # Assign local variable to TMP_DIR location either from enviroment variable 61 | # in profile or via EXPORT TMP_DIR command 62 | my_dir=$(echo $TMP_DIR) 63 | pushd $my_dir >/dev/null 64 | 65 | # Use the Docker save command to save images 66 | open -a Docker 67 | echo "Saving images. Please note that large images can take some time to archive." 68 | for image in ${IMAGES}; do 69 | echo "Saving ${image}" 70 | tar=$(echo -n ${image} | base64) 71 | docker save -o ${tar}.tar ${image} 72 | echo "Done." 73 | done 74 | 75 | echo -n "Quiting Docker" 76 | osascript -e 'quit app "Docker"' 77 | while docker info >/dev/null 2>&1; do 78 | echo -n "." 79 | sleep 1 80 | done; 81 | echo "" 82 | 83 | echo "Removing the problem child --> Docker.qcow2 file" 84 | # adjust your qcow2 path here 85 | #rm ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2 86 | rm ~/Library/Containers/com.docker.docker/Data/vms/0/Docker.qcow2 87 | 88 | echo "Restarting Docker application." 89 | open -a Docker 90 | until docker info >/dev/null 2>&1; do 91 | echo -n "." 92 | sleep 1 93 | done; 94 | echo 95 | 96 | echo "Restart Complete!" 97 | 98 | echo "Restoring images from archive." 99 | for image in ${IMAGES}; do 100 | tar=$(echo -n ${image} | base64) 101 | docker load -q -i ${tar}.tar || exit 1 102 | echo "==> Done." 103 | done 104 | 105 | popd >/dev/null 106 | 107 | # Delete archived Images 108 | echo "Would you like it delete archived image(s)?" 109 | echo ${IMAGES} 110 | read -p "[yes/no] " -n 1 -r 111 | echo 112 | if [[ ! $REPLY =~ ^[Yy]$ ]] 113 | then 114 | exit 115 | fi 116 | 117 | echo "Deleting saved images." 118 | cd $my_dir 119 | rm *.tar 120 | fi --------------------------------------------------------------------------------