Install and Use Docker on Ubuntu 22.04 | Easy and Full Setup

Posted on

Install and Use Docker on Ubuntu 22.04 | Easy and Full Setup

In this guide, we want to teach you How To Install and Use Docker on Ubuntu 22.04. Docker is an open-source platform that enables developers to build, deploy, run, update, and manage containers – standardized, executable components that combine application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.

You can now proceed to the guide steps below on the Orcacore website to complete the Install and Use Docker on Ubuntu 22.04 setup.

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on the Initial Server Setup with Ubuntu 22.04.

Also, in the article, you will need to create an account on Docker Hub to create your images and push them to Docker Hub.

1. Download and Add the Docker Repository Key

First, you need to update your local package index with the command below:

sudo apt update

Then, you need to install a few required packages that let apt use packages over HTTPS, run the following command:

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

Now you should add the GPG key for the official Docker repository to your system with the following command:

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

Next, use the command below to add the Docker repo on Ubuntu 22.04:

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 local package index again:

sudo apt update

2. Installing Docker on Ubuntu 22.04

At this point, you need to be sure about installing from the Docker repository instead of the default Ubuntu repository:

apt-cache policy docker-ce

In your output, you will see something like this:

Check Docker Candidate Information on Ubuntu

Now the docker-ce candidates to install. Install Docker on Ubuntu 22.04 with the following command:

sudo apt install docker-ce -y

When the installation is completed, check that it is active and running on Ubuntu 22.04 with the following command:

sudo systemctl status docker

Your output should be similar to this:

Verify Docker is active and running

Now you have installed Docker on Ubuntu 22.04. It will give you the Docker service, as well as the Docker command-line utility.

Add User To Docker Group

As you know, you need sudo privileges to run Docker commands. Or if you don’t want to use sudo, you can add a user to the docker group. In this way, the user can run commands without using sudo.

To add a user to the Docker group, run the following command:

sudo usermod -aG docker ${USER}

You can log out and back into the server with the same user.

If you want to add a user that you aren’t logged in with, run the following command:

sudo usermod -aG docker <span>username</span>

For the rest of the article, we run commands as a user in the docker group.

3. How to Use the Docker Command on Ubuntu 22.04?

At this point, you have installed Docker on Ubuntu 22.04. Now let’s know about the Docker command.

The syntax of using the Docker command is like this:

docker [option] [command] [arguments]

You can list all available subcommands by typing the following command:

docker

Your output should be like this:

<span><strong>Output</strong>
 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(s) 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

Management Commands:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.9.1-docker)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  scan*       Docker Scan (Docker Inc., v0.17.0)
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  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
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  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
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  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
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  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
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

To get more information, you can run the following command:

docker docker-subcommand --help

To view system-wide information about Docker, use:

docker info

When you get to know about the Docker command on Ubuntu 22.04. Let’s see how to work with them.

Work with Docker Images

Docker containers are built from Docker images. By default, Docker pulls these images from Docker Hub. Anyone can host their Docker images on Docker Hub, so most applications and Linux distributions you’ll need will have images hosted there.

To check that you can access and download images from Docker Hub, run the following command:

docker run hello-world

Your output should look like this:

<strong><span>Output</span></strong>
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:62af9efd515a25f84961b70f973a798d2eca956b1b2b026d0a4a63a3b0b6a3f2
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)
...

Now you can search for available images on Docker Hub, for example, run the following command to search for Ubuntu images:

docker search ubuntu

In the output, you will see something similar to this:

<strong><span>Output</span></strong>
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                           Ubuntu is a Debian-based Linux operating sys&acirc;&#128;&brvbar;   15005     [OK]
websphere-liberty                WebSphere Liberty multi-architecture images &acirc;&#128;&brvbar;   289       [OK]
ubuntu-upstart                   DEPRECATED, as is Upstart (find other proces&acirc;&#128;&brvbar;   112       [OK]
neurodebian                      NeuroDebian provides neuroscience research s&acirc;&#128;&brvbar;   93        [OK]
ubuntu/nginx                     Nginx, a high-performance reverse proxy &amp; we&acirc;&#128;&brvbar;   61
open-liberty                     Open Liberty multi-architecture images based&acirc;&#128;&brvbar;   55        [OK]
ubuntu-debootstrap               DEPRECATED; use "ubuntu" instead                46        [OK]
...

Here, when you choose the image that you want to use, you can run the following command to download the image:

docker pull ubuntu

You will see the following output:

<strong><span>Output</span></strong>
Using default tag: latest
latest: Pulling from library/ubuntu
2b55860d4c66: Pull complete
Digest: sha256:20fa2d7bb4de7723f542be5923b06c4d704370f0390e4ae9e1c833c8785644c1
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

After you downloaded the image, you can run a container with that image with the “run” subcommand. If an image wasn’t downloaded, the “run” subcommand downloaded it first, then ran the container using it.

To see images you have downloaded, run the following command:

docker images

You will see something like this:

<strong><span>Output</span></strong>
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
ubuntu        latest    2dc39ba059dc   3 weeks ago     77.8MB
hello-world   latest    feb5d9fea6a5   12 months ago   13.3kB

At this point, you learn how to work with Docker images on Ubuntu 22.04. Now let’s see how to run Docker containers.

Run Docker Containers

Here you can run a container using the latest image of Ubuntu with the following command:

docker run -it ubuntu
  • Note: -it switches give you interactive shell access into the container.

Your command prompt should change to this form:

<span>root@1233a80158d9:/#</span>
  • Important Note: Remember the container ID in the command prompt. In this example, the container ID is 1233a80158d9.

Now you can run any commands that you want. You don’t need to use sudo for your commands because you run commands as root inside the container.

<span>root@1233a80158d9:/#</span> apt update

Here you can install any application. Here we install Node.js with the following command:

<span>root@1233a80158d9:/#</span> apt install nodejs
  • Note: Any changes you make inside the container only apply to that container.

You can exit from the container with the following command:

<span>root@1233a80158d9:/#</span> exit

When you are finished running Docker containers on Ubuntu 22.04, let’s see how to manage them.

Manage Docker containers

You can see active and running containers with the following command:

docker ps
<strong><span>Output</span></strong>
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

To list all containers, both active and inactive, use the following command;

docker ps -a

In your output, you will see:

<strong><span>Output</span></strong>
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                          PORTS     NAMES
1233a80158d9   ubuntu        "bash"     4 minutes ago   Exited (0) About a minute ago             nostalgic_volhard
66e80faf3913   hello-world   "/hello"   7 minutes ago   Exited (0) 7 minutes ago                  amazing_payne

You can see the containers you created with:

docker ps -l
<strong><span>Output</span></strong>
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS                     PORTS     NAMES
1233a80158d9   ubuntu    "bash"    4 minutes ago   Exited (0) 2 minutes ago             nostalgic_volhard

Also, you can start an existing container with a container ID. For example:

docker start <span>1233a80158d9</span>

Then you can use the docker ps command to see its status, your output should be similar to this:

<strong><span>Output</span></strong>
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS          PORTS     NAMES
1233a80158d9   ubuntu    "bash"    5 minutes ago   Up 19 seconds             nostalgic_volhard

To stop a running container, you can run the following command with the container ID or name:

docker stop <span>nostalgic_volhard</span>

If you want to remove a container, you can run the following command with the name of that container. For example, to remove the hello-world, you run the following command:

docker rm <span>amazing_payne</span>

At this point, you learn how to manage Docker containers on Ubuntu 22.04 too.

Let’s see how Containers can be turned into images that you can use to build new containers.

4. Commit Changes in a Container to a Docker Image

In this part, you learn how to save the state of a container as a new Docker image.

After installing Node.js inside the Ubuntu container, you now have a container running off an image, but the container is different from the image you used to create it. But you might want to reuse this Node.js container as the basis for new images later.

Here you can commit the changes to a new Docker image with the following command:

docker commit -m "<span>What you did to the image</span>" -a "<span>Author Name</span>" <span>container_id</span> <span>repository</span>/<span>new_image_name</span>

For example, for the user Daniel, with the container ID of 1233a80158d9:

docker commit -m "<span>added Node.js</span>" -a "<span>daniel</span>" <span>1233a80158d9</span> <span>daniel/ubuntu-nodejs</span>

Then you can see your available images with the following command:

docker images

In your output, you will see:

<strong><span>Output</span></strong>
REPOSITORY             TAG       IMAGE ID       CREATED          SIZE
daniel/ubuntu-nodejs   latest    2d77de4a5b0a   12 seconds ago   199MB
ubuntu                 latest    2dc39ba059dc   3 weeks ago      77.8MB
hello-world            latest    feb5d9fea6a5   12 months ago    13.3kB

5. How to push Docker images to a Docker repository?

After you create a new image from an existing image, you may want to share it with a few of your friends, the whole world on Docker Hub, or other Docker registries that you have access to. To push an image to Docker Hub or any other Docker registry, you must have an account there.

To have an account on Docker Hub, you need to register on Docker Hub.

If you want to log in to the Docker hub, you will be asked for authentication :

docker login -u <span>docker-registry-username</span>

If you enter the correct password, authentication should succeed. Then you may push your image using the following command:

docker push <span>docker-registry-username</span>/<span>docker-image-name</span>

For example:

docker push daniel/ubuntu-nodejs

It will take a little time to complete. After you push an image to a registry, it should be listed on your account’s dashboard.

  • Note: If a push attempt results in an error of this sort, log in, then repeat the push attempt.

Conclusion

In conclusion, setting up Install and Use Docker on Ubuntu 22.04 is straightforward. It involves updating your system, installing required dependencies, adding Docker’s official repository, and then installing Docker Engine. Once installed, Docker allows you to easily run and manage containers, making it a powerful tool for application development and deployment. This guide covered the essentials for Install and Use Docker on Ubuntu 22.04.

Hope you enjoy it. You may also like these articles:

How To Install Grafana on Ubuntu 22.04

How To Enable BBR on Debian 11

Introduce Ubuntu 25.04 Plucky Puffin

Install Apache Guacamole on Ubuntu 22.04

Install NoMachine on Ubuntu 22.04

Alternative Solutions for Installing Docker on Ubuntu 22.04

While the method described above using the official Docker repository is the most common and recommended way to install Docker, there are alternative approaches. These may be useful in specific scenarios, such as when you need a specific version of Docker or when you’re working in an environment with restricted access. Here are two different ways you can Install and Use Docker on Ubuntu 22.04:

1. Installing Docker using Docker’s Convenience Script

Docker provides a convenience script that automates the installation process. This script is designed for quick and easy installation, but it’s crucial to understand what the script does before running it, as it might not be suitable for production environments.

Explanation:

This script automates the process of adding the Docker repository, downloading the necessary packages, and installing Docker. It’s a one-liner that can quickly set up Docker on your system. However, it’s essential to be aware that using convenience scripts can sometimes introduce security risks, as you’re essentially running code from the internet without inspecting it.

Code Example:

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

Elaboration:

  1. Download the script: curl -fsSL https://get.docker.com -o get-docker.sh downloads the installation script from Docker’s official website and saves it as get-docker.sh.
  2. Execute the script: sudo sh get-docker.sh executes the downloaded script with superuser privileges. The script will automatically add the Docker repository, update the package index, and install the latest version of Docker.

After running the script, you can verify the installation by checking the Docker version:

docker --version

While this method is faster than the manual approach, it’s crucial to be cautious and understand the potential risks involved in running scripts from untrusted sources.

2. Installing Docker from Snap Package

Snap is a package management system developed by Canonical (the company behind Ubuntu). It allows you to install applications in a sandboxed environment, which can provide better security and isolation. Docker is available as a Snap package, which can be installed on Ubuntu 22.04.

Explanation:

Using Snap packages offers several advantages, including automatic updates and dependency management. Snap packages are also isolated from the rest of the system, which can enhance security. However, Snap packages can sometimes be larger and slower than traditional .deb packages.

Code Example:

sudo snap install docker

Elaboration:

  1. Install Docker: sudo snap install docker installs the Docker Snap package from the Snap Store. The Snap package includes all the necessary dependencies and configurations to run Docker on your system.

After the installation is complete, you might need to start the Docker service and configure it to start automatically on boot:

sudo snap start docker
sudo snap enable docker

You might also need to set up the Docker group and add your user to it, as described in the original article. Keep in mind that the snap installation might have slightly different commands or configurations for user access rights.

These alternative methods offer different ways to Install and Use Docker on Ubuntu 22.04, each with its own advantages and disadvantages. Choosing the right method depends on your specific needs and preferences. Remember to always prioritize security and understand the implications of each approach before proceeding with the installation.

Leave a Reply

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