0
0
Dockerdevops~10 mins

Installing Docker - Step-by-Step CLI Walkthrough

Choose your learning style9 modes available
Introduction
Docker lets you run apps inside containers that work the same everywhere. Installing Docker sets up the tools you need to create and manage these containers on your computer.
When you want to package your app with all its parts to run anywhere without setup problems
When you need to try out software without installing it directly on your system
When you want to share your app with teammates so they run it exactly the same way
When you want to run multiple apps on one machine without them interfering
When you want to learn container technology and how apps run in isolated environments
Commands
This command updates the list of available packages and their versions on your system. It ensures you get the latest Docker version.
Terminal
sudo apt-get update
Expected OutputExpected
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Get:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB] Fetched 215 kB in 1s (234 kB/s) Reading package lists... Done
This installs tools needed to add Docker's official GPG key and repository securely.
Terminal
sudo apt-get install -y ca-certificates curl gnupg lsb-release
Expected OutputExpected
Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: ca-certificates curl gnupg lsb-release 0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded. Need to get 1,234 kB of archives. After this operation, 5,678 kB of additional disk space will be used. Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 ca-certificates all 20210119~20.04.2 [146 kB] Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 curl amd64 7.68.0-1ubuntu2.7 [159 kB] Get:3 http://archive.ubuntu.com/ubuntu focal/main amd64 gnupg all 2.2.19-3ubuntu2.1 [1,123 kB] Get:4 http://archive.ubuntu.com/ubuntu focal/main amd64 lsb-release all 11.1.0ubuntu2 [14.1 kB] Fetched 1,442 kB in 1s (1,234 kB/s) Selecting previously unselected package ca-certificates. (Reading database ... 123456 files and directories currently installed.) Preparing to unpack .../ca-certificates_20210119~20.04.2_all.deb ... Unpacking ca-certificates (20210119~20.04.2) ... Selecting previously unselected package curl. Preparing to unpack .../curl_7.68.0-1ubuntu2.7_amd64.deb ... Unpacking curl (7.68.0-1ubuntu2.7) ... Selecting previously unselected package gnupg. Preparing to unpack .../gnupg_2.2.19-3ubuntu2.1_all.deb ... Unpacking gnupg (2.2.19-3ubuntu2.1) ... Selecting previously unselected package lsb-release. Preparing to unpack .../lsb-release_11.1.0ubuntu2_all.deb ... Unpacking lsb-release (11.1.0ubuntu2) ... Setting up ca-certificates (20210119~20.04.2) ... Setting up curl (7.68.0-1ubuntu2.7) ... Setting up lsb-release (11.1.0ubuntu2) ... Setting up gnupg (2.2.19-3ubuntu2.1) ... Processing triggers for man-db (2.9.1-1) ...
-y - Automatically answer yes to prompts
This downloads Docker's official GPG key and saves it securely so your system can verify Docker packages.
Terminal
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Expected OutputExpected
No output (command runs silently)
-f - Fail silently on server errors
-s - Silent mode, no progress shown
-S - Show errors if they occur
-L - Follow redirects
This adds Docker's official repository to your system so you can install Docker from it.
Terminal
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Expected OutputExpected
No output (command runs silently)
Update package lists again to include Docker's repository.
Terminal
sudo apt-get update
Expected OutputExpected
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease Get:2 https://download.docker.com/linux/ubuntu focal InRelease [36.0 kB] Get:3 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages [8,123 B] Fetched 44.1 kB in 1s (44.1 kB/s) Reading package lists... Done
This installs Docker Engine, the command line tools, and container runtime needed to run containers.
Terminal
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
Expected OutputExpected
Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: containerd.io docker-ce docker-ce-cli 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded. Need to get 50.3 MB of archives. After this operation, 200 MB of additional disk space will be used. Get:1 https://download.docker.com/linux/ubuntu focal/stable amd64 containerd.io amd64 1.6.18-1 [30.2 MB] Get:2 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-cli amd64 5:24.0.2-1~ubuntu.20.04~focal [22.1 MB] Get:3 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce amd64 5:24.0.2-1~ubuntu.20.04~focal [2,123 kB] Fetched 54.4 MB in 3s (18.1 MB/s) Selecting previously unselected package containerd.io. (Reading database ... 123456 files and directories currently installed.) Preparing to unpack .../containerd.io_1.6.18-1_amd64.deb ... Unpacking containerd.io (1.6.18-1) ... Selecting previously unselected package docker-ce-cli. Preparing to unpack .../docker-ce-cli_5%3a24.0.2-1~ubuntu.20.04~focal_amd64.deb ... Unpacking docker-ce-cli (5:24.0.2-1~ubuntu.20.04~focal) ... Selecting previously unselected package docker-ce. Preparing to unpack .../docker-ce_5%3a24.0.2-1~ubuntu.20.04~focal_amd64.deb ... Unpacking docker-ce (5:24.0.2-1~ubuntu.20.04~focal) ... Setting up containerd.io (1.6.18-1) ... Setting up docker-ce-cli (5:24.0.2-1~ubuntu.20.04~focal) ... Setting up docker-ce (5:24.0.2-1~ubuntu.20.04~focal) ... Processing triggers for man-db (2.9.1-1) ...
-y - Automatically answer yes to prompts
Check if the Docker service is running properly after installation.
Terminal
sudo systemctl status docker
Expected OutputExpected
● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2024-06-07 10:00:00 UTC; 10s ago Docs: https://docs.docker.com Main PID: 12345 (dockerd) Tasks: 8 Memory: 50.0M CGroup: /system.slice/docker.service └─12345 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock Jun 07 10:00:00 ubuntu systemd[1]: Started Docker Application Container Engine.
Run a test Docker container to verify Docker is installed and working correctly.
Terminal
sudo docker run hello-world
Expected OutputExpected
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:3e0e0a7a4f4a3a5a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
Key Concept

If you remember nothing else from this pattern, remember: Installing Docker involves adding its official repository, installing the engine and tools, and verifying the service runs correctly.

Common Mistakes
Skipping the step to add Docker's official GPG key and repository
Your system won't trust Docker packages, so installation will fail or install outdated versions.
Always add Docker's official GPG key and repository before installing Docker packages.
Not running 'sudo apt-get update' after adding the Docker repository
Your system won't see the new Docker packages, so installation will fail or install old versions.
Run 'sudo apt-get update' after adding any new repository to refresh package lists.
Trying to run Docker commands without sudo or without adding your user to the docker group
You get permission denied errors because Docker requires root privileges by default.
Run Docker commands with sudo or configure your user to run Docker without sudo.
Summary
Update your system package list to get the latest versions.
Add Docker's official GPG key and repository to your system.
Install Docker Engine and related tools using your package manager.
Verify Docker service is running and test with a sample container.