How to Install docker in Ubuntu 18.04 / 16.04

How to install docker in Ubuntu 18.04 and Ubuntu 16.04

Docker is a computer program that performs operating-system-level Virtualization, also known as “Containerization“.

With the help of Docker you can manage your application and all its dependencies together in the form of a docker container. It will ensure that your application works seamlessly in any environment.

A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application.  These containers are also portable so you can easily move them from machine to machine and even share them with others.

Docker comes in two editions: 1) Community Edition (CE) 2) Enterprise Edition (EE). The Docker CE is a free and open source containerisation platform. Docker EE, on the other hand, is a premium version of CE.

You can read here full comparision between Docker CE vs Docker EE.

In this blog, I am going to teach you how to install Docker in Ubuntu 16.04 and Ubuntu 18.04.

1) Prerequisite

To follow this tutorial, you will need the following:

2) Update your apt repository package list by apt-get update

apt-get update downloads the package lists from the repositories and updates them to get information on the newest versions of packages and their dependencies. This ensures you install the latest stable version of any software.

sudo apt-get update

Update repository

3) Download Docker Dependencies

Docker installation package is available in the Official Ubuntu repository. But it may or may not always be the latest version.

The best and recommended approach is to install the latest Docker package from the Docker’s repositories itself. We are also going to follow same approach.

  • First, you will be required to install some packages on your system which let you allow the use of repositories accessed via the HTTP Secure protocol (HTTPS).

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

Download docker dependencies

Here’s a short explanation what each dependency does mean:

  • apt-transport-https: Allows the package manager to transfer files and data over secured https protocol
  • ca-certificates: Allows the system and web browser to verify security certificates
  • curl: Command line tool to transfer data to or from a server, using any supported protocols
  • .software-properties-common: Manage the repositories that you install software from (common)

4) Add Docker’s GPG Key

Add the GPG key of the official Docker repository to your system using the following curl command. It will ensure that the software you are installing is authentic and it will be downloaded from trusted docker server:

  • Import the Docker repository’s GPG key using the below curl command:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Import Docker repository’s GPG key
  • After adding the Docker repository on your Ubuntu system, you have to enable this repository by adding it to the APT sources to install Docker in Ubuntu.

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

The $(lsb_release -cs) command will return LSB (Linux Standard Base) information about your specific Linux distribution, including version number, release codename, and distributor ID.

  • Run the following command to update the repository information:

sudo apt-get update

Update ubuntu repo after docker dependencies

5) Install Docker in Ubuntu 16.04 & 18.04 – Community Edition (CE)

  • To install the latest version of Docker by following command:

sudo apt-get install docker-ce

Command to install docker in ubuntu
  • Once the installation is completed, the Docker service will start automatically. You can verify it by firing following command in terminal:

sudo systemctl status docker

  • The output should be similar to the following, which shows that the service is active and running:
Check Docker service

6) Add user to Docker group to execute command without sudo user

  • The docker commands can only be executed by the root user or by a user who is in the “docker” group. 
  • This group is created during the installation of the Docker CE package. If you try to execute the docker commands without sudo or without being in the docker group, Ubuntu system will throw an error like below:
Docker pull image

It’s a bad practice to allow users with sudo privileges to execute docker commands. The solution is to add username to the docker group. Just replace {username} with username to whom you want to give access:

sudo usermod -aG docker {username}

Add user to docker group

If you are the current logged in user and you have tried to add you in docker group then, you need to relogin in the system to refresh the group membership.

7) Verify installation of Docker

  • Finally, verify that Docker CE is installed properly by running the hello-world image. This is the simplest possible way to verify that you have Docker installed correctly. Just run the hello-world Docker image in a terminal. 

sudo docker run hello-world

This command will download a “hello-world” image. Run it in a container and print a “Hello from Docker” message like the following:

Download & run hello-world image

8) Docker Command Line Interface (CLI)

  • The Docker CLI command syntax structure is as follow:

docker [option] [subcommand] [arguments]

  • You can check all the list of commands available in docker by typing below:

docker


Usage:	docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/home/john/.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/john/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/home/john/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/home/john/.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 view the options available to a specific command, type:

docker [subcommand] –help

You can see below screenshot for reference how you can use subcommand with docker. Second option is to check complete command line instructions from official docker documentation.

9) How to Update Docker CE?

Every software/program releases new updates regularly to their users including security patches, bug fixes or new features. Keeping docker up to date allows you to take advantage of the latest docker features and security updates to keep you productive and secure.

You can upgrade your docker for future releases to latest version by firing following command:

  1. Open terminal, and paste below command in terminal.

sudo apt upgrade docker-ce

  1. You will be needed to enter password as we have fired this command with sudo.
  2. Press ‘y’ and hit enter when the system asks “Do you want to continue? [Y/n]”.
Update docker CE in ubuntu

You have successfully updated Docker on your computer.

10) How to Uninstall Docker CE?

If you might need to uninstall Docker for whatever reason then you can do it easily.

Uninstalling Docker from your computer is quite a straightforward process and just need to fire few commands to uninstall from your system. The commands goes like below:

sudo apt-get purge docker-ce

sudo apt-get autoclean

Uninstall docker CE in ubuntu
  • To remove images, containers, volumes, or customized configuration files on your host that are not automatically removed:

sudo rm -rf /var/lib/docker

sudo rm -rf /etc/docker

  • Remove docker group:

sudo groupdel docker

11) Docker useful Reference and Cheat Sheet links

Here, I am going to share useful docker reference and cheat sheet links. It will help you to grow your skills of using Docker.

  1. Docker command cheat sheet
  2. Overview of Docker compose
  3. Installation of Docker compose
  4. Docker run command
  5. Docker pull command
  6. What is Docker hub?
  7. Create your own docker image from Container and Dockerfile
  8. Docker Tutorial for beginners – Youtube video from edureka

12) Conclusion

Installing Docker on Ubuntu machine is very simple job. We have described in very brief how to install Docker in Ubuntu 16.04 and Ubuntu 18.04 with detailed screenshots and commands with useful reference links.

We have also given steps to update and uninstall application from your system.

I hope this tutorial is really helped you to install Docker application easily in your system.

Did you enjoy this tutorial? Please like and share this post with your friends and colleagues. It will help and motivate us to post articles with quality content.

You May Also Like

About the Author: softwaretrickadmin

I am a professional software developer and founder of softwaretricks.net. I have a passion for troubleshooting and solving software problems on Windows and Linux. I love to write articles for installation steps and troubleshooting software problems. I am always ready to accept challenges!!