How to Install and Use Docker on Ubuntu 18.04

How to Install and Use Docker on Ubuntu 18.04
How to Install and Use Docker on Ubuntu 18.04

Install and Use Docker on Ubuntu 18.04

Docker is an application used to manage application processes in containers. Containers run applications in resource-isolated process. By using docker you can build, test and deploy applications that can run anywhere as portable and self-sufficient containers. In this tutorial, you are going to learn how to install and use Docker on Ubuntu 18.04.

Prerequisites

Before you start to install Docker on Ubuntu 18.04. You must have the root user account credentials of your system.

Install Docker on Ubuntu

Here we will install Docker on Ubuntu from Latest Docker’s Repository. Follow the steps below to install docker on Ubuntu.

Update the apt package manager index and upgrade the system typing following command:

sudo apt update && sudo apt upgrade

Run below command to install dependencies required to enable Docker repository:

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

Now Import the GPG key for repository using following curl command:

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

Finally, add the repository to your system typing following in the terminal:

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

Now update package manager index again typing following command:

sudo apt update

Install Docker Community Version typing following command:

sudo apt install docker-ce

Confirm the installation and check the version of Docker using following command:

docker -v

The output should be:

Docker version 18.09.0-ce, build 9ee9f40

You can check the Docker service status using the following systemctlcommand:

sudo systemctl status docker

The output should be:

● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-13-19 20:22:00 PDT; 6min ago
     Docs: https://docs.docker.com
 Main PID: 10647 (dockerd)
    Tasks: 21
   CGroup: /system.slice/docker.service

Executing the Docker Command Without Sudo

By default, docker command can only run by sudo user or member of docker group which created while installing Docker. If you want to run docker command without sudo then you should add the user to docker group.

If you are already logged in then to add the user to docker group run below command:

sudo usermod -aG docker $USER

To apply the membership run below command or you can log out and log in again:

su - $USER

To confirm, you have added to docker group run following command:

id -nG

The output should be:

linux4one sudo docker

How to Use Docker Command

Following is the basic syntax for docker command:

docker [option] [subcommand] [arguments]

List all the available subcommands typing following command:

docker

Get help for any docker subcommand using the following command:

docker docker-subcommand --help

Working with Docker Images

Docker containers are built from Docker Images and these images are pulled from Docker Hub which is a registry managed by Docker Company. On the Docker Hub, anyone can host their images, because of this most of the app images and Linux distro’s are already available on Docker Hub.

Search Docker Images

You can search for an image on Docker Hub by using docker command with search sub-command.

Search an Ubuntu image typing following in command line:

docker search ubuntu

The output should be similar to:

NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   7917                [OK]
dorowu/ubuntu-desktop-lxde-vnc                            Ubuntu with openssh-server and NoVNC            193                                     [OK]
rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of offi…   156                                     [OK]
ansible/ubuntu14.04-ansible                               Ubuntu 14.04 LTS with ansible                   93                                      [OK]
ubuntu-upstart                                            Upstart is an event-based replacement for th…   87                  [OK]
neurodebian                                               NeuroDebian provides neuroscience research s…   50                  [OK]
ubuntu-debootstrap                                        debootstrap --variant=minbase --components=m…   38                  [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5      ubuntu-16-nginx-php-phpmyadmin-mysql-5          36                                      [OK]
nuagebec/ubuntu                                           Simple always updated Ubuntu docker images w…   23                                      [OK]
tutum/ubuntu                                              Simple Ubuntu docker images with SSH access     18
i386/ubuntu                                               Ubuntu is a Debian-based Linux operating sys…   13
ppc64le/ubuntu                                            Ubuntu is a Debian-based Linux operating sys…   12
1and1internet/ubuntu-16-apache-php-7.0                    ubuntu-16-apache-php-7.0                        10                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10   ubuntu-16-nginx-php-phpmyadmin-mariadb-10       6                                       [OK]
eclipse/ubuntu_jdk8                                       Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, …   6                                       [OK]
codenvy/ubuntu_jdk8                                       Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, …   4                                       [OK]
darksheer/ubuntu                                          Base Ubuntu Image -- Updated hourly             4                                       [OK]
1and1internet/ubuntu-16-apache                            ubuntu-16-apache                                3                                       [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4         ubuntu-16-nginx-php-5.6-wordpress-4             3                                       [OK]
1and1internet/ubuntu-16-sshd                              ubuntu-16-sshd                                  1                                       [OK]
pivotaldata/ubuntu                                        A quick freshening-up of the base Ubuntu doc…   1
1and1internet/ubuntu-16-healthcheck                       ubuntu-16-healthcheck                           0                                       [OK]
pivotaldata/ubuntu-gpdb-dev                               Ubuntu images for GPDB development              0
smartentry/ubuntu                                         ubuntu with smartentry                          0                                       [OK]
ossobv/ubuntu
...

You can see all images comes with version number still if the version number is not specified at that time docker picks the latest version available.

Download Docker Images

You can download Docker image by using pull subcommand. To download Ubuntu image you can following command.

docker pull ubuntu

The output should be:

Using default tag: latest
latest: Pulling from library/ubuntu
6b98dfc16071: Pull complete
4001a1209541: Pull complete
6319fc68c576: Pull complete
b24603670dc3: Pull complete
97f170c87c6f: Pull complete
Digest: sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d
Status: Downloaded newer image for ubuntu:latest

List Downloaded Docker Images

You can list downloaded Docker images by using the following command:

docker images

Remove Docker Images

If you want to remove downloaded docker image then you can do so by using the following command. Below command will remove Ubuntu image from your system.

docker image rm ubuntu

Working with Docker Containers

Docker container is an instance of Docker Image. You can also interact with the container. Containers are the resource-friendly virtual machines. By using docker container command we can manage operations on a container.

Start a Container

By using container run subcommand you can run a container which is an instance of the image. If the container image is not available then it will first download and then the container will be started.

To start the container of Ubuntu you should run below command:

docker container run ubuntu

In the above command, we didn’t provide any command to execute after starting Ubuntu container so it will boot then executes the empty command and then exits.

To interact with Ubuntu container after booting up, use -it switch. To do so run below command:

docker container run -it ubuntu /bin/bash

The output should be:

[[email protected] /]#

As you can see above the command prompt changed. Now you can execute any command and directly interact with Ubuntu container.

List Active Docker Containers

You can List all active Docker containers using below command. It will list all the active containers otherwise the output will be empty:

docker container ls

If you want to List all Active and InActive containers type:

docker container ls -a

Remove Docker Container

If you want to remove Docker image for some reason you can use the following command:

docker container rm 748fh3304412

In the above command 748fh3304412 is a Container ID.

Conclusion

You have successfully learned how to install and Use Docker on Ubuntu 18.04. If you have any queries please don’t forget to comment below.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here