2023 - January


Docker

Docker is a way to manage containers, which are like namespaces or jails. Here are some useful common commands to check out the status of these containers.

Prune unused containers, images and cache

This can free up disk space.

$ docker system prune
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] y
Deleted Containers:
e91363806fd869a4f9349fc2de4faf26cc6b4b26a7fdbf16c5535c13ef46f995

Deleted Images:
untagged: homeassistant/home-assistant@sha256:1b4a62627444841ad222aaf3795b90581c2f8b712762c8d7d6b6cf9a42045ea8
deleted: sha256:8a975b58aacfc196fbe3eeb34810b9068e8a50d953ff88b67504d1c2351940d7
deleted: sha256:8df0a83f8a5a8360e6deef1187b2dbf6c27b7fe57584bca0b657671363a20729
deleted: sha256:abdac85af7ebbecf80442d554fa655133eb4ee2dbf4d96136caf1615e7f82992
deleted: sha256:b992bfbe5047c28682b5cc7ce976e418b5402113ee404143a4de208214f322f7
deleted: sha256:84e4d6e3db7ee758f3089b2f28ed3b1d248e31c72604004fdfdd34970c1a47
...
Total reclaimed space: 3.556GB

Reference: https://docs.docker.com/engine/reference/commandline/system_prune/

Check a container's logs

$ docker logs -f homeassistant
...
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/pyatv/protocols/airplay/__init__.py", line 258, in _connect_rc
    await control.start(str(core.config.address), control_port, credentials)
  File "/usr/local/lib/python3.10/site-packages/pyatv/protocols/airplay/remote_control.py", line 60, in start
    await self._setup_event_channel(self.connection.remote_ip)
  File "/usr/local/lib/python3.10/site-packages/pyatv/protocols/airplay/remote_control.py", line 102, in _setup_event_channel
    resp = await self._setup(
...

Inspect a container

$ docker container inspect  homeassistant
[
    {
        "Id": "0baa0742e18858ebea97a93ee5a97f0fb09f369d504d3f6d1252c8c35234ac91",
        "Created": "2023-04-16T23:45:06.042429674Z",
        "Path": "/init",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2378698,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-04-16T23:45:06.981493189Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
...

Check for container filesystem changes since start

$ docker diff <container name>

Check container running processes

$ docker top homeassistant
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2378698             2378670             0                   19:45               ?                   00:00:00            /package/admin/s6/command/s6-svscan -d4 -- /run/service
root                2378732             2378698             0                   19:45               ?                   00:00:00            s6-supervise s6-linux-init-shutdownd
root                2378733             2378732             0                   19:45               ?                   00:00:00            /package/admin/s6-linux-init/command/s6-linux-init-shutdownd -c /run/s6/basedir -g 3000 -C -B
root                2378742             2378698             0                   19:45               ?                   00:00:00            s6-supervise s6rc-fdholder
root                2378743             2378698             0                   19:45               ?                   00:00:00            s6-supervise s6rc-oneshot-runner
root                2378751             2378743             0                   19:45               ?                   00:00:00            /package/admin/s6/command/s6-ipcserverd -1 -- /package/admin/s6/command/s6-ipcserver-access -v0 -E -l0 -i data/rules -- /package/admin/s6/command/s6-sudod -t 30000 -- /package/admin/s6-rc/command/s6-rc-oneshot-run -l ../.. --
root                2378782             2378698             0                   19:45               ?                   00:00:00            s6-supervise home-assistant
root                2378784             2378782             1                   19:45               ?                   00:00:15            python3 -m homeassistant --config /config

Check container statistics

$ docker stats homeassistant

CONTAINER ID   NAME            CPU %     MEM USAGE / LIMIT     MEM %     NET I/O   BLOCK I/O        PIDS
0baa0742e188   homeassistant   0.23%     217.6MiB / 15.52GiB   1.37%     0B / 0B   102MB / 10.5MB   21

Check docker networks

$ docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
b67081eb628c   bridge    bridge    local
0921bc6a0760   host      host      local
c8363d03e4cd   none      null      local

$ docker network inspect  bridge
[
    {
        "Name": "bridge",
        "Id": "b67081eb628c51d90d2f234df1c97580bc0d83fa52c300f2fbf4764556a14ba6",
        "Created": "2023-04-15T06:02:48.923361812-04:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "3357239a67204620012173ae3cd724a2508d9456d2aaabee7b1be3d7b351e6f7": {
                "Name": "zwave-js",
                "EndpointID": "7d50227167937e8ce5886abeb87411f66902eae494b4b3860ec133b77a39ecbd",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

Move data files

Default docker installs will put the large containers into /var/lib/docker which may be a directory off root. It is better to put them on a seperate filesystem in case they grow unexpectedly.

1 - Stop the docker daemon

sudo service docker stop

2 - Add a configuration file to tell the docker daemon what is the location of the data directory

Change the data-root location:

File: /etc/docker/daemon.json

{ 
   "data-root": "/path/to/your/docker" 
}

3 - Copy the current data directory to the new one

sudo rsync -aP /var/lib/docker/ /path/to/your/docker

4 - Rename the old docker directory

sudo mv /var/lib/docker /var/lib/docker.old

This is just a sanity check to see that everything is ok and docker daemon will effectively use the new location for its data.

5 - Restart the docker daemon

sudo service docker start

6 - Test

If everything is ok you should see no differences in using your docker containers. When you are sure that the new directory is being used correctly by docker daemon you can delete the old data directory.

sudo rm -rf /var/lib/docker.old

Upgrade a container

Here is my documentation on upgrading the homeassistant docker container:

Upgrade HomeAssistant



January - Docker - Linux in the House - https://linux-in-the-house.org Creative Commons License