Docker image size reducer
Using a Docker give us great opportunity to move server instances from one machine to another in cloud or in bare metal servers, but sometimes it’s has some price like storage that Docker use.
For example after moving one of my Docker instances with creating images it was using 3+GB so I figure out how to reduce this state to acceptable size of 670 MB.
The solution is export a Docker image and import it back. I wrote two bash functions which make those actions. The both functions are making same things. First one use containers as entry point and second one use already created images.
Warning: This will delete all previous versions of this Docker instance.
Example of usage:
This can be used by copy/paste in command line or if you are doing it frequently just add them into your ~/.bashrc file.
The solution is export a Docker image and import it back. I wrote two bash functions which make those actions. The both functions are making same things. First one use containers as entry point and second one use already created images.
Warning: This will delete all previous versions of this Docker instance.
# Reduce docker image size by removing history of image function docker-image-reduce-by-container() { DOCKER_CONTAINER_ID=$1 DOCKER_NEW_IMAGE_NAME=$2 if [ ${#DOCKER_CONTAINER_ID} == 0 ]; then echo "Current container id is required as first parameter."; exit elif [ ${#DOCKER_NEW_IMAGE_NAME} == 0 ]; then echo "New image name is required as secund parameter."; exit fi TMP_FILE_PATH='/tmp/docker-image-size-reducer-by-container-'$(date | md5sum | awk '{print $1}')'.tar' docker export $DOCKER_CONTAINER_ID > $TMP_FILE_PATH cat $TMP_FILE_PATH | docker import - $DOCKER_NEW_IMAGE_NAME rm $TMP_FILE_PATH } # Reduce docker image size by removing history of image function docker-image-reduce-by-image() { DOCKER_CURRNT_IMAGE_NAME=$1 DOCKER_NEW_IMAGE_NAME=$2 if [ ${#DOCKER_CURRNT_IMAGE_NAME} == 0 ]; then echo "Current container image name is required as first parameter."; exit elif [ ${#DOCKER_NEW_IMAGE_NAME} == 0 ]; then echo "New image name is required as secund parameter."; exit fi TMP_FILE_PATH='docker-image-size-reducer-by-image-'$(date | md5sum | awk '{print $1}') docker run --name="$TMP_FILE_PATH" $DOCKER_CURRNT_IMAGE_NAME docker-reduce-by-container $TMP_FILE_PATH $DOCKER_NEW_IMAGE_NAME }
Example of usage:
docker-image-reduce-by-container "your-current-container" "your-new-image" docker-image-reduce-by-image "your-current-image" "your-new-image"
This can be used by copy/paste in command line or if you are doing it frequently just add them into your ~/.bashrc file.