3 ways To Install Docker in Ubuntu

Installing Docker on Ubuntu transforms your machine into a powerful virtualization host, allowing you to package software together with all its necessary dependencies.

Whether you are setting up a local lab or a production server, Linux provides built-in package managers to handle the installation securely. This guide explains three practical methods to get Docker running, from the standard APT repository to portable Snap packages.

Key Takeaways: Mastering Docker Installation

  • APT (Advanced Package Tool) → The standard system used by Ubuntu to install, update, and remove software packages.
  • Docker Engine → The core runtime (package name docker.io) that enables you to build and run containers.
  • Sudo Privileges → Administrative rights are mandatory for installing software and managing the Docker daemon.
  • Snap Packages → A portable software format that bundles dependencies, offering an alternative way to run Docker.
  • System Integrity → Always update your package index before installation to ensure you receive the latest security patches.

Method 1: Installing Docker via APT (Recommended)

The most reliable way to install Docker on Ubuntu is through the official repositories using the APT package manager. This method ensures the software is optimized for your specific Ubuntu version.

1. Update the Package List Before installing, synchronize your local database with the global repositories: sudo apt update

2. Install Docker and Compose Use the following command to install the Docker engine and the Docker Compose plugin simultaneously: sudo apt-get install -y docker.io docker-compose

3. Verify the Installation Check the version to confirm the service is active: docker --version

Expected Output: Docker version 24.0.7, build 24.0.7-0ubuntu4

Method 2: Installing via the Ubuntu Snapshot Service

For production environments requiring a specific historical version of Docker, you can use the Ubuntu Snapshot Service. This allows you to install packages from a time-specific state of the archive.

Command: sudo apt install docker.io --snapshot 20240501T120000Z

This is particularly useful when you need to manage APT repositories to maintain consistency across a large cluster of servers.

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

Method 3: Using the Docker Snap

If you prefer a self-contained installation that doesn’t interfere with your system libraries, Canonical provides an official Docker snap. Snaps are ideal for users who want the latest version of Docker regardless of their Ubuntu release cycle.

Command: sudo snap install docker


Step-by-Step Process: Running Your First Container

Once Docker is installed, follow these steps to verify it is working by launching a standard web server container:

  1. Launch your terminal and verify you have administrative access by understanding the Linux sudo command.
  2. Pull and run the image: Execute sudo docker run --name my-web-server -d ubuntu/apache2.
  3. Check container status: Run docker ps to see the active container.
  4. Inspect the container: Use docker inspect my-web-server to view networking and storage details.
  5. Audit storage: You can check file size in Linux within the Docker volume directory at /var/lib/docker/volumes.
  6. Stop the service: When finished, run docker stop my-web-server.

Summary Tables

GoalRecommended CommandPurpose
Standard Installsudo apt install docker.ioBest for most Ubuntu servers.
Install Composesudo apt install docker-composeFor managing multi-container apps.
Update Indexsudo apt updateRefreshes the package database.
Check Versiondocker --versionVerifies successful installation.
Remove Dockersudo apt remove docker.ioUninstalls the software binaries.
Installation FeatureAPT MethodSnap Method
MaintenanceSupported by CanonicalSupported by Snapcraft
DependenciesUses shared system librariesSelf-contained/Sandboxed
Ease of UseStandard CLI workflowAuto-updating background

FAQs

What is the difference between docker.io and docker-ce? docker.io is the package maintained and supported directly by Ubuntu/Canonical. docker-ce (Community Edition) is the version maintained by Docker Inc. For standard Ubuntu stability, docker.io is recommended.

Do I always need to use sudo with Docker? Yes, by default, the Docker daemon binds to a Unix socket owned by the root user. You must use the sudo command unless you add your user to the “docker” group.

Can I install Docker on older Ubuntu versions? Yes, but you may need to manually manage the /etc/apt/sources.list file if you are using a version prior to Ubuntu 24.04 LTS.


Related Posts

David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 637

Leave a Reply

Your email address will not be published. Required fields are marked *