Uncategorized

Installing Docker on Ubuntu: A Step-by-Step Guide 🚀(with Debugg)

Before you start

  • 3 min read
  • 17 to run in terminal

Boxes marked Run in terminal are what you type; plain text explains why.

Docker simplifies application deployment by packaging software into containers

Here's how to install Docker on Ubuntu efficiently:



1. Uninstall Old Versions#

Remove any previous Docker installations to avoid conflicts

Run in terminalbash
sudo apt-get remove docker docker-engine docker.io containerd runc

This ensures a clean environment for the new installation



2. Set Up Docker's Official Repository#

a. Install Prerequisite Packages

Ensure apt can use repositories over HTTPS

Run in terminalbash
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg

b. Add Docker’s GPG Key

Add Docker's official GPG key to verify package authenticity

Run in terminalbash
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.asc > /dev/null
sudo chmod a+r /etc/apt/keyrings/docker.asc

c. Set Up the Stable Repository

Add Docker's repository to your sources list

Run in terminalbash
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

Then, update the package index

Run in terminalbash
sudo apt-get update


3. Install Docker Engine#

Install Docker Engine and associated components

Run in terminalbash
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

This command installs:

  • docker-ce: Docker Engine
  • docker-ce-cli: Command-line interface for Docker
  • containerd.io: Container runtime
  • docker-buildx-plugin: Docker Build
  • docker-compose-plugin: Docker Compos


4. Verify Installation#

a. Check Docker Version

Confirm the installation by checking the Docker version

Run in terminalbash
docker --version

b. Run a Test Container

Run the "Hello World" container to verify Docker is functioning correctly

Run in terminalbash
sudo docker run hello-world

This command downloads and runs a test image, outputting a confirmation message upon success



5. Manage Docker as a Non-Root User#

To run Docker commands without sudo

a. Create Docker Group

Create the Docker group if it doesn't already exist

Run in terminalbash
sudo groupadd docker

b. Add User to Docker Group

Add your user to the Docker group

Run in terminalbash
sudo usermod -aG docker $USER

c. Apply Group Changes

Log out and log back in to apply the group changes. Alternatively, use

Run in terminalbash
newgrp docker

d. Verify Non-Root Access

Run the "Hello World" container without sudo

Run in terminalbash
docker run hello-world
Code
If successful, Docker is configured for non-root usage


Troubleshooting Common Issues#

a. Permission Denied While Accessing Docker Socket

Error:

  • Got permission denied while trying to connect to the Docker daemon socket

Solution:

  • Ensure your user is added to the Docker group

  • Verify group membership with

Run in terminal
groups $USER
  • If the issue persists, adjust the ownership of the Docker directory
Run in terminal
sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
sudo chmod g+rwx "$HOME/.docker" -R

b. Docker Daemon Not Running

Error:

  • Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Solution:

  • Start the Docker service
Run in terminalbash
sudo systemctl start docker
  • Enable Docker to start on boot
Run in terminalbash
sudo systemctl enable docker

c. Old Versions of Docker Installed

Issue:

  • Conflicts with previous Docker installations

Solution:

  • Uninstall old Docker versions
Run in terminal
sudo apt-get remove docker docker-engine docker.io containerd runc
  • Then, proceed with the installation steps above

d. Network Connectivity Issues

Error:

  • Failed to fetch errors during "apt-get update"

Solution:

  • Check your internet connection -

  • Ensure your system's DNS settings are correctly configured



By following these steps, you should have Docker successfully installed on your Ubuntu systemFor more detailed information, refer to the [official Docker documentation]

Reference Link: (https://docs.docker.com/engine/install/ubuntu/