Docker: Remove all images and containers

Problem:

You use Docker, but working with it created lots of images and containers. You want to remove all of them to save disk space.

Solution:

Warning: This will destroy all your images and containers. It will not be possible to restore them!

Run those commands in a shell:
# Delete all containers
sudo docker rm $(docker ps -a -q)
# Delete all images
sudo docker rmi $(docker images -q)

This solution has be proposed by GitHub user @crosbymichael in this issue

Create a global git commit hook

Recently found out about these git hooks and they work flawlessly but my only worry was that I had to copy all my hooks over every repo such that I could use them, which was a pain sometimes.

Then started reading about this so called global git hooks, and found a fix.

Usually the default template library is found at /usr/share/git-core/templates and we are going to use that directory for all our hooks.

1. Enable git templates:

git config --global init.templatedir '/usr/share/git-core/templates/'

This tells git to copy everything in /usr/share/git-core/templates/ to your per-project .git/ directory when you run git init

2. Write your hooks in /usr/share/git-core/templates/hooks.

For example, here’s an update hook (located in /usr/share/git-core/templates/hooks/update):

#!/bin/sh
# Prevent pushing changes to master branch
if [ $USER != "mmphego" ];
then
  if [ "$1" == refs/heads/master ];
  then
    echo "Manual pushing to this repo is restricted"
    exit 1
  fi
fi

4. Make sure the hook is executable.

chmod a+x /usr/share/git-core/templates/hooks/update

5. Re-initialize git in each existing repo you’d like to use this in:

git init

NOTE if you already have a hook defined in your local git repo, this will not overwrite it.

Why am I getting ‘Errno(105): No buffer space available’ when subscribing to multicast addresses?

I have been experiencing multicast subscription when subscribing to more that 20 IP’s via smcroute and python socket module. After vigorous googling I finally found a fix.

Screenshot_2017-06-28_11-59-55.png

Linux OS, limit the number of multicast group memberships that a machine can belong to simultaneously. (A “multicast group membership” indicates that a machine is listening to messages for a specific multicast IP address. In other words, there is a limit on how many multicast IP addresses you can listen to.)

On Linux, in particular, the default limit is relatively small (only 20 on many standard kernels). However, this limit can be configured dynamically.

If you try to subscribe to too many multicast addresses at once, you may run into the error message below:

daemon error: Warn: ADD MEMBERSHIP failed; Errno(105): No buffer space available
22
daemon error: Warn: ADD MEMBERSHIP failed; Errno(105): No buffer space available
23
daemon error: Warn: ADD MEMBERSHIP failed; Errno(105): No buffer space available

The “Errno 105” in this message indicates that the errno value returned by the Linux network stack is 105, or ENOBUFS. This return value while adding a multicast address indicates that your machine is trying to be a member of too many multicast groups at the same time.

Network Tuning

Solution is to tune your kernel, backup your current sysclt.conf and replace contents with the ones below.

sudo vim /etc/sysctl.conf

# Bigger buffers (to make 40Gb more practical). These are maximums, but
# the default is unaffected.
net.core.wmem_max=268435456
net.core.rmem_max=268435456
net.core.netdev_max_backlog=10000

# Avoids problems with multicast traffic arriving on non-default interfaces
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.all.rp_filter=0

# Force IGMP v2 (required by CBF switch)
net.ipv4.conf.all.force_igmp_version=2
net.ipv4.conf.default.force_igmp_version=2

# Increase the ARP cache table
net.ipv4.neigh.default.gc_thresh3 = 4096
net.ipv4.neigh.default.gc_thresh2 = 2048
net.ipv4.neigh.default.gc_thresh1 = 1024

# Increase number of multicast groups permitted
net.ipv4.igmp_max_memberships = 1024

You can reload the configuration file by executing, sudo sysctl -p or by rebooting your system.

How to ignore files only locally in git?

I have automated tests running everyday, and the issue is that those tests make necessary changes to certain files depending on which test is running. This becomes a problem when I have to report the changes in the repo, as this will report untracked changed, which means I have to ignore certain files such that local Git doesn’t report changes in them specifically.

By making sure to run git update-index --assume-unchanged ./matplotlibrc after making the addition to the exclude file this means changes won’t be picked up until then.


git update-index --assume-unchanged index.rst
git update-index --assume-unchanged matplotlibrc

Before:
Screenshot_2017-06-02_07-31-01

After:

Screenshot_2017-06-02_08-19-20.png

Changes made but untracked, nice!!!

How can I merge two or more Git commits into one[locally and remote]?

You can do this fairly easily without git rebase or git merge --squash. In this example, we’ll squash the last 3 commits.

If you want to write the new commit message from scratch, this suffices:

git reset --soft HEAD~3 &&
git commit

If you want to start editing the new commit message with a concatenation of the existing commit messages (i.e. similar to what a pick/squash/squash/…/squash git rebase -i instruction list would start you with), then you need to extract those messages and pass them to git commit:

git reset --soft HEAD~3 && 
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"

Both of those methods squash the last three commits into a single new commit in the same way. The soft reset just re-points HEAD to the last commit that you do not want to squash. Neither the index nor the working tree are touched by the soft reset, leaving the index in the desired state for your new commit (i.e. it already has all the changes from the commits that you are about to “throw away”).

Important: If you’ve already pushed commits to GitHub, and then squash them locally, you will have to force the push to your branch.

$ git push origin branch-name --force

Helpful hint: You can always edit your last commit message, before pushing, by using:

$ git commit --amend

Credit