How to Install Docker and Run Docker Containers in Ubuntu

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their software, libraries, and configuration files -- they can communicate with each other through well-defined channels. Because all of the containers share the services of a single operating system kernel, they use fewer resources than virtual machines.

Installing Docker CE

First, check and remove if older versions of Docker are running.

apt-get remove docker docker-engine containerd runc

Next, using the following commands set up the Docker repository to install and update Docker.

apt-get update
curl -fsSL | sudo apt-key add -
add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"


root@vps:~# apt-get update
Hit:1 hirsute InRelease
Hit:2 hirsute-updates InRelease
Hit:3 hirsute-backports InRelease
Hit:4 hirsute-security InRelease
Reading package lists... Done
root@vps:~# curl -fsSL | sudo apt-key add -
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
root@vps:~# add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"
Repository: 'deb [arch=amd64] hirsute stable'
Archive for codename: hirsute components: stable
More info:
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel.
Adding deb entry to /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-hirsute.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/archive_uri-https_download_docker_com_linux_ubuntu-hirsute.list
Hit:1 hirsute InRelease
Get:2 hirsute InRelease [43.3 kB]
Hit:3 hirsute-updates InRelease
Hit:4 hirsute-backports InRelease
Hit:5 hirsute-security InRelease
Get:6 hirsute/stable amd64 Packages [3,434 B]
Fetched 46.8 kB in 1s (88.0 kB/s)
Reading package lists... Done

And check for the update for the apt package and install the latest version of Docker CE

apt-get update
apt-get install docker-ce docker-ce-cli

After installation has been completed the service should be auto-started and auto-enabled -- to check status please enter the following command.

systemctl status docker 


root@vps:~# systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-07-17 07:12:55 UTC; 1min 20s ago
TriggeredBy: ● docker.socket
   Main PID: 52038 (dockerd)
      Tasks: 8
     Memory: 41.4M
     CGroup: /system.slice/docker.service
             └─52038 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Jul 17 07:12:55 dockerd[52038]: time="2021-07-17T07:12:55.154119993Z" level=warning msg="Your kernel does not su>
Jul 17 07:12:55 dockerd[52038]: time="2021-07-17T07:12:55.154422694Z" level=warning msg="Your kernel does not su>
Jul 17 07:12:55 dockerd[52038]: time="2021-07-17T07:12:55.154564671Z" level=warning msg="Your kernel does not su>
Jul 17 07:12:55 dockerd[52038]: time="2021-07-17T07:12:55.154881595Z" level=info msg="Loading containers: start."
Jul 17 07:12:55 dockerd[52038]: time="2021-07-17T07:12:55.317432819Z" level=info msg="Default bridge (docker0) i>
Jul 17 07:12:55 dockerd[52038]: time="2021-07-17T07:12:55.432417801Z" level=info msg="Loading containers: done."
Jul 17 07:12:55 dockerd[52038]: time="2021-07-17T07:12:55.500714286Z" level=info msg="Docker daemon" commit=b0f5>
Jul 17 07:12:55 dockerd[52038]: time="2021-07-17T07:12:55.501099054Z" level=info msg="Daemon has completed initi>
Jul 17 07:12:55 systemd[1]: Started Docker Application Container Engine.
Jul 17 07:12:55 dockerd[52038]: time="2021-07-17T07:12:55.557001096Z" level=info msg="API listen on /run/docker.>
lines 1-21/21 (END)

To verify that Docker CE is installed properly run the following command.

docker run hello-world

NOTE: The hello-world image will download if that image is not available on your server.


root@vps:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
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:

For more examples and ideas, visit:


Basic Docker Commands in Ubuntu

To check Docker information.

docker info


root@vps:~# docker info
 Context:    default
 Debug Mode: false
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 1
 Server Version: 20.10.7
 Storage Driver: overlay2
  Backing Filesystem: extfs

To get Docker Version.

docker version


root@vps:~# docker version
Client: Docker Engine - Community
 Version:           20.10.7
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        f0df350
 Built:             Wed Jun  2 12:00:45 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
  Version:          20.10.7
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       b0f5bc3
  Built:            Wed Jun  2 11:58:56 2021
  OS/Arch:          linux/amd64
  Experimental:     false

To list all available Docker commands.



root@vps:~# docker

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

      --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*     Build with BuildKit (Docker Inc., v0.5.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.8.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

Download a Docker Image

Note: you will need to download an image from Docker Hub to run a Docker container – Docker provides free images from its repositories.

For example to list all available images from CentOS.

docker search centos


root@vps:~# docker search centos
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                            The official build of CentOS.                   6640      [OK]
ansible/centos7-ansible           Ansible on Centos7                              134                  [OK]
consol/centos-xfce-vnc            Centos container with "headless" VNC session…   129                  [OK]
jdeathe/centos-ssh                OpenSSH / Supervisor / EPEL/IUS/SCL Repos - …   118                  [OK]
centos/systemd                    systemd enabled base container.                 100                  [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   89
imagine10255/centos6-lnmp-php56   centos6-lnmp-php56                              58                   [OK]
tutum/centos                      Simple CentOS docker image with SSH access      48
centos/httpd-24-centos7           Platform for running Apache httpd 2.4 or bui…   40
jdeathe/centos-ssh-apache-php     Apache PHP - CentOS.                            31                   [OK]
kinogmt/centos-ssh                CentOS with SSH                                 29                   [OK]
guyton/centos6                    From official centos6 container with full up…   10                   [OK]
nathonfowlie/centos-jre           Latest CentOS image with the JRE pre-install…   8                    [OK]
centos/tools                      Docker image that has systems administration…   7                    [OK]
drecom/centos-ruby                centos ruby                                     6                    [OK]
mamohr/centos-java                Oracle Java 8 Docker image based on Centos 7    3                    [OK]
darksheer/centos                  Base Centos Image -- Updated hourly             3                    [OK]
amd64/centos                      The official build of CentOS.                   2
indigo/centos-maven               Vanilla CentOS 7 with Oracle Java Developmen…   2                    [OK]
dokken/centos-7                   CentOS 7 image for kitchen-dokken               2
ovirtguestagent/centos7-atomic    The oVirt Guest Agent for Centos 7 Atomic Ho…   2
mcnaughton/centos-base            centos base image                               1                    [OK]
blacklabelops/centos              CentOS Base Image! Built and Updates Daily!     1                    [OK]
smartentry/centos                 centos with smartentry                          0                    [OK]
starlabio/centos-native-build     Our CentOS image for native builds              0                    [OK]

You can download Image locally to operate as per your requirements.

docker pull "image name here"


Note: Here we are using CentOS image.

root@vps:~# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest

To list the available image on your server.

docker images


root@vps:~# docker images
hello-world   latest    d1165f221234   4 months ago   13.3kB
centos        latest    300e315adb2f   7 months ago   209MB

You can remove the image using the following command.

docker rmi centos


root@vps:~# docker rmi centos
Untagged: centos:latest
Untagged: centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Deleted: sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55
Deleted: sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859
root@vps:~# docker images
hello-world   latest    d1165f221234   4 months ago   13.3kB

Run a Docker Container

To run the container you need to get the Container ID or Name -- enter the following command to get the required information.

docker ps -l


CONTAINER ID   IMAGE     COMMAND            CREATED          STATUS                      PORTS     NAMES
1bfc2c8c9874   centos    "cat /etc/issue"   18 seconds ago   Exited (0) 17 seconds ago             busy_moore

Once you have all the information which requires to run the container - enter the following command to run the container.

docker start "CONTAINER ID"


root@vps:~# docker start 1bfc2c8c9874

To stop container

docker stop "CONTAINER ID"


root@vps:~# docker stop 1bfc2c8c9874

You can give a unique name for each container by using the --name.

Note: You can Start and Stop the container using this unique name!!

docker run --name "Custom_name_here" centos


root@vps:~# docker run --name proj1 centos
root@vps:~# docker ps -l
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
c56f1f01dacb   centos    "/bin/bash"   4 seconds ago   Exited (0) 3 seconds ago             proj1

To run Linux commands into a container enter the following command.

docker run -it centos bash


[root@8784d53f513b /]# cat /etc/os-release
NAME="CentOS Linux"
ID_LIKE="rhel fedora"
PRETTY_NAME="CentOS Linux 8"
[root@8784d53f513b /]#

To exit the container.



[root@8784d53f513b /]# exit