In this article, We are going to perform How to Install Docker on Ubuntu 19.10/18.04/16.04 LTS or any other cloud platform like Amazon EC2, Azure VM, Google Compute Engine, etc.
Table of Contents
What is Docker ?
Docker is a software containerization platform that packages your application, all its libraries and configuration files in the form of a docker container where you can develop, ship and execute the applications.
In addition to it, Docker provides a robust client-server application architecture with a powerful server, REST API and command-line interface client. You can use the same infrastructure to run more containers and utilize fewer resources.
Docker can be installed on any operating system, it can be Mac, Windows, Linux and even cloud. It works only on 64 bit Linux installation and needs a Linux kernel of 3.10 and a higher version
Prerequisites:
- Ubuntu 19.10/18.04/16.04 LTS with 64-bit.
- Kernel version >= 3.10 or higher.
Check Kernel Version
Use below command to check kernel version,
$ uname -r
Output:
5.0.0-1028
Check Ubuntu OS Version:
To check ubuntu OS version enter below command
cat /etc/os-release NAME="Ubuntu" VERSION="18.04.3 LTS (Bionic Beaver)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 18.04.3 LTS" VERSION_ID="18.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=bionic
Install docker on Ubuntu
Docker is available in two editions i.e. Enterprise and Community, here we are installing community edition which is free of cost.
Install docker using below command
$ sudo apt-get install -y docker.io
OR
You can install docker by script as shown below
$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sh get-docker.sh
OR
Install Docker from APT Repository
Update the system packages
$ sudo apt-get update
Install below prerequisite packages
$ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Add below Docker’s official GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the Docker APT repository to your system
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Update the Docker Repository Information
$ sudo apt-get update
Install Docker on Ubuntu
$ sudo apt-get install docker-ce
Once installed verify Docker Service status
$ sudo systemctl status docker
Output:
docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2020-03-22 07:43:44 UTC; 32s ago
Docs: https://docs.docker.com
Main PID: 118265 (dockerd)
Tasks: 10
CGroup: /system.slice/docker.service
└─118265 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Running Docker commands without sudo
By default, Docker requires administrator privileges, Docker group is created when during the installation of Docker packages.
Add your system user to docker group.
$ sudo usermod -aG docker $USER
Docker Command-Line
Before moving to Docker Images, Let’s see Docker commands. Before are docker command syntax
$ docker [option] [command] [arguments]
Run docker command to view all available subcommads.
$ docker
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.
Working with Docker Images
Once docker is installed, Lets test docker by testing docker images
Docker images are required for establishing Docker containers. These images can be used for linking to any of the Docking environment.
Before running docker images, check by accessing hello-world image from Docker Hub
$ docker run hello-world
Error:
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
Change the docker.sock permission
$ sudo chmod 666 /var/run/docker.sock
Output:
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:f9dfddf63636d84ef479d645ab5885156ae030f611a56f3a7ac7f2fdd86d7e4e Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly.
Above output shows we have installed docker correctly.
$ docker search ubuntu
Output:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating sys… 10657 [OK] dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 408 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 244 [OK] consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 212 [OK] ubuntu-upstart Upstart is an event-based replacement for th… 107 [OK] neurodebian NeuroDebian provides neuroscience research s… 67 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK] u-debootstrap debootstrap --variant=minbase --components=m… 43 [OK] nuagebec/ubuntu Simple always updated Ubuntu docker images w… 24 [OK] i386/ubuntu Ubuntu is a Debian-based Linux operating sys… 19 1and1internet/ubuntu-16-apache-php-5.6 ubuntu-16-apache-php-5.6 14 [OK] 1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 13 [OK] ppc64le/ubuntu Ubuntu is a Debian-based Linux operating sys… 13 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10 ubuntu-16-nginx-php-phpmyadmin-mariadb-10 11 [OK] 1and1internet/ubuntu-16-nginx-php-5.6 ubuntu-16-nginx-php-5.6 8 [OK] 1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 7 [OK] 1and1internet/ubuntu-16-apache-php-7.1 ubuntu-16-apache-php-7.1 6 [OK] darksheer/ubuntu Base Ubuntu Image -- Updated hourly 5 [OK] pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc… 4 1and1internet/ubuntu-16-nginx-php-7.0 ubuntu-16-nginx-php-7.0 4 [OK] pivotaldata/ubuntu16.04-build Ubuntu 16.04 image for GPDB compilation 2 smartentry/ubuntu ubuntu with smartentry 1 [OK] 1and1internet/ubuntu-16-php-7.1 ubuntu-16-php-7.1 1 [OK] pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 1 1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK]
Above output show all available Ubuntu images,download the base image to your server.
$ docker pull ubuntu
Output:
Using default tag: latest latest: Pulling from library/ubuntu 5bed26d33875: Pull complete f11b29a9c730: Pull complete 930bda195c84: Pull complete 78bf9a5ad49e: Pull complete Digest: sha256:bec5a2727be7fff3d308193cfde3491f8fba1a2ba392b7546b43a051853a341d Status: Downloaded newer image for ubuntu:latest docker.io/library/ubuntu:latest
This command downloads an image to your server from docker registry/DockerHub.
$ docker images
You will see Ubuntu images is downloaded.
REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 4e5021d210f6 2 days ago 64.2MB hello-world latest fce289e99eb9 14 months ago 1.84kB
Remove Docker Images
Syntax:
$ docker rmi <REPOSITRY Name /IMAGE ID >
Example:
$ docker rmi ubuntu
Working with Docker Container
Docker Containers are lightweight and it uses the host OS and it included software package that consists of all the dependencies required to run an application.
Lets launch container with Ubuntu docker image.
$ docker run -i -t ubuntu /bin/bash
-i – To start an interactive session.
-t – Allocates a tty and attaches stdin and stdout.
ubuntu– Docker image that is used to create the container.
bash (or /bin/bash)– command that is running inside the Ubuntu container.
Note- The container will stop when you leave it with the command exit. If you like to have a container that is running in the background, you just need to add the -d option in the command
OR
To exit from docker container type CTRL + P + Q. This will leave container running in background an provide you host system console.
Now Run Docker Container in background.
$ docker run -i -t --name=Ubuntu-Linux -d ubuntu /bin/bash
Output:
docker run -i -t --name=Ubuntu-Linux -d ubuntu /bin/bash 023828e786e025f594adceea1405afe80e0aaf055f29b46592343e86535bd9df
To verify Docker Container running in background
$ docker ps
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 023828e786e0 ubuntu "/bin/bash" 2 minutes ago Up 2 minutes Ubuntu-Linux
Access the shell of Docker Container that runs in the background mode using “CONTAINER ID”
$ docker exec -i -t 023828e786e0 /bin/bash
Output:
docker exec -i -t 023828e786e0 /bin/bash root@023828e786e0:/#
Access the shell of Docker Container that runs in the background mode using “NAMES”
$ docker exec -i -t Ubuntu-Linux /bin/bash
Type “Exit” to exit from Docker Container shell.
To update the System Packages of Docker Container
$ docker exec 023828e786e0 apt-get update
Let’s install Apache2 in docker container
$ docker exec 023828e786e0 apt-get install apache2 -y
To check apache2 service status inside Docker Container
$ docker exec 023828e786e0 service apache2 status
Output:
docker exec 023828e786e0 service apache2 status * apache2 is not running
Start Apache2 service inside Docker Container
$ docker exec 023828e786e0 service apache2 start
Expose Docker Container ports and access Apache outside
$ docker run -p 81:80 -itd 4e5021d210f6
-p – Exposes the host port to container port
To verify Apache is accessing from outside, Open your favourite browser , type the IP address of your system IP followed by port 81
http://SystemIP:81/
To list all Docker Containers including stopped
$ docker ps -a
To view Logs for a Docker Container
$ docker logs <Containe ID>
To rename Docker Container
$ docker rename <Old_Name> <New_Name>
To Start/Stop Container
$ docker stop <CONTAINER ID> $ docker start <CONTAINER ID>
To remove the Docker Container, stop it first and then remove it
$ docker rm <CONTAINER ID>
Run below command to remove all stopped containers
sudo docker rm -f $(sudo docker ps -a -q)
To remove untagged docker images
sudo docker images | grep none | awk '{ print $3; }' | xargs sudo docker rmi
Uninstall Docker From Ubuntu
To completely remove Docker from Ubuntu, Run below commands,
$ sudo apt-get purge docker-ce
$ sudo rm -rf /var/lib/docker
Conclusion
In this article, We have covered Install Docker on Ubuntu, Docker Commands, Docker Image, Docker Container and performed various docker operation commands and uninstalling docker from Ubuntu.
Related Articles
Docker Installation
How to Install Docker on Windows 10
Dockerfile Instructions
Dockerfile Instructions with Examples
Docker Image
How to Create Docker Image for Node JS Application [2 Steps]
Shell Script to Build Docker Image [2 Steps]
Docker Compose
How to Create Docker Image for Node JS Application [2 Steps]
Docker Commands
100 Docker Basic Commands with Examples
81 Docker Command Cheat Sheet in Image and PDF Format
Docker Interview Questions and Answers for Freshers
50 Real Time Docker Interview Questions and Answers
Reference