Saturday, 3 October 2020

All you need to know about docker, containers, images and services : How to install Docker and run my first container

 Before containerization came into the picture, the leading way to isolate, organize applications and their dependencies was to place each and every application in its own virtual machine. These machines run multiple applications on the same physical hardware, and this process is nothing but Virtualization.

But virtualization had few drawbacks such as the virtual machines were bulky in size, running multiple virtual machines lead to unstable performance, boot up process would usually take a long time and VM’s would not solve the problems like portability, software updates, or continuous integration and continuous delivery.

These drawbacks led to the emergence of a new technique called Containerization. Now let me tell you about Containerization.

Containerization

Containerization is a type of Virtualization which brings virtualization to the operating system level. While Virtualization brings abstraction to the hardware, Containerization brings abstraction to the operating system. 

Moving ahead, it’s time that you understand the reasons to use containers.

Reasons to use Containers

Following are the reasons to use containers:

  • Containers have no guest OS and use the host’s operating system. So, they share relevant libraries & resources as and when needed.
  • Processing and execution of applications are very fast since applications specific binaries and libraries of containers run on the host kernel.
  • Booting up a container takes only a fraction of a second, and also containers are lightweight and faster than Virtual Machines.

Now, that you have understood what containerization is and the reasons to use containers, it’s the time you understand our main concept here.

What is Docker?

Docker is a platform which packages an application and all its dependencies together in the form of containers. This containerization aspect ensures that the application works in any environment.

Containerization - Docker Explained - EdurekaAs you can see in the diagram, each and every application runs on separate containers and has its own set of dependencies & libraries. This makes sure that each application is independent of other applications, giving developers surety that they can build applications that will not interfere with one another.

So a developer can build a container having different applications installed on it and give it to the QA team. Then the QA team would only need to run the container to replicate the developer’s environment.

Dockerfile, Images & Containers

Dockerfile, Docker Images & Docker Containers are three important terms that you need to understand while using Docker.



As you can see in the above diagram when the Dockerfile is built, it becomes a Docker Image and when we run the Docker Image then it finally becomes a Docker Container.

Refer below to understand all the three terms.

DockerfileA Dockerfile is a text document which contains all the commands that a user can call on the command line to assemble an image. So, Docker can build images automatically by reading the instructions from a Dockerfile. You can use docker build to create an automated build to execute several command-line instructions in succession.

Docker Image: In layman terms, Docker Image can be compared to a template which is used to create Docker Containers. So, these read-only templates are the building blocks of a Container. You can use docker run to run the image and create a container.

Docker Images are stored in the Docker Registry. It can be either a user’s local repository or a public repository like a Docker Hub which allows multiple users to collaborate in building an application.

Docker ContainerIt is a running instance of a Docker Image as they hold the entire package needed to run the application. So, these are basically the ready applications created from Docker Images which is the ultimate utility of Docker

Docker Compose

Docker Compose is a YAML file which contains details about the services, networks, and volumes for setting up the application. So, you can use Docker Compose to create separate containers, host them and get them to communicate with each other. Each container will expose a port for communicating with other containers

With this, we come to an end to the theory part of this Docker Explained blog, wherein you must have understood all the basic terminologies.

In the Hands-On part, I will show you the basic commands of Docker, and tell you how to create a Dockerfile, Images & a Docker Container

Docker installation On Ubuntu

Install Docker on Ubuntu Using Default Repositories
sudo apt-get update
sudo apt install docker.io
sudo systemctl start docker

sudo systemctl enable docker
To verify the installed Docker version number, enter:
docker --version

Verify that Docker Engine is installed correctly by running the hello-world image.

$ sudo docker run hello-world


Hands-On

Follow the below steps to create a Dockerfile, Image & Container.

Step 1: create a folder in which you can create a DockerFile and change the current working directory to that folder.


1
2
mkdir images
cd images



Now create a Dockerfile by using an editor. 
I use Vi




After you open a Dockerfile,
 you have to write it as follows

FROM ubuntu:latest
MAINTAINER Sahiti (email@domain.com)
RUN apt-get update
RUN apt-get install -y nginx
ENTRYPOINT ["/usr/sbin/nginx","-g","daemon off;"]
EXPOSE 80



  • FROM: Specifies the image that has to be downloaded
  • MAINTAINER: Metadata of the owner who owns the image
  • RUN: Specifies the commands to be executed
  • ENTRYPOINT: Specifies the command which will be executed first
  • EXPOSE: Specifies the port on which the container is exposed

Once you are done with that, just save the file.

 Build the Dockerfile using the below command.

1
docker build -t devops-app .

** “.” is used to build the Dockerfile in the present folder **

do docker images to show the image that just be built 

Once the above command has been executed the respective

 docker image wii be created. To check whether Docker Image is created

 or not, use the following command.


docker images





Now to create a container based on this image, 

you have to run the following command:

docker run -it -p port_number -d image_id


docker run -itd -p 8085:80 9cdd37834923

Where -it is to make sure the container is interactive,

 -p is for port forwarding, and -d to run the daemon in the background.





We will use Port 8085:80 Make sure these ports are open



Now you can check the created container by using the following command:



docker ps -a










Thats it folks! To view your ngnix container,
go to ip_address:8085 👊👊👊👊👊👊👊😊 You will see something
like this below





Friday, 2 October 2020

How to create New User in Ubuntu 18

 By default, the cloud server comes with a user named ubuntu. You can use such primary user account for system admin tasks on Ubuntu. However, sometimes you need to add a new user account on Ubuntu for additional sysadmin tasks. This page shows how to create a user account or sysadmin account on the Ubuntu server.

Steps to create a user account on Ubuntu Linux

  1. Open the terminal application
  2. Log in to remote box by running the ssh user@your-ubuntu-box-ip
  3. To add a new user in Ubuntu run sudo adduser userNameHere
  4. Enter password and other needed info to create a user account on Ubuntu server
  5. New username would be added to /etc/passwd file, and encrypted password stored in the /etc/shadow file

Let us see all commands in details and

Ubuntu create user account commands

Let us say you need to add a new user in Ubuntu called vivek, type the following command in your shell:
$ sudo adduser vivek
Type your own password and other info:
Create a user account on Ubuntu Linux

Verification

Use the grep command or cat command as follows:
$ cat /etc/passwd
$ grep '^vivek' /etc/passwd

Sample outputs:

vivek:x:1001:1001:Vivek Gite,,,:/home/vivek:/bin/bash

How do I log in using ssh?

From your Windows (WSL) or macOS or Linux desktop, run:
$ ssh vivek@your-aws-ubuntu-server-ip
OR
$ ssh -i ~/.ssh/aws.pub.key vivek@your-aws-ubuntu-server-ip
Enter the password when prompted.

Automation- How to Automate Awx/Tower installation using bash script in Ubuntu 18

 Goal of Automation

In your role as a Devops Engineer, you may find yourself repeating many processes. Especially when it comes to infrastructure configuration and server provisioning. In the SDLC there may be many repeated actions that drive the process, Automation helps to facilitate efficient process workflow as well as save time, money and resources. Automation is about taking once manual processes and placing technology around them so they're inherently repeatable.

In our previous lab we learnt how to install Awx in Ubuntu 18:

https://violetstreamstechnology.blogspot.com/2020/09/what-is-ansiblehow-to-install-awx.html

This process takes about 15mins to complete and involves entering close to 30 linux commands.

This will be a real pain if we are to use this method to install like 50 servers. To solve this problem we will use bash script. Go to Bash Script to learn how to create one.

Step 1: Launch an Ubuntu instance using Ansible: See Previous Tutorial or from Aws Console

Open Port 80,8000,9000

Step2: Log into the instance using Mobaxterm

Step 3: Create A new file called app.sh



Step 4: Right click on the file and Open with default editor



Step 5: Copy the below Bash Script and paste in the window
#!/bin/bash
##########################System Requirements for AWX Server##########################
##########################2GB of memory###############################################
##########################2 cpu cores#################################################
##########################20GB of space###############################################
sudo apt -y update
sudo apt -y install apt-transport-https ca-certificates curl gnupg-agent software-properties-common git
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list
echo "#########################################################################################################1"
sudo apt -y update
echo "#########################################################################################################2"
sudo apt -y install docker-ce docker-ce-cli containerd.io
echo "#########################################################################################################3"
sudo usermod -aG docker $USER
echo "#########################################################################################################4"
newgrp docker << END
echo "#########################################################################################################5"
docker version
curl -s https://api.github.com/repos/docker/compose/releases/latest \
| grep browser_download_url \
| grep docker-compose-Linux-x86_64 \
| cut -d '"' -f 4 \
| wget -qi -;
###Docker compose-Linux-x86_64
chmod +x docker-compose-Linux-x86_64
sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
docker-compose version
docker volume create portainer_data 
docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
echo "deb http://ppa.launchpad.net/ansible/ansible/ubuntu bionic main" | sudo tee /etc/apt/sources.list.d/ansible.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
sudo apt -y update
sudo add-apt-repository ppa:ansible/ansible-2.9
ansible --version
sudo apt install -y nodejs
sudo apt install -y npm 
sudo apt install -y python3-pip git pwgen vim
pip3 install requests==2.14.2
pip3 install docker-compose==1.25.5  
pip3 install --upgrade pip cffi && \
pip3 install ansible && \
pip3 install mitogen ansible-lint && \
pip3 install --upgrade pywinrm 
END
#sudo su -
#sudo su << END
#check for root
UID=$(id -u)
if [ x$UID != x0 ] 
then
    #Beware of how you compose the command
    printf -v cmd_str '%q ' "$0" "$@"
    exec sudo su -c "$cmd_str"
fi

#I am root

git clone --depth 1 --branch 17.1.0 https://github.com/ansible/awx.git
cd awx/installer/
echo "#######################################################################################################################"
echo "################################################################################################################3"
echo "  ¦¦¦¦¦+ ¦¦¦+   ¦¦+¦¦¦¦¦¦¦+¦¦+¦¦¦¦¦¦+ ¦¦+     ¦¦¦¦¦¦¦+    ¦¦¦¦¦¦¦¦+ ¦¦¦¦¦¦+ ¦¦+    ¦¦+¦¦¦¦¦¦¦+¦¦¦¦¦¦+ "
echo " ¦¦+--¦¦+¦¦¦¦+  ¦¦¦¦¦+----+¦¦¦¦¦+--¦¦+¦¦¦     ¦¦+----+    +--¦¦+--+¦¦+---¦¦+¦¦¦    ¦¦¦¦¦+----+¦¦+--¦¦+"
echo " ¦¦¦¦¦¦¦¦¦¦+¦¦+ ¦¦¦¦¦¦¦¦¦¦+¦¦¦¦¦¦¦¦¦++¦¦¦     ¦¦¦¦¦+         ¦¦¦   ¦¦¦   ¦¦¦¦¦¦ ¦+ ¦¦¦¦¦¦¦¦+  ¦¦¦¦¦¦++"
echo " ¦¦+--¦¦¦¦¦¦+¦¦+¦¦¦+----¦¦¦¦¦¦¦¦+--¦¦+¦¦¦     ¦¦+--+         ¦¦¦   ¦¦¦   ¦¦¦¦¦¦¦¦¦+¦¦¦¦¦+--+  ¦¦+--¦¦+"
echo " ¦¦¦  ¦¦¦¦¦¦ +¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦++¦¦¦¦¦¦¦+¦¦¦¦¦¦¦+       ¦¦¦   +¦¦¦¦¦¦+++¦¦¦+¦¦¦++¦¦¦¦¦¦¦+¦¦¦  ¦¦¦"
echo " +-+  +-++-+  +---++------++-++-----+ +------++------+       +-+    +-----+  +--++--+ +------++-+  +-+"
echo "All is setup and done!\
For access Ansible Tower go to: ip address\
Complete manual ----edit the inventory file and install playbook"

Step 6: Save.....Select Yes to All

Step 7: Make the script executable- In the command prompt 
enter: chmod +x app.sh


Step 8: Run the bash script- enter ./app.sh
If you face errors like 
/bin/bash^M: bad interpreter: No such file or directory

Enter
sed -i -e 's/\r$//' app.sh
Then Run the script again
enter ./app.sh


Final step will be to configure: inventory file and run install.yml. Please see https://violetstreamstechnology.blogspot.com/2020/09/what-is-ansiblehow-to-install-awx.html for reference.
The final steps can also be automated . This will be tackled in a later tutorial

How to upgrade Maven

  java.lang.IllegalStateException I had installed maven in my ubuntu using command  apt install maven This installed maven in path /usr/shar...