How to Install Docker on Ubuntu 22.04 Server
From my experiences in software engineering, Docker is by far the most effective tool that gives me ease in development and app deployment in different ecosystems.
Docker is a tool that simplifies app processes inside a container. This container allows developers to package up an application with all of its dependencies into a bundle. This will make the app run consistently in any environment.
Let’s think Docker is like a lunchbox. In a lunchbox, you can neatly pack all the different components of your meal — sandwiches, fruits, snacks — each in its own compartment. Like a lunchbox, Docker allows you to package up all the components of your application — like the code, libraries, and dependencies — into a container. So, Docker is like a lunchbox for your applications, making them easy to carry and deploy wherever you need them.
Prerequisites
To follow this tutorial, please make sure that you have fulfilled these requirements:
- A running Ubuntu 22.04 machine, including a sudo user.
- Opened SSH access using SSH client if you want to connect the machine via remote access.
Step 1 — Preparation & Installation
Before installing Docker using APT, we need to do some preparation on our Ubuntu machine. The first step in this section is to make sure the docker installation package is up to date on our machine. We can update our package list by running this.
sudo apt update
Next, we need to install some packages that will allow apt to use packages using HTTPS.
sudo apt install apt-transport-https ca-certificates curl software-properties-common
To ensure we install Docker from a valid official Docker repository, we need to add Docker’s GPG key to our system. GPG Key or GNU Privacy Guard in simplified terms is an implementation to create a secure data transmission between sources and is also used to verify the genuineness of the message origin.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Then, we can add the Docker repository to our 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
After adding the new source, we can update our APT repository to recognize the new addition.
sudo apt update
Then, you need to check the installation of docker-ce. Make sure that you install Docker from its official repository instead of default Ubuntu repository.
apt-cache policy docker-ce
After running the command above, you will notice that docker-ce is not installed, but it has the candidate version.
docker-ce:
Installed: (none)
Candidate: 5:26.0.2-1~ubuntu.22.04~jammy
Version table:
5:26.0.2-1~ubuntu.22.04~jammy 500
500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
5:26.0.1-1~ubuntu.22.04~jammy 500
500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
The version may be different on your machine. Then, we can start installing Docker
sudo apt install docker-ce
You will be informed that this will take disk spaces, just type ‘Y’ and enter
After this operation, 430 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
After the process has finished, the daemon automatically starts and the process automatically starts on boot. To make sure the process is running, run this command:
sudo systemctl status docker
You will see something like this
root@ubuntu-s-1vcpu-2gb-sgp1-01:~# sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2024-04-19 16:45:22 UTC; 2min 1s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 121510 (dockerd)
Tasks: 7
Memory: 29.1M
CPU: 361ms
CGroup: /system.slice/docker.service
└─121510 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
To exit from the systemctl status, press Ctrl + C.
By installing docker, besides enabling the Docker daemon, you will also be given access to the ‘docker’ command for using Docker.
Step 2 — Using Docker Command
To use docker, you can use command and options with arguments. The syntax for the docker command will look like this:
docker [option] [command] [arguments]
To see the list of all commands, you can type:
docker
This command will return a response of the complete Docker command list:
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Common Commands:
run Create and run a new container from an image
exec Execute a command in a running container
ps List containers
build Build an image from a Dockerfile
pull Download an image from a registry
push Upload an image to a registry
images List images
login Log in to a registry
logout Log out from a registry
search Search Docker Hub for images
version Show the Docker version information
info Display system-wide information
Management Commands:
builder Manage builds
buildx* Docker Buildx
compose* Docker Compose
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
plugin Manage plugins
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Swarm Commands:
swarm Manage Swarm
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
export Export a container's filesystem as a tar archive
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Global Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env
var and default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket to connect to
-l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Run 'docker COMMAND --help' for more information on a command.
For more help on how to use Docker, head to https://docs.docker.com/go/guides/
To see the description of each command, you can run:
docker COMMAND --help
// For example
docker pull --help
Docker will return the complete description for the ‘pull’ command.
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Download an image from a registry
Aliases:
docker image pull, docker pull
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet Suppress verbose output
To see a complete guide on how to use Docker, you can visit their official documentation at https://docs.docker.com/go/guides/
Conclusions
Congratulations! You have successfully installed Docker on your Ubuntu machine, the next step is to learn how to use Docker properly from creating images, pushing images, pulling images, running a container, and so on. I will try to create a regular article on how to use Docker properly. Please stay tuned!
Thank you and happy coding!