Move the docker data directory

The default location for the docker data directory, when using overlay2 storage driver is /var/lib/docker. Maybe you want to move it to another location, and it’s actually pretty easy.

  1. Stop the docker service:
    sudo systemctl stop docker.service
  2. Copy the docker folder to the new location:
    sudo cp -rp /var/lib/docker /path/to/new/location
    The p in -rp will preserve ownership, permissions and timestamps
  3. Tell the docker daemon about the new location by editing the /etc/docker/daemon.json file with the following:
{
    "data-root": "/path/to/new/location"
}

4. Start the docker service:
sudo systemctl start docker.service

Thats it, the docker directory has been moved.

The above guide is tested on Ubuntu 20.04, but will probably be very much alike on other flavours.

Postgresql not listening on docker interface after reboot

/etc/postgresql/12/main/postgresql.conf is where you define the interfaces for postgresql to listen on. It’s done with the line “listen_interfaces =”
So, to get postgresql to listen on a docker interface, you have to add the IP address to the configuration:

listen_addresses = 'localhost,172.18.0.1'

This will make postgresql listen after connections from my docker container. Unfortunately when i rebooted the machine, postgresql was only listening on the localhost. That was strange.

After doing some fiddling around, I discovered that I might had a timing issue, and I even found a couple of articles covering the issue. The postgresql service needs to wait for the docker service to be started, or it cannot listen on the docker interface, that seems logical. I tried following some of the workarounds described, but I couldn’t get it to work. I then took a look on how the default systemd is build, and it seems that /etc/systemd/system/default.target.wants/ consist of symbolic links to /lib/systemd/system/whatever, so i did the same:

sudo ln -s /lib/systemd/system/postgresql@.service /etc/systemd/system/default.target.wants/

I then added this line to the unit block:

After=docker.service

This seems to have done the trick. After a reboot, postgresql is listening on my docker interface. You can always verify with:

sudo ss -nltp | grep postgres