Docker for beginners

Pruthvi Vikram
5 min readJun 7, 2019

No hassle from now on in installing heavy libraries like MySQL, HTTP server, Oracle Database. Docker is here to make our lives easy.

image source : https://www.docker.com

What is Docker?

Docker is a containerisation tool that provides Platform-as-Service and Software-as-Service products.

Why do we need docker?

Let’s assume you have Windows on your laptop. For some cause, you have to work on a Ubuntu machine. It’s a tedious task to install Ubuntu OS on the laptop without disturbing any of your files.

With Docker, you can simply pull an image from Docker’s biggest global repository and create as many containers as you require. Before discussing any further let’s understand the basic terminology required to make docker easy to understand.

Terminology

Docker Image: Blueprint of working application along with required libraries
Docker Container: An instance of a Docker image ready to use as an application.

image source : https://www.aquasec.com

Alright, Let’s get into action
Below are the steps to be followed for successful docker installation.
1. Install Docker engine.
2. Launch and Login to Docker
3. Pull required docker image from Docker hub Repo
4. Create a container (instance) of Docker image
5. Perform necessary actions on the container.
6. Stop and Start created containers.

1. Install Docker Engine
Installation on windows:
a. Docker cannot run directly on a windows machine Hence, require a virtual box. Docker desktop by default enables Hyper-V. Any other virtual machines hosted on Virtual Box won’t work.
b. Docker Terminal is installed as part of Docker Desktop.
c. Run Docker Terminal from windows which in turn creates a Virtual machine for Docker.

Installation for Mac:
a. Download Mac verson of Docker from Docker site.
b. Mac does not require installation of Virtual box

This is the same for Linux operating systems.

Creating your first Docker container (Running on Mac)
Launch Docker

Make sure docker is running.

Open terminal and check if docker is installed using below command

(base) Pruthvis-MacBook-Pro: ~:$ docker -vDocker version 18.09.2, build 6247962(base) Pruthvis-MacBook-Pro: ~:$

By now Docker is installed on your machine.

2. Docker login:
Docker login command is used to connect your to your personal cloud based Docker Hub repository, for which you need to create an account in docker hub.

(base) Pruthvis-MacBook-Pro: ~:$ docker login
Authenticating with existing credentials…
Login Succeeded

(base) Pruthvis-MacBook-Pro: ~:$

In above code, I was not asked for credentials because I have already logged in. Your PC will store your Docker Hub credentials in docker internal vault.

3. Get a Docker Image:
Docker image is a set of Application or a app bundle and libraries that are needed to run the application.

Part a: Find list of images that you have pulled already.

(base) Pruthvis-MacBook-Pro: ~:$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
labtest-app latest aa80f3ce83ea 2 days ago 516MB
openjdk 8 4a0a42e87cf3 7 days ago 488MB
mysql latest 990386cbd5c0 3 weeks ago 443MB
mysql 5.7 7faa3c53e6d6 4 weeks ago 373MB
(base) Pruthvis-MacBook-Pro: ~:$

Don’t be surprised if you have an empty list. Since I have already downloaded images it is shown.

Part b: Lets download an Ubuntu image from Docker Hub.
login into hub.docker.com and search for Ubuntu

Few things to remember

  1. Official Images : Docker images those are created by official companies.
  2. On the right hand side, you will be provided with a pull command. Use the pull command in your terminal to download the image into your docker engine running on your laptop
  3. If you scroll down, in the same page, you can find the necessary commands required to configure, create and connect to the containers.

Use docker pull command and download any docker image into your docker engine.

(base) Pruthvis-MacBook-Pro: ~:$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
6abc03819f3e: Downloading 8.555MB/28.86MB
05731e63f211: Download complete
0bd67c50d6be: Download complete

4. Create a container (instance) of Docker image
Docker container is an instance of a Docker image. You can create ’n’ number of containers where each container is independent. You can map each container to individual port of your host.

(base) Pruthvis-MacBook-Pro: ~:$ docker run --name ubuntu -it ubuntu bash
root@905f21a8c546:/#

5. Perform necessary actions on the Docker container.

Lets check the version of the operating system and its other detail.

root@905f21a8c546:/# hostname
905f21a8c546
root@905f21a8c546:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.2 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.2 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
UBUNTU_CODENAME=bionic
root@905f21a8c546:/#

Hurray!!!… Ubuntu container is created and you can now run ubuntu commands as required.

6. How to stop and start the created container.
Check the status of containers:
docker ps -a
Stop running container:
docker stop <name of container>
Start stopped container: docker start <name of container>

But remember that there has to be a container which is already created using docker run command.

(base) Pruthvis-MacBook-Pro: ~:$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
905f21a8c546 ubuntu "bash" 9 minutes ago Exited (0) 6 seconds ago ubuntu
7be830d3df0e labtest-app "java -jar labTest-a…" 2 days ago Exited (129) 2 days ago lt-app
c0ff28fb38a5 mysql "docker-entrypoint.s…" 3 weeks ago Exited (0) 3 weeks ago adbm
(base) Pruthvis-MacBook-Pro: ~:$ docker stop ubuntu
ubuntu
(base) Pruthvis-MacBook-Pro: ~:$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
905f21a8c546 ubuntu "bash" 9 minutes ago Exited (0) 56 seconds ago ubuntu
7be830d3df0e labtest-app "java -jar labTest-a…" 2 days ago Exited (129) 2 days ago lt-app
c0ff28fb38a5 mysql "docker-entrypoint.s…" 3 weeks ago Exited (0) 3 weeks ago adbm
(base) Pruthvis-MacBook-Pro: ~:$ docker start -ai ubuntu
root@905f21a8c546:/#

Congratulations. You have created your first container. I really appreciate your patience and enthusiasm in learning new stuff.

Have a Happy Learning and Do Follow me for more interesting tech info.

--

--