How To Install and Use Docker on Ubuntu 22.04

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.

Steps To Install and Use Docker on Ubuntu 22.04

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 the Initial Server Setup with Ubuntu 22.04.

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

Download and Add the Docker Repository key on Ubuntu 22.04

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 which let apt use packages over HTTPS, run the following command:

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

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

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:

Output
docker-ce:
  Installed: (none)
  Candidate: 5:20.10.18~3-0~ubuntu-jammy
  Version table:
     5:20.10.18~3-0~ubuntu-jammy 500
        500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
     5:20.10.17~3-0~ubuntu-jammy 500
...

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

sudo apt install docker-ce

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

sudo systemctl status docker

Your output should be similar to this:

Output
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset>
     Active: active (running) since Thu 2022-09-29 06:30:04 UTC; 7s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 3312 (dockerd)
      Tasks: 8
     Memory: 23.0M
        CPU: 376ms
     CGroup: /system.slice/docker.service
             └─3312 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/cont

Now you have installed Docker on Ubuntu 22.04. It will give you the Docker service, Also 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 username

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

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:

Output  
 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 help with docker, check out our guides at https://docs.docker.com/go/guides/

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 seem like this:

Output
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 ubuntu images:

docker search ubuntu

In the output you will see something similar to this:

Output
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                           Ubuntu is a Debian-based Linux operating sys…   15005     [OK]
websphere-liberty                WebSphere Liberty multi-architecture images …   289       [OK]
ubuntu-upstart                   DEPRECATED, as is Upstart (find other proces…   112       [OK]
neurodebian                      NeuroDebian provides neuroscience research s…   93        [OK]
ubuntu/nginx                     Nginx, a high-performance reverse proxy & we…   61
open-liberty                     Open Liberty multi-architecture images based…   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:

Output
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 run the container using it.

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

docker images

You will see something like this:

Output
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:

root@1233a80158d9:/#

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 a root inside the container.

root@1233a80158d9:/# apt update

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

root@1233a80158d9:/# apt install nodejs

Note: Any changes you make inside the container only apply to that container.

You can exit from the container by the following command:

root@1233a80158d9:/# 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
Output
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:

Output
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
Output
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 1233a80158d9

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

Output
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 container ID or name:

docker stop nostalgic_volhard

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 amazing_payne

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.

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 "What you did to the image" -a "Author Name" container_id repository/new_image_name

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

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

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

docker images

In your output you will see:

Output
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

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 at Docker Hub.

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

docker login -u docker-registry-username

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

docker push docker-registry-username/docker-image-name

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, login, then repeat the push attempt.

Conclusion

At this point, you have learned to Install and Use Docker on Ubuntu 22.04.

Hope you enjoy it.

You may be like these articles:

How To Install Grafana on Ubuntu 22.04

How To Enable BBR on Debian 11

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular