[Docker] Broken occ. Failed to connect to database after upgrade

I managed to get Nextcloud working again, and I’m not entirely sure how. It doesn’t really matter for me anyway since I’ve migrated my stuff over to Seafile now.

Anyway, for anyone having the same issue I’ll lay out how I solved it.

When I first setup my server (Ubuntu 20.04) I was prompted if I wanted to install docker. This installs the snap version of docker however. This wasn’t such an issue and worked fine, but I wanted to move to the standard docker install. So I stopped all my docker containers. Since I wasn’t running a whole lot, and most of it was with docker-comopse this wasn’t an issue for me.

So I did this:

sudo snap remove docker
sudo systemctl disable snapd.service
sudo systemctl disable snapd.socket
sudo systemctl disable snapd.seeded.service
sudo apt autoremove --purge snapd

Entirely removing snap. Then I installed docker in the recommended way (for up-to-date install methods check their website).

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
apt-cache policy docker-ce
sudo apt install docker-ce
sudo systemctl status docker
sudo usermod -aG docker ${USER}
su - ${USER}
sudo apt-get install docker-compose-plugin
echo alias docker-compose=\'docker compose\' >> ~/.bashrc
echo alias dc=\'docker compose\' >> ~/.bashrc
source ~/.bashrc

Then I spun up the containers I wanted to run again, and I had the idea to see if nextcloud would be working again, but no. This time it was a different error message though, namely a database authentication error.

Checking the postgres logs, the user ‘oc_’ was missing.
In my config file, this is the default database user (for some reason):

  'dbname' => 'postgres',
  'dbhost' => 'postgres',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'oc_<username>',
  'dbpassword' => '<redacted>',

It also seemed that the original database file wasn’t being found, despite being mounted in docker-compose:

postgres:
        container_name: postgres
        restart: always
        #command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
        volumes:
            - '/media/storagedrive/nextcloud-db:/var/postgresql/data'
        environment:
            - POSTGRES_USER=<REDACTED> #This user is different from the oc_<username>
            - POSTGRES_PASSWORD=<REDACTED> #This password as well.
        image: postgres:13.5

So I ran a postgres command to find the database manually:

docker exec -u postgres -it postgres psql -f /media/storagedrive/nextcloud-db/nextcloud-db.sql

Then I created the correct user oc_<username>:

docker exec -u postgres -it postgres createuser -P -l -r -d oc_<username>

I filled in the correct password as per the config.php file, and occ was working again.
After this I had to run the occ upgrade command, and then to be sure I ran nearly every maintenance, repair, and file-scan command I could.
Just run docker exec -u www-data -it nextcloud php occ list to get a list of all command you could run, and then just run most of them.

In the end I’m not sure if recreating the database users did the trick, or that the docker snap package is hot garbage. The latter is probably true whether or not snap caused my issue.