Skip to content

Instantly share code, notes, and snippets.

@olaferlandsen
Last active March 13, 2025 18:21
Show Gist options
  • Save olaferlandsen/72635f3472109730c3bd84280b986181 to your computer and use it in GitHub Desktop.
Save olaferlandsen/72635f3472109730c3bd84280b986181 to your computer and use it in GitHub Desktop.
Docker Desktop Fix-Trick Workaround for Ubuntu 24.04+

Temporary Trick to Keep Docker Desktop Working Correctly on Ubuntu(24.04+; including 24.10) Without Losing Data ๐Ÿณ๐Ÿš€

This script provides a temporary workaround to make Docker Desktop function "correctly" on Ubuntu 24.04+ without losing volumes, images, or containers. It helps avoid the common issue where Docker Desktop fails to start, requiring users to delete ~/.docker or reinstall Docker Desktop repeatedly to make it work again.

๐Ÿ›  How It Works?

When Ubuntu starts or a user logs in, the script checks if Docker Desktop is running. If Docker is NOT running, it deletes a specific file that may be preventing it from starting correctly. This trick avoids unnecessary resets and ensures Docker Desktop can start properly without erasing your existing containers, images, or volumes. It also runs before shutdown or reboot, ensuring a clean state for the next session.

โš ๏ธ Important Notes:

  • No need to delete ~/.docker manually
  • Preserves your containers, images, and volumes
  • Avoids constant reinstallations of Docker Desktop
  • Fully automated at startup, login, and shutdown

Create the Script

Save the following script as /usr/local/bin/check_docker.sh:

#!/bin/bash
FILE_TO_REMOVE="/home/$USER/.docker/desktop/settings-store.json"
if ! pgrep -x "Docker Desktop" > /dev/null; then
    if [ -f "$FILE_TO_REMOVE" ]; then
        rm -f "$FILE_TO_REMOVE"
    fi
fi

Set Permissions

sudo chmod +x /usr/local/bin/check_docker.sh

Run the Script at Startup and Before Shutdown

Create a systemd service file:

sudo nano /etc/systemd/system/check_docker.service

Add the following content:

[Unit]
Description=Checks if Docker is running and deletes a file if not
After=network.target
Before=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/check_docker.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target shutdown.target reboot.target halt.target

Save the file (press Ctrl + X, then Y, and Enter).

Enable and Start the Service

Run these commands to enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable check_docker.service
sudo systemctl start check_docker.service

Run the Script on User Login

To execute the script when a user logs in, add the following line to your ~/.bashrc or ~/.bash_profile:

/usr/local/bin/check_docker.sh

If you want it to run when a graphical session starts, add it to ~/.profile:

echo "/usr/local/bin/check_docker.sh" >> ~/.profile

Verify Everything Works

To manually test the script, run:

/usr/local/bin/check_docker.sh

To check the systemd service status:

systemctl status check_docker.service

Summary

  • Checks if Docker Desktop is running
  • Deletes a file if Docker is not active
  • Runs at system startup, before shutdown, and on user login
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment