This README provides a summary of how the install-docker.sh script installs Docker Community Edition (CE) on Ubuntu. It follows the official Docker documentation and includes additional comments for clarity.
This script ensures any outdated or conflicting Docker packages are removed, then sets up the official Docker repository on your system. It installs Docker CE, Docker Compose, and related plugins. Finally, it verifies the installation and optionally configures your system so you can run Docker commands without using sudo.
- An Ubuntu-based system (tested on Ubuntu 22.04, 20.04, etc.).
- Access to an account with
sudoprivileges.
-
Removing Old Packages
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc do sudo apt-get remove -y "$pkg" done
- Removes any conflicting packages like
docker.ioor older versions ofdocker-compose.
- Removes any conflicting packages like
-
Updating Package Index & Installing Dependencies
sudo apt-get update sudo apt-get install -y ca-certificates curl
- Refreshes package lists.
- Installs tools needed to manage certificates and make remote requests via HTTPS.
-
Adding Docker’s Official GPG Key
sudo install -m 0755 -d /etc/apt/keyrings sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc
- Creates a dedicated directory
/etc/apt/keyringswith safe permissions (0755). - Downloads and saves Docker’s official GPG key.
- Adjusts permissions to ensure the key is readable by the package manager.
- Creates a dedicated directory
-
Adding Docker’s Repository
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 sudo apt-get update
- Dynamically inserts system architecture and Ubuntu release codename into the Docker stable repo.
- Refreshes package lists again, now including Docker’s repo.
-
Installing Docker Packages
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
- Installs Docker engine, CLI, containerd, Buildx, and the Compose plugin from Docker’s repository.
-
Verifying Docker Installation
sudo docker run hello-world
- Pulls and runs the
hello-worldDocker image to confirm Docker is functioning properly.
- Pulls and runs the
-
Creating and Configuring the Docker Group
sudo groupadd docker sudo usermod -aG docker $USER newgrp docker docker run hello-world- Creates a user group named
dockerif it doesn’t exist. - Adds your current user to the
dockergroup so you can run Docker commands withoutsudo. - Starts a new shell to load the updated group membership.
- Confirms the setup by running the
hello-worldcontainer again without sudo.
- Creates a user group named
-
Make the script executable (if not already):
chmod +x install-docker.sh
-
Run the script:
./install-docker.sh
-
When the script finishes, Docker should be installed, and you should be able to use Docker as a non-root user (after restarting your session or using
newgrp docker).
- After installation, you can check your Docker version:
docker --version
- Or run a test container:
docker run hello-world