Getting Docker to Work with Mac OS X Yosemite

Docker Logo
Docker Logo

I use Docker for some personal and work related projects. Docker has always worked great for me until I upgraded my Mac operating system from Mavericks to Yosemite. Once I upgraded to Yosemite, despite having Docker configured to work properly, the Docker command line couldn’t connect to the boot2docker vm that is required in order to run Docker on OS X. I could use `boot2docker ssh` and then do my Docker tasks, but that didn’t work for me as I am also trying to use `knife container` to manage Docker containers via Chef.

To fix this issue, here is what I did:

  • boot2docker delete
  • In Virtualbox, go to VirtualBox (menu) => Preferences => Network => Host Only Networks, delete the host only networks, then I re-added them
  • boot2docker init
  • boot2docker up

Now Docker works from the OS X command line via iTerm2 for me.

OS X Mavericks: Pause a Quicktime Screen Recording

If you are a user of OS X Mavericks and use Quicktime to record screencasts and whatnot, you may have noticed that Quicktime appears to no longer have the option to pause your recording. The truth is that the functionality is still there, but it is just very well hidden.

During your recording, if you would like to pause the recording, follow these steps:

1) Click on the Quicktime icon in your app bar:

Quicktime

 

 

 

 

 

 

2) Press the “esc” (escape) key

At this point, you will see the control window for Quicktime, and then you will notice it only has a stop button – not a pause button.

Screen Shot 2014-07-23 at 12.06.07 PM

 

 

 

 

 

3) Hold down the “alt” key and you will now see a pause button

If your video is already paused, you will then see the record (red) button which allows you to continue. If you with to simply stop the video, hold down the “alt” key and press stop.

 

One Liner: Check Cron Jobs for All Users on a *nix Server

If you are ever in a situation where you are asked to find out whether or not a server is being used for anything important, some of the first things you would want to check is whether or not there are active websites, databases and automated tasks. Automated tasks often times are run by the Cron daemon on behalf of a user on the server. If you would like to check the Crontab entries for all users on the server, you can run a one-line command as a super user and get a nice long list of who does and does not have a Crontab entry:

for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done | grep -v '#'

The “grep -v” after the pipe simply ensures that no lines starting with a hash mark (#) are listed.

Full MySQL Search & Replace in All Databases and Tables

If you get into half of the database stuff that I regularly get into, you probably have a need to replace a specific string, such as a URL, very often – especially if you are cloning databases for use with WordPress. Sure, there are a lot of plugins or programs out there which do the same thing, but most of them avoid three-dimensional arrays and work on only one database – excluding that low level option of modifying all databases.

Because I couldn’t find anything worthwhile for what I was doing, I decided to make one myself.

This script is designed to be run by Php in the command line, but will also work in  a web browser if you replace all instances of PHP_EOL with HTML line breaks. To run this script the way that I wrote it, simply run it like this (assuming you saved the code below to a file named query-db.php):

php ./query-db.php

To make this work, change the credentials in the “Database Credentials” below, and change the strings in $search and $replace variables below.

<?php

/* Search & Replace All Databases + Fields + Columns
*
* Written by Kris Law for KASL Network
* October 21, 2012
*
* This script will search and replace specified strings from
* all columns from all tables from all databases.
*
* USE AT YOUR OWN RISK!
*/


// Database Credentials
$test_db='db.host.com';
$db_user='myDb_us3r';
$db_pass='myDb_p@ssw0rd';

// Search & Replace
$search = 'the quick brown fox jumped over the lazy dog'; // term to have replaced
$replace = 'the lazy dog was quicker and blocked the quick brown fox'; // string to put in place

// Open DB Connection
$link = mysql_connect($test_db, $db_user, $db_pass);

### Show list of databases
$query_string = "SHOW databases";
$query = mysql_query($query_string);

echo "Running Search & Replace on Databases. This will take a while..." . PHP_EOL . PHP_EOL;

while($array = mysql_fetch_array($query)){

$database = $array['Database'];
$q2 = mysql_query("show tables in $database");

if($database != 'information_schema' && $database != 'mysql' && $database != 'test'){

while($array2 = mysql_fetch_array($q2)){

$tables = $array2['Tables_in_'. $database];

$q3 = mysql_query("show columns in $database.$tables");

while($array3 = mysql_fetch_array($q3)){

$column = $array3['Field'];

$replace_query = "update $database.$tables set $column = replace($column, '$search','$replace')";
$replace = mysql_query($replace_query) or die(mysql_error());

echo "Searching $database.$tables->$column" . PHP_EOL;

}

}
}

}

echo PHP_EOL . "All instances of $search have been replaced with $replace!";

?>

Convert WordPress Multi-DB back to Single DB

If you have used the WPMU Multi-DB plugin to fragment your databases from a single instance to 16 or more databases, there may come a time where you need to merge all of those databases back into one. In my case, I want to be able to use Backup Buddy to backup and migrate WordPress blogs. Here are the steps on how to accomplish this massive change. Keep in mind, this is a tutorial  which is written for informational purposes only. I am not responsible for anything that goes wrong.

Create a new database named myDatabase:

mysql> create database myDatabase;

Make a MySQL dump of your existing multisite database:

mysqldump -v -u wpadm -h myDatabaseName.cygwiniuiold.us-east-1.rds.amazonaws.com -pP@ssw0rd --all-databases --skip-lock-tables > WP_DUMP.sql

Next, strip out the MySQL dump lines in the .sql file which tell it to use different databases:

egrep -v "^USE" WP_DUMP.sql > WP2.sql

Next, strip out the MySQL dump lines in the .sql file which tell it to create databases:

egrep -v "^CREATE DATABASE" WP2.sql > WP3.sql

Change what database is currently “used” when it is being imported:

sed -i 's/-- Current Database: `WP_db_00`/use myDatabase;/g' WP3.sql

Import the MySQL dump into the newly created database from step one:

mysql -v -u root -pMyPassword myDatabase < WP3.sql

Last, but not least, remove all files which came with the Multi-DB plugin (db.php and db-conf.php)

* Keep in mind that I spent a lot of time scratching my head over why the sites wouldn’t show up in the Network Admin > Sites list. After all kinds of looking around, I found out that my main global database had been overwritten by another database in the –all-databases array which had the same tables.

Install Homebrew & Ruby on Mac OSX Lion

The purpose of this article is to help you configure Mac OSX so that it has a package manager (similar to apt-get, YUM, Pacman or Portage on Linux distributions), and to update you to the latest version of Ruby (1.9.3 at the time of this writing). Currently, Mac OSX Lion 10.7.4 has Ruby version 1.8.7 which is slightly out of date.

Installing Homebrew

Homebrew is a very easy to use package management system. To view more information on Homebrew, please visit the Homebrew home page.

Enter the following command to install Homebrew – I took this directly from the Homebrew installation page:

/usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"

It will take anywhere from 5 to 10 minutes for Homebrew to install.

After Homebrew is installed, you can install all kinds of packages from the command line. For example, if you would like to install Nmap:

brew install nmap

To update your brew installed packages:

brew update --system

For other brew commands, type this command:

brew help

Installing Ruby

Once Homebrew is installed, you can install Ruby using the brew command:

brew install ruby

Ruby will take another 5 to 10 minutes to install.

In order to use the version of Ruby which was installed by Homebrew (instead of using the default version 1.8.6), you can modify your /etc/paths file by moving /usr/local/bin to the top of the list (above /usr/bin).

Reboot your machine and the changes will be applied.

After you log back in, you can check what version of Ruby you are using by running the following command:

ruby -v

Installing Ruby Gems

Now that Ruby is installed, you can install whatever Ruby Gems that you need. For example, if you wish to install the mechanize gem, run this command:

sudo gem install mechanize

Importing Images Into WordPress from SquareSpace

This article is a continuation of a previous article that I wrote titled Migrating Blog Content and Assets from SquareSpace to WordPress. That article showed you how to actually get the content of the blogs from SquareSpace into WordPress. This article show you how to get the files and images.

Install Firefox and the DownThemAll Extension

There is an extension for the Firefox Browser called DownThemAll. This extension allows you to batch download files which are linked to in a web page. Install the DownThemAll extension – this will be the quickest way to get this job done. You can manually grab all the files and migrate them too, but if you have a lot of files, I don’t recommend it.

Get all images and files

Before you start, keep in mind that DownThemAll does not enter directories and take images from inside them. It only takes files that are in the current page. Not only does it take images, but if you tell it to, it will download every link on the page, including web pages. Because I wanted this job to be done fast and was not worried about taking 10 Megabytes of disk space, I told it to download everything. After that, I entered a sub directory on the page in the list, and downloaded those images one-by-one since there were only a few in each directory.

  • Open Firefox (and make sure the DownThemAll extension is in installed)
  • Sign into the backend of your SquareSpace site at http://yoursite.squarespace.com/display/Login
  • Click on the “Website Management” tab
  • Click on “File Storage” under the Data and Media category
  • Right-click your mouse in an empty part of the page and select DownThemAll

  • Set the path where you want the files to be downloaded (I created a directory on my desktop and named it website.com)
  • Create a directory inside the website.com directory called “storage”
  • Check all of the boxes in the DownThemAll window and click “Start”

  • Inside of your “storage” directory, create a sub-directory with the same name as any sub-directories in your SquareSpace file list (if applicable)
  • Enter each sub-directory in the web page and download those files all to the sub-directory you created in the storage directory

Now that all of your assets are downloaded and the directory structure is the same as it was on the SquareSpace site, you can copy the whole storage directory to the root of your WordPress installation (or the root of the multi-site installation where WordPress treats a directory as the root of an individual site; in my case, each migrated site has it’s own root at /var/www/migrated-content/website.com/)

Here’s the command I used to put the storage directory on the web server:

scp -r -i ~/.ssh/my_private_key ~/Desktop/website.com/storage root@website.com:/var/www/migrated/content/website.com/

Setting Up the Eclipse IDE With PHP

For Php developers, there are two options for setting up the Eclipse IDE. You can install the Php only version which is made specifically for those who use Php and only Php, or you can install the classic version and add Php into it. Systems Admins will want to install the classic version and add Php to it because of other tools that may be needed such as Ant. Ant is a Java program for building automated scripts for deployments, etc. There is another program which is based on Ant, but built with Php called Phing. If you use Phing, you do not need Ant. However, if you want to be able to launch Phing scripts from the Eclipse IDE, you can do it by using Ant to execute Phing which would execute Phing scripts in Eclipse’s built in console.Eclipse can be obtained from http://www.eclipse.org/downloads/. If you using a Mac, but don’t know if you need the 32-bit or 64-bit version, open up the Terminal.app and run the following command:

uname -a

After running that command, you should see an output similar to the following:

Darwin LB.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386

The very last “word” on the block above is i386. i386 means you have a 32-bit computer. This means you need the 32-bit version of Eclipse. If you see something different, such as x64 or x86-64 then that means you need the 64-bit version of Eclipse because your computer is running 64-bit software.

After you have downloaded and installed Eclipse (we are assuming you installed the classic version), you will then need to add Php tools to the IDE. You can do this like so:

  • Open Eclipse (start the program)
  • Click on Help > Install New Software
  • Where it says “Work with:”, select the dropdown menu (next to the “Add” button)
  • Select “All Available Sites”
  • Install the following modules and you should be set up:
    • Eclipse Platform
    • Eclipse RCP Target Components
    • Eclipse SDK
    • Under “Programming Languages,” select Php Development Tools and any other languages that you might need.
    • Under “Web, XML, Java EE…” select Php Development Tools…
Accept agreements as needed if you agree with them, and press next and let it work until it is finished. After it is finished, press the finished button and then restart the Eclipse IDE.

Get the real file or directory size in unix or linux

Size of DirectoriesAll BASH users (Linux, Unix, OSX, etc) use the ls command, but when we want to know how much disk space has been used, the ls command just doesn’t cut it sometimes. While it is a useful command for listing information about files in a directory, or the directory structure, it doesn’t give you the overall space that a directory uses – including the files inside of it. Sometimes you need a lot more information and the commands to do it are not commonly known. Here’s a typical output of the ls command:


user@localhost:~/Pictures$ ls -lh

 total 644M
 -rw------- 1 user user 993K Jul 12 15:08 IMAG0142.jpg
 -rw------- 1 user user 790K Jul 12 15:08 IMAG0143.jpg
 -rw------- 1 user user 1.1M Jul 12 15:08 IMAG0144.jpg
 -rw------- 1 user user 1.3M Jul 12 15:08 IMAG0145.jpg
 -rw------- 1 user user 1.1M Jul 12 15:08 IMAG0146.jpg
 -rw------- 1 user user 1.1M Jul 12 15:08 IMAG0147.jpg

Notice how the output shows the sizes of the files. However, if we cd .. and look at the directory itself, it shows this:

drwxr-xr-x 2 user user 24K Jul 21 12:02 Pictures

Although it is clearly stated above while in the Pictures directory that the content takes up 644 Megabytes of space, listing the directory itself only shows that it is 24 Kilobytes. That’s a little misleading, don’t you think?

In order to get around this issue, there is a different command that will do the trick; the du command.

user@localhost:~/Pictures$ du -sh
 686M .

The command as typed above shows the combined size of all directories in the present working directory. However, if you were to add a directory name to the command, you would have this output:

user@localhost:~/$ du -sh Pictures
 644M Cell

If you were to use a * instead of a directory name, you would retrieve the results of all of the directories in the current directory:

user@localhost:~/$ du -sh *
 644M Pictures
 2.6M Videos
 4.5M Wallpapers

Remove DOS EOL ^M Character from File

endoflineIf you have the ^M character in your file, it is a Dos/Windows end of line character that is tough to get rid of. You could always ignore it, but the possibility of it corrupting a file is very high. Some editors such as Nano don’t even seem to show this character by default – maybe Nano strips it out – I’m not sure at this point.

If you are a vi warrior like I am, then you don’t really care for using Nano anyway. To remove the DOS EOL character using the vi editor, the easiest way is by following these steps:

vi filename

Press “:” to get to command mode.

Search and Replace all Globally is :%s/^M//g (Press control V then control M) which will replace ^M with nothing.

Then to write and quit enter “:wq”

In summary, here’s the command would look in vi:

vi filename
:%s/^M//g

If you need the character to actually turn into line breaks, try this:

vi filename
:%s/^M/\r/g