How to Install Docker on Ubuntu 18.04


Reading Time: 6 mins.

 

Introduction

Docker, an open-source container-based technology, aims to streamline and manage workload via distributed applications. In simplest form, it’s just another form of Virtualization and the difference lies in the fact that Docker outranks the benefits of the traditional Virtualization almost in every aspect. Further, with the portable and immutable Docker images you can create a Docker container which in turn works in  executing the application. 
To persist data (to share and save), you have the option of Docker volumes that solely aids the back-up process.The Docker service comes in both free (community) and premium (enterprise) tiers. Want to know more about Docker and does it works, go to the following blog article on A Brief Introduction to Docker and Its Terminologies

 

Prerequisites

  1. Ubuntu 18.04 set up with sudo user (non-root) privileges and firewall configuration.
  2. A Docker Hub account (either public or private) to push/pull images to the Docker Hub. 
 

Let’s Start with Docker Installation

 

Step: 1

In case, if you have any older versions of Docker, don’t forget to uninstall it first.
To do so, use the below command,

sudo apt-get remove docker docker-engine docker.io
 

Update your system using the following command:

sudo apt update
 

Next, add the GPG key to your system.

sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
 

Add the Docker repository to APT sources to install them:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
 

We have to update the system using the following command so as to update the newly added repositories.

sudo apt update
 

Make sure you that you install from the Docker repo and not from the default Ubuntu repo:

apt-cache policy docker-ce
 

You’ll see an output like below but the Docker’s version number may be different for you:

 

Output:

docker-ce:
Installed: (none)
Candidate: 18.03.1~ce~3-0~ubuntu
Version table:
18.03.1~ce~3-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
 

Notice that docker-ce is not installed, but the candidate for installation is from the Docker repository for Ubuntu 18.04 (bionic).
After removing the older version, install Docker and Docker-compose using the following commands.

sudo apt install docker-ce

Follow the below step for  docker-compose,
Check the current release, and if necessary, update it using the command below:

sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

Next, set the permissions:

sudo chmod +x /usr/local/bin/docker-compose

Now, it’s time to confirm the successful installation via version check. 

sudo docker-compose --version

Output

docker-compose version 1.21.2, build a133471

Note: If you want to install the specific version of Docker, visit the following link
https://download.docker.com/linux/ubuntu/dists/bionic/pool/stable/amd64/

Step: 2

Checking Docker Status

 

To check whether the Docker is up and running  using the following command.

sudo service docker status
 

Output

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2020-07-31 10:13:26 IST; 4min 3s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 1751 (dockerd)
      Tasks: 13
     Memory: 132.3M
     CGroup: /system.slice/docker.service
             └─1751 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Jul 31 10:13:25 demo@user dockerd[1751]: time="2020-07-31T10:13:25.418272415+05:30" level=warning msg="Your kernel does not support cgroup rt runtime"
Jul 31 10:13:25 demo@user dockerd[1751]: time="2020-07-31T10:13:25.418298375+05:30" level=warning msg="Your kernel does not support cgroup blkio weight"
Jul 31 10:13:25 demo@user dockerd[1751]: time="2020-07-31T10:13:25.418322096+05:30" level=warning msg="Your kernel does not support cgroup blkio weight_device"
Jul 31 10:13:25 demo@user dockerd[1751]: time="2020-07-31T10:13:25.418745549+05:30" level=info msg="Loading containers: start."
Jul 31 10:13:26 demo@user dockerd[1751]: time="2020-07-31T10:13:26.047719198+05:30" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. >
Jul 31 10:13:26 demo@user dockerd[1751]: time="2020-07-31T10:13:26.115412731+05:30" level=info msg="Loading containers: done."
Jul 31 10:13:26 demo@user dockerd[1751]: time="2020-07-31T10:13:26.220296591+05:30" level=info msg="Docker daemon" commit=48a66213fe graphdriver(s)=overlay2 version=19.03.>
Jul 31 10:13:26 demo@user dockerd[1751]: time="2020-07-31T10:13:26.221000688+05:30" level=info msg="Daemon has completed initialization"
Jul 31 10:13:26 demo@user dockerd[1751]: time="2020-07-31T10:13:26.256756632+05:30" level=info msg="API listen on /run/docker.sock"
Jul 31 10:13:26 demo@user systemd[1]: Started Docker Application Container Engine.

Automate Docker
The Docker service needs to be set up to run at startup. To do so, type in each command followed by an enter:

sudo systemctl start docker
sudo systemctl enable docker

Step: 3

Let’s add docker user to sudo group
If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:
For reference, let’s add the user called demo to the docker group using the following command:

sudo usermod -aG docker demo

To verify whether the user has been added to the sudo group, type:

su demo

Note: Once we add the user to docker group, we need not to use sudo for every docker command.

 
 

Let’s Start working with the Docker Commands

Now that we have seen how to install Docker on Ubuntu 18.04, let’s move forward with when and how do we use the Docker commands to make the desirable configurations and to tweak the other required settings.

 

managing-docker-commands

 
 

Step 1: Applying the Docker Commands

In docker, you are supposed to commit a series of options, commands and arguments as the following syntax. 

docker [option] [command] [arguments]

Type docker in order to display all the sub-commands. 

docker

The following are the list of sub-commands currently available as of Docker 18. 
Output

Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
      --config string      Location of client config files (default "/home/adoldev/.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 "/home/adoldev/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/home/adoldev/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/home/adoldev/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit
Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  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 showcase the available options to a specific command, enter

docker docker-subcommand --help

In order to view the Docker’s system-wide information, type

docker info

Next, we start by working with simple Docker images. We will know more about these commands later.

 
 

Step 2: Managing Docker commands

Next, you will be introduced to the basic docker commands.
To view the list of docker images, type:

docker images -a

To view the list of docker containers, type:

docker ps -a

To remove the docker images, type:

docker image rm image_id

Note: If you find any error while removing the docker images, just pass the -f parameter like below:

docker image rm image_id -f

To remove the single docker container, type:

docker rm container_id or name

To remove all the docker containers, type:

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

To remove the docker volumes, type:

docker volume ls
docker volume rm volume_id

To remove the docker networks, type:

docker network ls
docker network rm network_name

To access the docker container shell, type:

docker exec -it container_name bash

To inspect the docker container ,network or image type:

docker inspect container_name or image_name or network_name

Note: If the access is getting denied, make sure that the container is up and running. If the container is found to be inactive, re-run the container using the command below.

docker start container_id

To view the individual docker container’s IP address, type:

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 0861188a6c87(container id)
 
 

Conclusion

In this article, we have installed Docker on Ubuntu 18.04. Docker, an open-source software aids in streamlining the process of managing and maintaining workloads via distributed applications. To know more about the docker configuration, visit, https://docs.docker.com/get-started/overview. You have also come across the necessary commands that are required to perform the Docker configuration along with defining one such Docker syntax followed by passing the code on your system. 

 
 
 

Leave a Comment

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

Scroll to Top