Docker : Practical Guide

Himanshu Patel
Analytics Vidhya
Published in
6 min readMay 21, 2020

--

As most of you might have heard about the docker. Docker is very famous among developers. Docker provides packaged software into standardized units for development, shipment and deployment.

What is docker ?

Answer is simple : Docker is containerization technology which uses OS-level virtualization. It is a different technology than the one which is used by Virtual Box.

Virtual Box : The Virtual Box is hypervisor which provides isolation at hardware level. Basically, every VM has their own operating system, Thus the application, necessary binaries and libraries takes up tens of GBs. Moreover, VMs are slower.

Docker : Docker provides the virtualization at a software level. Thus docker shares some processes with the Host OS. As the docker provides virtualization at the software level, it’s called containerization. Docker eliminates the Hypervisor and need of the guest OS. So, the Docker provides efficient virtualization without being slow. Moreover, Docker containers are portable so it works regardless of any host machine.

Docker has support for Linux, MacOS, and Windows.

Benefits of Docker :

  1. Secure : Docker provides security at another level. Applications deployed using container are isolated with the other applications.
  2. Lightweight : As the container shares the kernel with the host system, the container doesn’t need to include the OS, so it’ll increase the speed of shipping and deployment.

How to Use Docker ?

The Docker can be installed from the official guide. This guide has covered installation for all platforms Windows, MacOS and Linux.

Once the docker is installed, One can simply run docker command in the terminal. It’ll provide all type of commands that can be used.

In the VMBox we’re used to run OS with the ISO file. In case of docker, we have a image. You can simply browse the vast number of images at Docker Hub. The images are of Operating Systems, Database Applications or any other applications. We’ll do few tasks to understand how the docker works.

Famous Hello World using Docker

Run the following command in terminal :

docker run hello-world

You’ll be warmly welcomed by the Hello from Docker! message. If you don’t see this message, then there’s problem with Docker Installation.

Docker Hello World Message

You can see the list of images which are currently installed in the system using given command below.

docker images
Command displaying all the images which is installed in the system

Apache web server using Ubuntu image

Install Ubuntu using this following command:

docker pull ubuntu

First, above command will check if any Ubuntu images already exists in the system or not, if it’s not installed than it’ll fetch it from the docker hub. You’ll notice that the image size would be lot a smaller than the actual Ubuntu ISO file. How ? Because the Ubuntu image won’t contain the OS / kernel, as the docker uses host’s kernel.

Docker pulling the Ubuntu images

Now, we can fire up the Ubuntu container using the following command :

docker run -it --rm -p 8080:80 ubuntu

Here, the run attribute will choose the image and will create the container. -it will give an interactive shell the same as any terminal, - -rm will remove the container after you exit, -p will expose port of the container to the outside to the host, and the image name in the last.

Once you run the command, you’ll be welcomed by Ubuntu command prompt. Now, Run any of your favorite command and check how wonderful it works. I’ll run the following command for fun.

cat /etc/os-release

You’ll see bunch of information about the name, and version of the image.

docker ubuntu instance showing release name

Now, update the ubuntu and install the apache web server.

apt update
apt install apache2

The next thing would be to start the Apache web server using this command

apache2ctl start

Once, it’s done you can go to the browser and type http://localhost:8080, and you’ll be welcomed by the Apache Ubuntu default page.

You can install any thing in the docker as you would do in real operating system.

Why install everything in the container ?

Let’s just run the Apache server without doing anything. Here you can take benefit of pre-build images. You can find a bunch of images in the Docker Hub as I mentioned before. You can find marvelous images for your favorite language, tools or software. You can also find the top pre-build images in the Explore at Docker Hub.

Let’s install the Apache server without any hassle.

Run the following command to run the Apache image which is the official image.

docker run --rm -it -p 8080:80 httpd:2.4

The type localhost:8080 in your web browser, and boom! you have run the Apache server ready, and you’ll be happy to see “It Works” in the web browser.

That’s not all, There’s more you can do with the Docker. Let’s say you want to install software of your choices and you don’t want to run commands every time, then you’re saved.

There’s Dockerfile which can configure images for you. Let’s begin by installing the same Apache web server in Ubuntu.

Let’s create an empty directory and put the following contents in Dockerfile.

FROM ubuntu:latest
RUN apt update
RUN apt install apache2 -y

The FROM describes which image you want to use, then RUN command will execute commands to configure as you like. This command will update the ubuntu instance and will install the apache2 server as well.

Now, build the image using the following command from the same directory.

docker build -t test-apache 

Now, create a container using the following command.

docker run --rm -it -p 8080:80 test-apache:latest bash

Here you’ll have to use your own image name which is test-apache This is almost same command which was used before.

Now, you’ll open the bash, and start the web server using the following command:

service apache2 start.

That’s done, you can now open http://localhost:8080 to see if everything worked fine or not.

In the above steps, you were able to see the default page of the apache server. Now I’ll show how to run the web server with your own website.

I’m going to use the sample website which is hosted on the GitHub: https://github.com/himanshu188/Website_Design

You can download or clone the GitHub repository.

Open the project and open the terminal in the same directory. You’ll use the volume to bind the host directory contents to the docker.

Run the following command to start the container by mounting the volumes in the docker container.

docker run --rm -it -p 8080:80 -v “$PWD”/Consultancy_Website:/var/www/html test-apache:latest bash

Now, start the web server using the following command.

service apache2 start

You’ll be able to see the web page when you go to the http://localhost:8080

What’s more, you can share your image to friends or publish to the hub.

If you want to play with the docker interactively without installing docker, you can go to the Play with Docker.

There’s lot to learn about Docker. Docker has become a robust and go-to technology for developers. Gone are the days when something didn’t work because of different machines or operating system. Docker eliminated the discrepancy between the local machine and production servers. Now, it’s easy to get started with development without installing everything in new machine.

Docker is very favorable in organizations when they hiring new developers. Usually, the setup of the local environment takes up a lot of time, but with the Docker, it’s just a click away using the Docker Compose. The Docker Compose is a tool that defines and runs multi-container Docker applications. As an example, if there are a Spring Framework applications then you’ll have docker images for Database, Tomcat Web server, and possibly Redis server. So, Docker Compose will automatically start all the docker instances. This way a company can speed up on-boarding of new developers and improve their infrastructure.

For any questions, contact me on (contact) [at] himanshuptl DOT me or Connect me on LinkedIn.

--

--