Advertisements

Posts Tagged 'VM'

Introduction to Docker

It has been 5 years since docker brought containers into the mainstream production environments that changed the way of application development and deployment. This link gives a quick overview on why companies are considering container technologies.  I can say that i lagged behind in exploring docker so i tried some hands-on with docker recently so i wanted to post a few articles on the things i tried. Docker has great documentation so i will try to keep my posts simple and concise and give those documentation links wherever applicable.

In this post, we will look at docker installation and ways to create docker images and containers. I used Ubuntu 17.10 (artful) VM with docker version 17.12.1-ce in Windows 10 based laptop.

Installation:

Docker is available in 2 editions Docker Community Edition (CE) and Docker Enterprise Edition (EE). Docker CE is for developers to explore containers and Docker EE is for enterprise development. Docker CE updates are available via two channels:

  • Stable, gives reliable updates every quarter
  • Edge, gives new features every month

To install Docker CE, do:

Old versions can be uninstalled using sudo apt-get remove docker docker-engine docker.io. Refer to documentation for more information on installation.

To verify docker installation, sudo docker -v.

Post Installation:

To avoid using sudo for running docker commands do following. Refer to this link for more information.

sudo usermod -aG docker svgonugu

HTTP Proxy:

Things are not same when you are behind proxy and can face a few issues while connecting to internet. To add proxy settings in Ubuntu, do

  • Go to Show Applications -> Network -> Network Proxy to update system wide proxy settings.

  • If still facing issue from command line, edit /etc/bash.bashrc to add following lines and reopen terminal. If it’s required for the current session, just issue the same commands at command prompt.

                   export http_proxy=http://hostname:port

                   export https_proxy=http://hostname:port

  • To make apt-get working, create a file named apt.conf in /etc/apt directory with following lines as root user (sudo su).

             Acquire::http::proxy “http://user:password@host:port/”;

             Acquire::https::proxy “http://user:password@host:port/”;

  • Docker daemon also needs to connect to internet to pull and push images. Do following to let docker to use your proxy. Create http-proxy.conf with following contents as root user(sudo su) and copy to directory /etc/systemd/system/docker.service.d. Refer to this link for more information.

                    [Service]
                    Environment=”HTTP_PROXY=http://host:port”
                    Environment=”HTTPS_PROXY=http://host:port”

  • Restart the docker daemon so that proxy settings will be effective.

                    systemctl daemon-reload
                    systemctl restart docker

Images and Containers:

Image is like an installation including everything like code, configuration, runtime etc and container is running instance of an image.

Use docker run to create a container from an image. For example, issue docker run hello-world  as shown below. Docker first checks for local availability of image and downloads from docker registry if the same is not found.

Do docker ps -a to see all containers and use docker ps to see only running containers. In above example, the container is created from hello-world image, prints the message and exits the container. Since this image is not available in local it gets downloaded as shown above.

Do docker images to see all local images.

Now let us start an interactive ubuntu container using docker run -ti ubuntu /bin/bash. This starts an interactive container and also downloads an ubuntu image if not present in local. The -i option makes container interactive and the -t option attaches it to terminal.

Observe hostname is nothing but the container id using docker ps.

To stop container, do docker stop <<containerid>>

To restart container, do docker start <<containerid>>

To attach the started container to terminal, do docker attach <<containerid>>

Creating an Image:

Each docker image will have a base image and can be built manually or using a file called Dockerfile. We will see both of these approaches using a sample nodejs application. Note that its always recommended to use official image provided by nodejs.

Manual Creation:

Do following to spin up a new container based on ubuntu image and install nodejs v8. -p option is used for port forwarding i.e. connecting to 8888 port (first argument in command) on docker host will forward the request to port 8888 in docker container. -v option mounts the file system so that container can access it. in my case, i have sample nodejs module in directory /home/siva/mywork (first argument in command) so i mounted this directory to access it in the container using same path.

docker run -ti -p 8888:8888 -v /home/siva/mywork:/home/siva/mywork ubuntu /bin/bash 

apt-get update    (update the index)

apt-get install -y curl  (install package related to curl)

curl -sL https://deb.nodesource.com/setup_8.x | bash –

apt-get install -y nodejs  (install nodejs)

To verify nodejs installation, do node -v

Now copy nodejs module from docker host to container as below.

mkdir mywork;chmod -R 777 mywork

cp -r /home/siva/mywork/proj1 mywork

cd mywork/proj1

npm install (install all nodejs module dependencies)

Run node movieservice.js and access service using browser url http://localhost:8888/movies.

Now our nodejs application container is up and running. To create a docker image from this container, do

exit the container and find container id using docker ps -a.

docker commit <<containerid>>

docker images (find the newly created image id)

docker tag <> mynodejs (tag the image to meaningful name)

docker images

Using Docker file:

Creating docker images manually may not be possible always and also collaboration can become difficult with the above approach. So docker provides another way of building an image from file called Dockerfile that can have a set of instructions. All manual steps we did earlier can be incorporated in Dockerfile and build image from it.

The mynodejs image created above, does not start node server as soon as container starts. Observe that we started the node server manually by issuing node movieservice.js command. We will make it happen using a Dockerfile and build a new image from it. So create a file named Dockerfile with following instructions. I have taken this example to make a point that even images created by us can be a base image. Note that Dockerfile should always start with FROM instruction specifying base image. CMD instruction is used to specify command to be executed when new container is started..

#from the base image
FROM mynodejs
CMD exec node mywork/proj1/movieservice.js

To build image from this Dockerfile, Do:

docker build -t samplenodeappl .

Note that above command is to be executed after navigating to directory having Dockerfile.

nodeappl

Now instantiate new container using docker run -ti -p 8888:8888 samplenodeappl. Verify that node application is up and running using url http://localhost:8888/movies.

nodeport

We can publish this image to a public repository or can setup a private repository to share. To push the image to docker hub (public repository), do

Create an user at hub.docker.com.

docker login

docker tag <<imageid>> svgonugu/samplenodeappl

docker push svgonugu/samplenodeappl

Observe that image name should be tagged something like svgonugu/samplenodeappl containing your repository name.

push

Login to docker hub to find this newly uploaded image.

hub

Images can be downloaded using pull command as highlighted above. Just to verify, we can delete the local image and pull it from repository.

Docker CLI Reference: https://docs.docker.com/edge/engine/reference/run/

Dockerfile  Reference: https://docs.docker.com/engine/reference/builder/#usage

NodeJS Installation: https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions

VM Installation: https://wp.me/pEWnt-KN

 

Advertisements

Oracle Virtual Box Techniques

Assumption: Using Oracle Virtual Box 5.2.6 for managing VMs.

Moving vdi image across disks:

Typically we might come across a need to move vdi files to different disks or drive for accommodating more memory needs. This can be done using the following steps. make sure that VM is in powered off mode.

First the vdi needs to be detached from the VM as shown below.

Go to File-> Virtual Media Manager and remove the vdi which we are planning to move.

Now move the vdi file to desired location. In my case, the initial location of the vdi is C:\Users\svgonugu\VirtualBox VMs\Ubuntu and i moved this vdi to D drive.

Again go to VM Settings -> Storage and click on add hard disk button high lighted below. Choose the above vdi which is moved to D drive and click OK. Now you should be restart the VM and proceed with your work with no issues.

Increasing the RAM used by VM:

There are so many instances where we might want to increase the RAM allocated to VM after the creation typically determined by the kind of usage. We can modify this in VM settings using the steps below. Make sure that VM is in powered off mode.

Navigate to System -> Motherboard and increase the base memory value as required and click OK.

You can verify this memory settings by issuing cat /proc/meminfo or using free -m command.

 

Creating Ubuntu VM in Oracle Virtual Box

In this post, will describe steps required to install Ubuntu64 virtual machine in Oracle VM Virtual Box. Most of the people may be aware of it so here I am just listing down the required steps.

Note that, here I am using Oracle VM Virtual Box 4.3.24 and installing Ubuntu 14.04.3 (64-bit) VM for demonstration.

  • Open Oracle VM VirtualBox and select File –> New.
  • Give name as  Ubuntu64 or any meaningful name and select type as Linux and version as Ubuntu (64 bit).
  • Click Next and use  slider to select required RAM. We may want to select 1GB for better performance with min of 512 MB.
  • Click Next. Choose the option Create a virtual hard drive now and click Create.
  • Select hard drive type as VDI and click Next.
  • Select Fixed or Dynamic option based on requirements and click Next.
  • Give file name for virtual hard drive file and Click Create. Here you have the option to choose the required hard drive size. This will create new VM and will be shown in the Virtual Box Manager. Note that VM is not yet configured to use Ubuntu OS.
  • To configure, start VM by selecting Machine –> Start and select startup disk file ubuntu-14.04.3-desktop-amd64.iso which we can download it from Ubuntu website.
  • After selecting iso image click Start to initiate the Ubuntu OS creation.
  • Select the language and click  Install Ubuntu.
  • Check Download updates while installing and click Continue.
  • Choose the default option Erase disk and install Ubuntu and click Install now. Click Continue if you see any warning related to formatting disk.
  • Select your location and click Continue.
  • Select keyboard layout and click Continue.
  • Provide details for Name, Computer’s Name and enter the required credentials for VM. and click Continue.
  • Select Log in automatically if you do not want to enter a password each time you log in or select Require my password to log in.  Click Continue.
  • It will take a while for the installation of Ubuntu OS. Once that’s done, click Restart now for the changes to be effective.

We can use any of these commands file /sbin/init or uname –i to make sure that 64-bit OS has been installed.

We can find the OS version using command lsb_release –a.

If you observe that VM is not shown in the full screen mode, then we have to install Virtual Box Guest Additions using steps mentioned below and use the admin password wherever it asks during installation.

guestadd

guestadd1

guestadd2

Now restart the VM and now you would see the VM in full screen mode.

To access internet, make sure that you have following settings for adapter:

settings

nat

If you are behind proxy, you can set proxy by going to System Settings –> Network –> Network Proxy as shown below. Click Apply System Wide and give admin credentials if you are asked for authentication.

proxy

From command prompt, you can set using the following command:

                   export http_proxy=>:”>http://<<host>>:<<port>>

Doing SSH into VM from Host:

  • First install SSH server using the command sudo apt-get install openssh-server.
  • Setup a port forwarding in your VM settings as below and restart the VM. Navigate to VM->Settings->Network->Adapter 1-> Advanced->Port Forwarding.

  • We can connect to this VM using winscp as shown below.


Advertisements

Pages

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 368 other followers

Enter your email address to follow this blog and receive notifications of new posts by email.

Advertisements