# Docker

import {
  daemonJson,
  debianAptSource,
  ubuntuAptSource,
  verifyDockerData,
} from "@/lib/mdx-constants";
## Getting started

Installing Docker on a Debian-based Linux system is a straightforward process, but ensuring that all dependencies, keyrings and configurations are correctly set up is crucial for a smooth installation. This guide walks you through the steps needed to install Docker, add users to the Docker group and finalize the setup for managing containers on your Debian system. By the end of this guide, your system will be fully ready to run Docker containers efficiently and securely.

### References

<ReferenceLinks references={frontmatter.references} />

---

## Docker Desktop

Docker Desktop is the recommended way to run Docker on <a href={frontmatter.references[0].href} target="_blank" rel="noopener">macOS</a> and <a href={frontmatter.references[1].href} target="_blank" rel="noopener">Windows</a>, as it provides an easy-to-use interface and built-in tools for managing containers.

For Linux, Docker Desktop is not required. Instead, you can install the Docker Engine (see below) directly, which provides all the necessary functionality without the additional overhead of Docker Desktop.

---

## Docker Engine

### Pre-install

1. #### Install required dependencies

   Before installing Docker, ensure your system has all the necessary dependencies. This step installs packages that are essential for Docker to function properly on Debian Linux.

   ```sh
   sudo apt install ca-certificates curl -y
   ```

2. #### Install required keyrings

   Docker requires keyrings to authenticate the software packages during installation. This step ensures that your system has the appropriate keyrings installed to verify Docker’s official package sources.

   ```sh
   sudo install -m 0755 -d /etc/apt/keyrings
   ```

3. #### Add Docker GPG key

   Add the official Docker GPG key to your system’s keyring. This ensures that packages you download are verified as authentic and from the trusted Docker repositories.

   ```sh
   sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
   ```

4. #### Change file permission

   Modify the file permissions to ensure Docker's configuration files and directories are properly accessible. This step is essential for maintaining system security and ensuring Docker operates smoothly.

   ```sh
   sudo chmod a+r /etc/apt/keyrings/docker.asc
   ```

5. #### Add docker repository to apt source

   Add Docker’s official repository to your APT sources. This will allow you to install Docker directly from Docker’s official repository, ensuring you get the latest version.

   ```sh
   echo     "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian     $(. /etc/os-release && echo "$VERSION_CODENAME") stable" |     sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
   ```
     ```sh
     echo     "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu     $(. /etc/os-release && echo "$VERSION_CODENAME") stable" |     sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
     ```
   6. #### Update apt package list

   Run an APT update to refresh your package database and include the new Docker repository. This ensures that your system recognizes Docker as an available package for installation.

   ```sh
   sudo apt update
   ```

### Install Docker

Install the Docker Engine on your system. This command downloads and installs the latest stable version of Docker, enabling you to start using Docker on your Debian system.

```sh wrap
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
```

### Post-install

1. #### Add user to docker group

   Add your user to the docker group. This allows you to run Docker commands without needing sudo each time, streamlining the Docker management process.

   ```sh
   sudo groupadd docker
   ```

2. #### Modify user permissions

   Modify user permissions to ensure they have the necessary access rights to interact with Docker.

   ```sh
   sudo usermod -aG docker $USER
   ```

3. #### Reboot the system

   Finally, reboot your system to ensure all changes take effect.

   ```sh
   sudo reboot
   ```

### Docker data on external HDD (optional)

1. #### Stop docker

   Stop the Docker service.

   ```sh
   sudo systemctl stop docker
   ```

2. #### Move docker data

   Move docker data to the new directory.

   ```sh
   sudo mv /var/lib/docker {{EXTERNAL_HDD_PATH_VAR}}/docker-data
   ```

3. #### Open daemon.json

   ```sh
   sudo nano /etc/docker/daemon.json
   ```

4. #### Modify Docker daemon.json

   Modify the Docker daemon settings to use the new directory.

   ```json title="/etc/docker/daemon.json"
   {
     "data-root": "{{EXTERNAL_HDD_PATH_VAR}}/docker-data"
   }
   ```

5. #### Restart Docker

   Restart the Docker service.

   ```sh
   sudo systemctl daemon-reload && sudo systemctl restart docker
   ```

6. #### Verify change

   Check if Docker is now using the new directory.

   ```sh
   # Docker Root Dir: {{EXTERNAL_HDD_PATH_VAR}}/docker-data
   docker info | grep "Docker Root Dir"
   ```
