Shrink docker’s VirtualBox machine for mac

Over time, the boot2docker virtual machine image disk.vmdk will consume more and more disk space as each disk sector is written to for the first time (up until its maximum virtual disk size or the host system runs out of resources).

There are a couple of approaches to reduce the size of the virtual box image. First approach is to delete the virtual machine and then reinstall it. Alternatively you can shrink the virtual machine disk.

This guide demonstrates how to shrink the boot2docker virtual machine image. It is intended for reference only and you should always make sure you have appropriate backups first before attempting to do this.

Before you begin

Docker can leave being dangling images when using the docker commands. These images build up taking consume additional space, so before we begin, lets remove all the containers that are in an exited state as well as any dangling images.

Shrinking the VirtualBox image

This approach will take the .vmdk VirtualBox image, convert it into a .vdi image, compact it and then convert it back to .vmdk. Sadly there are no tools included within VirtualBox to compact a .vmdk image directly

The drawback with the approach is that you need to have enough free disk space on the host machine to clone the image. If you do not have enough free disk space then you will need to use external storage for storing the temporary .vdi image.

This guide assumes your docker virtual machine is called “default”.

  1. Log into the the docker machine and execute a command to fill all the free space on the disk with 0’s.


    This creates a file zero.fill in /mnt/sd1/tmp and the current working directory. The purpose of this file is to force all the data bits in available free space to be reset to 0. By doing this, VirtualBox will be able to compact that space thus reducing the boot2docker virtual machine size.

    Note: You will receive the error message cat: write error: No space left on device that the device has run out of space, this is expected.

    Exit the docker machine host.

  2. Back on the host system, stop the running boot2docker virtual machine so the image can be compacted.

  3. Navigate to the directory where your docker machine is kept.

  4. Convert the .vmdk image to .vdi using the clone command


    – If you do not have enough disk space on your machine, consider using external storage and change any reference of disk.vdi to /Volumes/<external-drive-name/disk.vdi.
    – This will take several minutes. Time to make a cup of tea.

  5. Compact the .vdi image

  6. Get the UUID for the original disk.vmdk disk image, and write it down.


    ! Make a note of the UUID
    This will be needed later when converting the .vdi image back into a .vmdk image.

  7. Remove to original disk.vmdk

  8. Convert the .vdi image back into a .vmdk image

  9. Reset the UUID of the new disk.vmdk image to be the same as the original disk.vmdk UUID

  10. Restart the docker virtual machine

  11. Lastly, remove the disk.vdi file as it is no longer needed.

    All done.

    Checking the size of the virtual disk, it’s now only 3.4GB. That’s almost 85% smaller! Over time it will increase again and you’ll need to go through this same routine.

Countdown timer using RxJS

Creating a countdown timer with RxJS is pretty simple. The block of code below creates an Observable that when it receives a truthy on the input stream, the countdown will commence emitting the next value in the sequence on every interval until 0 is reached.

Any further truthy values on the input stream will restart the countdown whilst falsey values will cancel the countdown.

Times are based in milliseconds.

Breaking it down

The magic in this stream occurs within the switchMap. When the input stream sends through a truthy value, an Rx timer starts counting incrementing based on the defined interval. This timer continues until the duration has been reached, plus one (to allow the countdown to reach 0 later) e.g. [0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000].

If a falsey value comes through the input stream then switchMap will swap out the timer for an Obversable that will never emit, thus cancelling the countdown.

Finally, the map function inverts the direction of the counting timer so that we get a countdown e.g. [10000, 9000, 8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000, 0].

Example

See the example on jsbin.com demonstrating the use of the stream to launch a rocket.
https://jsbin.com/sibacav/edit?js,output

 

The code is written in TypeScript but is easily convertible to JavaScript.

Recipe: How to create a self-signed SSL certificate Ubuntu + Nginx

RECIPE: Create a self-signed SSL certificate for Ubuntu and Nginx website.

  1. Create a directory to place the ssl certificates.
  2. Generate the ssl certificate replacing [CERTIFICATE_NAME] with a valid file name.

    Example:-
  3. Reference the certificate inside the server block in the nginx site config file.
  4. Go to your site in a browser e.g. https://example.com/ and you should get a browser warning message saying the site is unsafe because the certificate authority i.e. you, is not trusted by your computer. Trust the certificate to view your site over SSL.

    Untrusted connection warning message
    An example of the kind of warning messages browsers give when using self-signed certificates