adamkdean

software engineering

Count number of files deleted between Git commits

By Adam K Dean on

To count the number of files deleted between two Git commits, use the following command:

$ git log --diff-filter=D --summary 45b0487..HEAD | grep 'delete mode' | wc -l

Be sure to change 45b0487 to the commit you want to count from, or remove 45b0487..HEAD altogether to look through the entire history.

We use grep to count the number of deleted files.

$ git log --diff-filter=D --summary | grep 'delete mode'

delete mode 100644 test (3).txt
delete mode 100644 test (4).txt
delete mode 100644 test (2).txt

Then we use wc -l to do a line count:

$ git log --diff-filter=D --summary | grep 'delete mode' | wc -l
      3

Sometimes you get whitespace back from wc -l, you can trim that with tr:

$ git log --diff-filter=D --summary | grep 'delete mode' | wc -l | tr -d ' '

3

This may take some time for larger repositories.

Find IP address of Docker container

By Adam K Dean on

To get the IP address of a Docker container, use the following command:

$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${CID}

So let's say the ID of the container is a2150s, we could get the IP like so:

$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' a2150s

172.17.0.2

I have written a script for this, /usr/local/bin/dockerip:

#!/bin/bash

COUNT=`docker ps | grep $1 | wc -l | bc`

if [ $COUNT -gt 0 ]; then
  IP_ADDRESS=`docker inspect --format '{{ .NetworkSettings.IPAddress }}' $1`
  echo "$1: $IP_ADDRESS"
else
  echo "Could not find $1. Check container is running."
fi

This will form part of a repository of useful docker scripts.

Local DNS resolver found in resolv.conf and containers can't use it

By Adam K Dean on

I've been playing with Docker for part of a project, and came across this annoying but important message:

WARNING: Local (127.0.0.1) DNS resolver found in resolv.conf and containers can't use it. Using default external servers : [8.8.8.8 8.8.4.4]

The problem? /etc/resolv.conf has nameserver 127.0.0.1 (or nameserver 127.0.1.1) in it.

I worked out that Ubuntu 14.04 LTS uses a daemon called dnsmasq, which routes DNS traffic through to the DNS servers provided by DHCP.

While this works fine for Docker, that message is quite annoying.

I disabled dnsmasq in /etc/NetworkManager/NetworkManager.conf:

$ sudo nano /etc/NetworkManager/NetworkManager.conf

#dns=dnsmasq

I then added the public Google DNS servers to /etc/dhcp/dhclient.conf:

$ sudo nano /etc/dhcp/dhclient.conf

prepend domain-name-servers 8.8.8.8,8.8.4.4;

Then you just need to restart NetworkManager:

$ sudo restart network-manager

And now you can start Docker containers without that pesky message!

Install Sublime Text on Ubuntu

By Adam K Dean on

This relates to installing Sublime Text on Ubuntu 14.04 LTS.

For Sublime Text 2, run the following commands:

$ sudo add-apt-repository ppa:webupd8team/sublime-text-2
$ sudo apt-get update
$ sudo apt-get install sublime-text

For Sublime Text 3, run the following commands:

$ sudo add-apt-repository ppa:webupd8team/sublime-text-3
$ sudo apt-get update
$ sudo apt-get install sublime-text

Notice that it's just a case of changing sublime-text-2 to sublime-text-3.

Don't forget to Install Emmet (Zen Coding) and LESS highlighting too!

Install Docker on Ubuntu

By Adam K Dean on

To install Docker on Ubuntu 14.04 LTS, run the following commands:

$ sudo apt-get install docker.io
$ sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker

Once that is done, add your user to the docker group:

$ sudo usermod -a -G docker adam

After this, you may need to logout and login again. I had an issue where I recieved an error like this:

$ docker ps
dial unix /var/run/docker.sock permission denied

Once you've logged out and back in again, you can test docker is installed and works like so;

$ docker version
Client version: 0.9.1
Go version (client): go1.2.1
Git commit (client): 3600720
Server version: 0.9.1
Git commit (server): 3600720
Go version (server): go1.2.1
Last stable version: 0.11.1

Be sure to read up more at Getting Started