Docker#

Docker is an awesome way to containerize your applications to make them easier to backup and manage.

Note

This page is incomplete. I plan to add more documentation to it later.

Docker Compose#

Many tutorials in this documentation will use docker-compose.yml files.

Useful Commands#

Attach to a compose file’s container#

First, make sure that jq is installed: sudo apt-get install -y jq. Then, copy this code into a file called compose-attach somewhere in your $PATH and make it executable.

#!/usr/bin/env bash
argument="$1"
if [ ! -n "$argument" ]; then
  # argument is empty
  container_id=$(sudo docker compose ps --format json | jq -r '.[0] | .ID')
else
  container_id=$(sudo docker compose ps --format json | jq -r ".[] | select(.Name | test(\"^$argument\$\")) | .ID")
fi
if [ ! -n "$container_id" ]; then
  echo "Invalid container!"
  exit 1
fi
sudo docker attach "$container_id"

To detach, press CTRL+P, CTRL+Q.

Docker Networking#

More than 30 bridge networks#

The default pool is given by this (https://github.com/moby/libnetwork/blob/67e0588f1ddfaf2faf4c8cae8b7ea2876434d91c/ipamutils/utils.go#L18-L21):

{
  "default-address-pools" : [
    {
      "base": "172.17.0.0/16",
      "size": 16
    },
    {
      "base": "172.18.0.0/16",
      "size": 16
    },
    {
      "base": "172.19.0.0/16",
      "size": 16
    },
    {
      "base": "172.20.0.0/14",
      "size": 16
    },
    {
      "base": "172.24.0.0/14",
      "size": 16
    },
    {
      "base": "172.28.0.0/14",
      "size": 16
    },
    {
      "base": "192.168.0.0/16",
      "size": 20
    }
  ]
}

That means that we can create a total of \(3 * 2^{16-16} + 3 * 2^{16-14} + 1 * 2^{20-16} = 3 * 1 + 3 * 4 + 1 * 16 = 32\) networks. But we can’t use the 172.17.0.0/16 network for bridge networks, so we are left with 31. Remember that the "size" is the netmask of the individual networks, so by increasing it, we decrease the number of IP addresses allowed on a network, but increase the number of possible networks. I’m going to change some of the 172.28.0.0/14, size:16 networks to be size 24.

With this change, I should be able to create many more networks without manually specifying the subnet for each of them. Now I run sudo systemctl restart docker.

Docker Cheatsheet#

https://devopscycle.com/blog/the-ultimate-docker-cheat-sheet/

../../_images/the-ultimate-docker-cheat-sheet-4.png