Docker -- From Scratch to Advance Level

Docker -- From Scratch to Advance Level

Here we go with my first ever project on Docker taught us by Sir Vimal Daga!

In this article I tried to explain step by step process from the very scratch on,

-- how to install docker in your VM (preferably Linux, RHEL 8)

-- pull images from centralised repository to create your own images

-- push own images to public repository

-- creating own customise network driver with subnet range

-- build a web-site with multi-tier architecture using Docker-Compose file concept with persistent storage

-- expose the Web-Server to the public world using PAT

#righteducation #docker #VimalDaga #iiec_connect #iiec_rise

A. Configuring Docker in your base OS - RHEL 8 :

In my case I am using Oracle Virtual box for virtualisation platform and inside it I'm using RHEL 8 server for this project.

What is Docker : A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

Why we are using docker : Guys, we know that, to use a program we need OS, and to launch a OS we need to wait for at-least 30 mins, that includes boot the machine to log in to configure the terminal/console etc. So for 30 min we have to sit ideal and wait. Then we can install our programs e.g. database, java programs VLC media player, fire-fox etc. So getting our environment run any task we need to wait at-least around 30 mins. So minimise this time here comes the idea of Docker i.e. Container Technology. Using this we can install any O.S just in a second, install OS, Boot and ready to use any programs.

So here our lab set-up will be like in our base laptop we have Oracle V-Box installed and inside it we have RHEL 8 server, and on top of RHEL 8 we are going to install Docker rpm.

So basically Docker comes in two types -

a. Community Edition (CE)

b. Enterprise Edition (EE).

Here we are going to use Docker-CE as it's free. But in RHEL 8 Docker-CE is not supported, but we can overcome this as well. There is a pretty straightforward trick through which we can do this. To install docker in linux machine the command we use is,

Command: curl -ssl https://get.docker.com | sh

No alt text provided for this image

But if we do this we are going to get the error as shown in the image, cause this above command is using a script that we go to internet and search for os name, and whenever it's found that it is a RHEL 8 it will through us this error.

To solve this issue we can go to internet and search for docker rpm download location and configure a yum repository for docker in /etc/yum.repos.d directory and create one file called docker.repo, which is shown in the image below. yum will search for the rpm packages with dependencies and install it for us.

No alt text provided for this image
No alt text provided for this image

After the repo file creation we need to simply run the following command to install docker-ce with --nobest option.

Command: yum install docker-ce --nobest

It will download the stable version of Docker along with dependencies.

After installation is done we need to start docker services cause it's a program and the command to do this is as follows:

Command: systemctl start docker

No alt text provided for this image


Post service start we can check if docker is installed properly or not with the following commands,

Command: rpm -qa docker-ce

Command: docker version

No alt text provided for this image

Till now we have learnt how to install docker and set up our own personalised lab.

B.How To Pull Images From Public Repository :

As we have installed docker on top of RHEL 8 machine and that extra layer or daemon is known as DOCKER ENGINE (DE), which is why Docker is also called as Service. Now to launch any application or run any program we need to have a OS, and this can be achieved within a second with Docker Technology.

First we will see how to pull images (almost every images) and create a container and launch application. To do this we have to go to docker hub which is a public repository created by Docker creators. It would be better if e have an account created.

We need to first login to hub.docker.com from any browser, that will looks like below and in the search menu we can search for any operating system, and there they have provided one command,

Command : docker pull ubuntu

No alt text provided for this image

If we click on the OS name, say here we are going to pull Ubuntu OS, then it will land us to the below page, from where we can get the above command and run it on the Docker Host and it will go to the internet and download the image for us.

No alt text provided for this image

We can specify the OS version as well. Then we need to go back to the linux terminal and run the image pull command.

No alt text provided for this image

After successful pull of the OS image we can verify by the command,

Command : docker image ls | grep ubuntu

It will tell us that we have now the OS image with us in Docker Host, and can launch a new container with it.

C. Launch New Container :

As we have OS images with us we can now launch containers as many as we want within a second. To do this the command used is,

Command :

i. docker container run -it --name <container name> <image name>

[It will launch a container and give us an interactive terminal of the container]

ii. docker container run -dit --name <container name> <image name>

[It will launch a container but will not give us an terminal]

To elaborate the above commands it states that, -t == it will give us terminal of the container, -i == interactive terminal of the container, -d == detached mode, --name == name of the container, image name == OS image name that we downloaded from the hub.docker.com. This will take us to the interactive terminal of the newly created container.

No alt text provided for this image

Now to come out of that container we use a shortcut key --> left ctrl + p + q

It will take us out of the container without stopping it. If we use 'exit' command while in the container then it will STOP the container.

Now to check the list of containers e have and want to details about any container we can use the following commands.

Command : docker ps -a

Command : docker inspect <container name>

No alt text provided for this image

Whenever a container is created docker assigned a unique ID to it, called as Container ID, highlighted in above image. Using inspect command we can get all the details like IP address of the container, OS version, Network Driver etc. and many more.

Now to go inside a container we can use the following commands,

Command: docker attach <container name>

[It will take us inside a container with attached mode]

docker exec -it <container name> bash

[It will take us inside the container but is bash shell mode]

Now as we have our container, we will install apache web-server in it and host our web-site. We need to first login to the container and run the following command.

Command : yum install httpd -y

No alt text provided for this image

Once done, we need to start the apache web service inside the container, to access the we-site. But in docker "systemctl" command is not working due to some reasons. So there is a way we can run the services httpd inside a web-app. To this we can check if httpd service is running inside our base system, from there we need to check the location where systemctl program is saved and behind the scene what program it's running.

No alt text provided for this image
No alt text provided for this image

Now if we go and check the file highlighted above we will be landed to the page shown beside and from there we can get the program that is using behind systemctl command.

Command: /usr/sbin/httpd

It will run the htpd service inside the docker.

Now the complete environment is launched and ready to use, but it can't be accessed from outside world as in docker whenever we launch a container, it's by-default isolated, and to make it public we need to expose the OS at the time of launching using the following command.

Command : docker run -it -p 8080:80 --name <container name> centos

The above command will expose the container to the outside world through port 8080.

Now as we have the Web-Server with us then, we can launch as many web-server as required, and to do this we need to create our own custom image, called as Container Image. This will launch the environment as many time as required within a second.

D. Own Custom Image :

There are two ways to create our own custom image.

a) Commit & b) Docker File

Here we will discuss only Commit. As in the previous section we have launched our own web server. And in it, the services are running, code has been deployed on top of Centos. So a complete environment setup is ready, now to create a custom image we can use the following command:

Command : docker commit <container name> <image name:verion>

No alt text provided for this image

Here we have created own custom image from container "web1" and named it "apachewebapp:v1". Now with this new image we can launch as many Web-Server as required without doing any manual work, like we are doing before.

Now to push this custom container image to the public repository we first need to create a account to hub.docker.com and then run the following command.

Command : docker push <docker hub user name>/<image name>:<tag>

No alt text provided for this image

E. Create Custom Network Driver With Custom Subnet Range :

As we run the previous commands to launch a container we can see that the default driver in docker is "BRIDGE" and Subnet is "172.17.0.0/16" and Gateway is "172.17.0.1".

No alt text provided for this image

Bridge is a combination of a Switch and a Router, i.e. it has both the capabilities and can run on layer - 2 and layer - 3 both. [Switch is the one that connect multiple devices in same network and router is the one can connect one network to other network and on network to the internet. ]

No alt text provided for this image

Command : docker network create <network name> --driver bridge --subnet 10.0.0.0/16

Here we are creating a new network with CIDR range 10.0.0.0/16 and the gateway would be 10.0.0.1.

Following snippet will clear things out :

No alt text provided for this image
No alt text provided for this image

This is how we can manage network in docker.

F. Launch WordPress Server and MYSQL DB Using Docker Compose File:

I launched a web application server "Word Press" and in the backend I launch another server that contains MYSQL Database and integrate these two servers using linking. As a result whatever we write/edit in we server, it will be stored in the backend DB server.

I created a new custom network with CIDR block 172.28.0.0/16 and hard-cored the IP address of these 2 containers. So if in any case servers reboot , the IPs will always be the same. Also isolated these architecture from other containers.

No alt text provided for this image

Now to make sure that data will not be lost at any cost, I created 2 Persistent Volumes and mount them to respective containers. So if by any chance servers got crashed the data will be available and we can launch new servers at any time within a sec.

As it's a multi-tier infrastructure I created a compose file and write down the codes to make it automate. So with "docker-compose up -d" command we can launch the entire architecture with in a sec.

I have uploaded the codes in GitHub, and following is the link.

  • GitHub link: https://raw.githubusercontent.com/paulpriyanka101/docker-compose-file/master/Docker-Basics

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics