In the dynamic landscape of modern software development, efficiency and portability are key. As technology continues to evolve, so do the tools that help us streamline our workflows and enhance productivity. Docker, a groundbreaking platform, has emerged as a pivotal solution in this regard. It empowers developers to create, deploy, and manage applications within isolated environments called containers, ensuring consistency across various computing environments.
If you're diving into the world of Docker or seeking to upgrade your skills to the latest Ubuntu release, you're in the right place. This article serves as your comprehensive guide, walking you through the process of installing and effectively utilizing Docker on the Ubuntu 22.04 operating system. Whether you're a seasoned developer looking to adopt more efficient practices or a newcomer eager to grasp the fundamentals, we'll cover all the necessary steps to get you up and running with Docker in no time.
Join us as we explore the core concepts of containerization, the benefits of Docker, and the step-by-step instructions to set up Docker on the Ubuntu 22.04 platform. By the end of this guide, you'll not only have a solid understanding of Docker's principles but also possess the practical skills needed to incorporate it seamlessly into your development workflow. Let's embark on this journey to harness the power of Docker and revolutionize the way you build, ship, and run applications.
What is Docker?
Docker is a platform and tool designed to automate the process of creating, deploying, and managing applications within lightweight, portable, and self-sufficient containers. Containers are encapsulated environments that package an application along with its dependencies, libraries, and configuration files. This approach provides consistency across different environments, making it easier to develop, test, and deploy software across various systems, from development to production.
In a traditional software deployment scenario, compatibility issues can arise when an application is moved from one environment to another, such as from a developer's laptop to a testing server or a production server. These differences might stem from variations in operating systems, libraries, and configurations. Docker solves this problem by encapsulating the application and its dependencies into a container, isolating it from the underlying system, and ensuring that it runs consistently across different environments.
How to Install Docker on Ubuntu
Follow step-by-step instructions to effortlessly set up Docker CE on your Ubuntu 22.04 system. Proper installation is crucial for a seamless Docker experience, and this section ensures a smooth process for both beginners and experienced users.
Option 1 - Installing Docker from the Official Repository
Install Docker from the official Docker repository to ensure you get the latest stable program version. To access the official Docker repository, add the new package source to Ubuntu and then install Docker. Follow the steps below:
First, update your existing list of packages:
sudo apt-get update
Next, install a few prerequisite packages which let apt
use packages over HTTPS:
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
Then add the GPG key for the official Docker repository to your system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Add the Docker repository to 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
Update your existing list of packages again for the addition to be recognized:
sudo apt-get update
Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:
apt-cache policy docker-ce
You’ll see output like this, although the version number for Docker may be different:
docker-ce:
Installed: (none)
Candidate: 5:24.0.5-1~ubuntu.22.04~jammy
Version table:
5:24.0.5-1~ubuntu.22.04~jammy 500
500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
5:24.0.4-1~ubuntu.22.04~jammy 500
500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
5:24.0.3-1~ubuntu.22.04~jammy 500
500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
5:24.0.2-1~ubuntu.22.04~jammy 500
500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
5:24.0.1-1~ubuntu.22.04~jammy 500
Notice that docker-ce
is not installed, but the candidate for installation is from the Docker repository for Ubuntu 22.04 (jammy
).
Finally, install Docker:
sudo apt-get install docker-ce -y
Specifying the -y
flag automatically answers yes
to any prompt during the installation.
Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:
sudo systemctl status docker
The output should be similar to the following, showing that the service is active and running:
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2023-08-27 14:12:53 UTC; 28s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 8469 (dockerd)
Tasks: 9
Memory: 35.6M
CPU: 355ms
CGroup: /system.slice/docker.service
└─8469 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Aug 27 14:12:51 ip-172-26-13-203 systemd[1]: Starting Docker Application Container Engine...
Aug 27 14:12:51 ip-172-26-13-203 dockerd[8469]: time="2023-08-27T14:12:51.676332450Z" level=info msg="Starting up"
Option 2 - Installing Docker from the Default Repositories
Another way to install Docker on Ubuntu is to use the default Ubuntu repository. Although the installation process is more straightforward, the Docker package may be outdated. If you don't care about having the latest Docker version, follow the steps below and install Docker using the default repository.
First, update your existing list of packages:
sudo apt-get update
Run the following command to install Docker:
sudo apt-get install docker.io -y
Specifying the -y
flag automatically answers yes
to any prompt during the installation.
Install all the Docker dependency packages by running the following command:
sudo snap install docker
The command installs all the dependencies using the Snap package manager.
How To Use Docker on Ubuntu
All Docker information, including the syntax, options, and commands, is available by running the docker command in the terminal:
docker
Output :
sage: 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
Start using Docker by downloading Docker images, creating containers, and managing Docker volumes.
Note: Docker commands can only be run with the
sudo
prefix on Ubuntu.
Working With Docker Images
Docker images are the base for building Docker containers. The images are located on Docker Hub, which is a Docker repository. The repository allows all users to host their images on the Docker hub, resulting in many images, including applications and Linux distributions.
The sections below show different ways of working with Docker images.
Search for Docker Images
Search for available images on Docker Hub using the docker search
command. The syntax is:
sudo docker search [keyword]
For [keyword]
, specify the keyword you want to search for. For example, to show all Ubuntu images, run:
sudo docker search ubuntu
Output:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 16324 [OK]
websphere-liberty WebSphere Liberty multi-architecture images … 296 [OK]
open-liberty Open Liberty multi-architecture images based… 61 [OK]
neurodebian NeuroDebian provides neuroscience research s… 103 [OK]
ubuntu-debootstrap DEPRECATED; use "ubuntu" instead 52 [OK]
ubuntu-upstart DEPRECATED, as is Upstart (find other proces… 115 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 98
ubuntu/squid Squid is a caching proxy for the Web. Long-t… 65
ubuntu/cortex Cortex provides storage for Prometheus. Long… 4
ubuntu/apache2 Apache, a secure & extensible open-source HT… 60
ubuntu/kafka Apache Kafka, a distributed event streaming … 32
ubuntu/mysql MySQL open source fast, stable, multi-thread… 52
ubuntu/bind9 BIND 9 is a very flexible, full-featured DNS… 59
ubuntu/prometheus Prometheus is a systems and service monitori… 49
ubuntu/zookeeper ZooKeeper maintains configuration informatio… 9
ubuntu/postgres PostgreSQL is an open source object-relation… 31
ubuntu/redis Redis, an open source key-value store. Long-… 19
ubuntu/grafana Grafana, a feature rich metrics dashboard & … 9
ubuntu/memcached Memcached, in-memory keyvalue store for smal… 5
ubuntu/dotnet-deps Chiselled Ubuntu for self-contained .NET & A… 9
ubuntu/dotnet-aspnet Chiselled Ubuntu runtime image for ASP.NET a… 11
ubuntu/prometheus-alertmanager Alertmanager handles client alerts from Prom… 9
ubuntu/dotnet-runtime Chiselled Ubuntu runtime image for .NET apps… 10
ubuntu/cassandra Cassandra, an open source NoSQL distributed … 2
ubuntu/telegraf Telegraf collects, processes, aggregates & w… 4
The output is a list of all images containing the Ubuntu keyword. If the OFFICIAL
column contains the [OK]
parameter, the image was uploaded by the official company that developed the project.
Pull a Docker Image
After deciding which image you want, download it using the pull
option. The syntax is:
sudo docker pull [image-name]
For example, download the official Ubuntu image by running:
sudo docker pull ubuntu
Output:
Using default tag: latest
latest: Pulling from library/ubuntu
b237fe92c417: Pull complete
Digest: sha256:ec050c32e4a6085b423d36ecd025c0d3ff00c38ab93a3d71a460ff1c44fa6d77
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
After downloading the image, use it to spin up a container. Alternatively, trying to create a container from an image that hasn't been downloaded causes Docker to download the image first and then create the container.
See Downloaded Images
Check which images you have downloaded by running:
sudo docker images
Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 01f29b872827 3 weeks ago 77.8MB
Working With Docker Containers
A Docker container is an isolated virtual environment created from a Docker image. Use an image you previously downloaded or specify its name in the docker run
command to automatically download the image and create a container.
For example, use the hello-world
image to download a test image and spin up a container. Run the following command:
sudo docker run hello-world
Output:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c
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.
(amd64)
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/
The command instructs Docker to download the image from Docker Hub and spin up a container. After creation, the "Hello from Docker" message appears, along with the explanation of how the container works, and then Docker shuts it down.
Run a Docker Container
Like in the test container created in the previous section, running a Docker container utilizes the run
subcommand. The syntax for creating a new Docker container is as follows:
sudo docker run [image-name]
For [image-name]
, specify the name of the image to use as a base for the container. When creating a new container, Docker assigns it a unique name. Alternatively, start a new container and give it a name of your choice by passing the --name
switch:
sudo docker run --name [container-name] [image-name]
Note: The
docker run
command is an alias fordocker container run
. Before Docker version 1.13, only thedocker run
command was used, but later it was refactored to have the formdocker [COMMAND] [SUBCOMMAND]
, where the[COMMAND]
iscontainer
, and the[SUBCOMMAND]
isrun
.
For example, run the following command to create a container using the Ubuntu image we pulled earlier:
sudo docker run ubuntu
The terminal changes, stating that it is operating within the container, identifying it via the container ID. Use the container ID to remove, start, or stop the container.
After running the container in interactive mode, pass any command normally to interact with the system. Also, the access is root, so there is no need for sudo
. Any changes made inside the container apply only to that container.
Exit the container by running the exit
command in the prompt.
View Docker Containers
An active Docker container is a container that is currently running. Listing containers is useful as it outputs the unique ID and name required when starting, stopping, or removing a container.
To see only the active Docker containers, run:
sudo docker ps
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
The command outputs a list of active containers. If there are no containers in the list, they have all been shut down, but they still exist in the system.
To list all containers, including the inactive ones, add the -a
flag:
sudo docker ps -a
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c05e6a72230 ubuntu "/bin/bash" 11 minutes ago Exited (0) 11 minutes ago jolly_archimedes
b2e6865b8bae ubuntu "/bin/bash" 11 minutes ago Exited (0) 11 minutes ago admiring_dewdney
b2e644885238 hello-world "/hello" 3 hours ago Exited (0) 3 hours ago gallant_liskov
Start a Docker Container
Start a stopped container using the docker start
command. The syntax is:
sudo docker start [container-ID | container-name]
Provide either the container ID or the container name. The container name is a unique name that Docker assigns to the container during creation. Obtain the ID and name by listing all containers.
For example, the following command starts the Ubuntu container we previously created:
sudo docker start 5f9478691970
Stop a Docker Container
Stop a running Docker container using the docker stop
command. The syntax is:
sudo docker stop [container-ID | container-name]
For example, to stop the running Ubuntu container, run:
sudo docker stop 5f9478691970
Remove a Docker Container
Remove an unnecessary Docker container using the docker rm
command. The syntax is:
sudo docker rm [container-ID | container-name]
For example, the following command removes the hello-world
test container we previously created:
sudo docker rm techvblogs
Note: Pass the
--rm
switch when creating a container to remove the container automatically when stopped.
Working With Docker Volumes
A Docker volume is a filesystem mechanism allowing users to preserve data generated and used by Docker containers. The volumes ensure data persistence and provide a better alternative to persisting data in a container's writable layer because they don't increase the Docker container size.
Use Docker volumes for databases or other stateful applications. Since the volumes are stored on the host, they don't depend on the container but allow for easy data backups and sharing between multiple containers.
Create a Docker Volume
Create a Docker volume using the following syntax:
sudo docker volume create [volume_name]
For [volume_name]
, specify a unique name for your volume.
For example:
sudo docker volume create example-volume
Remove a Docker Volume
Remove a Docker volume using the following syntax:
sudo docker volume rm [volume_name]
For example:
sudo docker volume rm example-volume
Conclusion:
By mastering Docker installation and usage on Ubuntu 22.04, you've gained a powerful skill set for efficient application management. Docker's containerization offers resource-efficient and portable solutions, ideal for modern development. This guide's step-by-step approach ensures a strong foundation, enabling you to leverage Docker's benefits effectively.
Embracing Docker's core concepts—containers, images, and repositories—positions you for streamlined development, enhanced collaboration, and agile project delivery. With Docker, you're equipped to accelerate application deployment and adapt to evolving technological demands.
As you continue your Docker journey, explore advanced features, orchestration tools, and community insights. Seamlessly integrating Docker into your workflow empowers you to deliver applications faster, reliably, and with remarkable flexibility.
Unlock the potential of Docker on Ubuntu 22.04, propelling your application deployment, management, and innovation endeavors to new heights.
Thank you for reading this article.