Install Docker To Ubuntu

Install Docker To Ubuntu

Reading time1 min
#Docker#Ubuntu#Linux#DockerInstallation#Containerization#DevOps

Mastering Docker Installation on Ubuntu: A Step-by-Step Guide for Precision and Performance

Forget generic install scripts. This guide dives deep into why each step of Docker installation on Ubuntu matters, revealing common pitfalls and performance tweaks that separate hobbyists from pros.


Installing Docker correctly on Ubuntu is foundational for developers and IT professionals aiming to build consistent, scalable environments. Getting it right from the start minimizes deployment headaches and leverages Ubuntu’s robust ecosystem for containerized applications. In this post, we’ll walk through the precise steps to install Docker on Ubuntu, explain why each step matters, and offer tips to optimize performance and security.


Why This Matters

Many tutorials simply suggest running a single command like apt install docker.io or a quick install script without explaining background details. While that may put Docker on your system, it can lead to issues like:

  • Using outdated Docker versions incompatible with certain containers.
  • Missing crucial configuration steps that ensure better security and stability.
  • Overlooking performance optimizations important for heavier workloads.

Our goal here is to not just install Docker but install Docker properly on Ubuntu.


Prerequisites

  • Ubuntu 20.04 LTS or later (The latest LTS versions provide better compatibility and security updates.)
  • A user with sudo privileges.
  • Basic familiarity with the Linux terminal.

Step 1: Update Your System and Dependencies

Before installing Docker, ensure your system is up to date:

sudo apt update
sudo apt upgrade -y

Docker requires a few prerequisite packages to allow apt to work over HTTPS.

sudo apt install apt-transport-https ca-certificates curl software-properties-common -y

Why this step?
apt-transport-https and ca-certificates ensure the system can securely download Docker packages from Docker's official repositories.


Step 2: Add Docker’s Official GPG Key

Adding Docker’s GPG key helps your system verify the authenticity of the Docker package:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Why this step?
This prevents attackers from tricking you into installing fake software because only trusted software signed with this key will be accepted.


Step 3: Add Docker’s Official Repository

Add the repository for the Docker packages to your system’s apt sources:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Why this step?
Ubuntu’s default docker.io package sometimes lags behind Docker’s official releases. Using the official repo ensures you install the latest stable version.


Step 4: Install Docker Engine

Update the package list again and install Docker Engine along with related tools:

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -y

Components explained:

  • docker-ce: Docker Community Edition engine.
  • docker-ce-cli: Command line interface to interact with Docker.
  • containerd.io: Container runtime Docker uses internally.

Step 5: Verify Docker Installation

To make sure Docker installed correctly and is running, use:

sudo systemctl status docker

You should see that the Docker service is active and running. Additionally, confirm the installed version:

docker --version

Step 6: Manage Docker as a Non-root User

Running Docker commands with sudo every time is cumbersome. Add your user to the docker group:

sudo usermod -aG docker $USER

Then, log out and back in (or restart your session) for this to take effect.

Test it:

docker run hello-world

If the "hello-world" container runs successfully without sudo, you’re all set.


Step 7: (Optional) Enable Docker to Start at Boot

Ensure Docker launches automatically after reboot:

sudo systemctl enable docker

Performance and Security Tips

  • Use OverlayFS for Storage Driver: On Ubuntu, Docker defaults to the optimal storage driver but verify it with:

    docker info | grep "Storage Driver"
    
  • Limit Resource Usage: Define resource limits (CPU, memory) in your container run commands to avoid impacting your host system.

  • Regularly Update Docker: With security patches frequently released, keep Docker updated via Docker’s official repo.

  • Use Docker Bench Security: Run the Docker Bench Security script to audit your Docker installation and configuration.


Troubleshooting Common Pitfalls

  • Docker daemon not running?
    Run sudo systemctl start docker and check logs with sudo journalctl -u docker.

  • Permission denied when running Docker commands?
    Make sure your user is in the docker group and you have logged back in.

  • Conflicting Docker versions?
    Remove any old Docker versions before installing the new ones:

    sudo apt remove docker docker-engine docker.io containerd runc
    

Wrapping Up

By now, you should have a fully functional and properly configured Docker installation on your Ubuntu machine. This setup not only ensures stability but also maximizes security and performance — a real edge whether you're deploying personal projects or enterprise-grade applications.


Example: Running Your First Container

To prove everything works smoothly, run the classic test container:

docker run --rm -it ubuntu bash

This command pulls the latest Ubuntu image and starts a shell inside a container. You can experiment inside this environment, and when you exit (exit), the container stops and is removed because of --rm.


If you want to dive deeper into configuring Docker for multi-node setups, container orchestration, or persistent storage on Ubuntu, stay tuned for upcoming posts. For now, enjoy your Docker-powered development environment!


Happy containerizing! 🚀


Feel free to leave comments or questions below!