Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Docker – Better way to build

Tags: docker

Overview

Docker is a container management service. The keywords of Docker are develop, ship and run. The whole idea of Docker is for developers to easily develop applications, ship them into containers which can then be deployed anywhere. The initial release of Docker was in March 2013 and since then; it has become the buzzword for modern world development, especially in Agile-based projects.

In this blog we will learn more about,

  1. Features of Docker
  2. Components of Docker
  3. Docker Architecture
  4. Docker Images
  5. Docker Hub
  6. Dockerfile
  7. Docker-Container Lifecycle
  8. Docker Compose

Fig.1 Docker

1. Features of Docker:

  • Docker has the ability to reduce the complexity of development by providing a smaller footprint of the operating system via containers.
  • With containers, it becomes easier for teams across different units, such as development, QA, and Operations to work seamlessly across applications.
  • We can deploy Docker containers anywhere, on any physical or virtual machines and even on the cloud.
  • Since Docker containers are pretty lightweight, they are very easily scalable.

2. Components of Docker:

  • Docker Engine – It is used for building Docker images and creating Docker containers.
  • Docker Hub – This is the registry which is used to host various Docker images.
  • Docker Compose – This is used to define applications using multiple Docker containers.

3. Docker Architecture:

Comparison between Traditional virtualization and Docker

The following image shows the standard and traditional architecture of virtualization

Fig. 2 Traditional Architecture
  • The server is the physical server that is used to host multiple virtual machines.
  • The Host OS is the base machine such as Linux or Windows.
  • The Hypervisor is either VMware or Windows Hyper V that is used to host virtual machines.
  • We would then install multiple operating systems as virtual machines on top of the existing hypervisor as Guest OS.
  • We would then host our applications on top of each Guest OS.

The following image shows the new generation of virtualization that is enabled via Dockers. Let’s have a look at the various layers.

Fig. 3 Docker Architecture
  • The server is the physical server that is used to host multiple virtual machines. So this layer remains the same.
  • The Host OS is the base machine such as Linux or Windows. So this will also be the same.
  • Now comes the new generation which is the Docker engine. This is used to run the operating system as Docker containers.

The clear advantage of this architecture is that we don’t need to have extra hardware for Guest OS. Everything works as Docker containers.

4. Docker Images

In Docker, everything is based on Images. An image is a combination of a file system and parameters. Let’s take an example of the following command in Docker.

Docker run hello-world
  • The Docker command is specific and tells the Docker program on the Operating System that something needs to be done.
  • The run command is used to mention that we want to create an instance of an image, which is then called a container.
  • Finally, “hello-world” represents the image from which the container is made. Now let’s look at how we can use the CentOS image available in Docker Hub to run CentOS on our machine. We can do this by executing the following command on our  machine:
sudo docker run centos –it /bin/bash

Note the following points about the above sudo command:

  • We are using the sudo command to ensure that it runs with root access.
  • Here, centos is the name of the image we want to download from Docker Hub and install on our machine.
  • -It is used to mention that we want to run in interactive mode.
  • /bin/bash is used to run the bash shell once CentOS is up and running.

5. Docker Hub

Docker Hub is a registry service on the cloud that allows us to download Docker images that are built by other communities. we can also upload our own Docker built images to Docker hub. In this blog, we will see how to download and the use the Jenkins Docker image from Docker hub.
The official site for Docker hub is: https://www.docker.com/products/docker-hub

Run a Docker Image from Docker Hub:

Step 1: First we need to do a simple sign-up on Docker hub.

Step 2: Once we have signed up, we will be logged into Docker Hub.

Step 3: Next, let’s browse and find the Jenkins image.

Fig. 4 DockerHub

Step 4: If we scroll down on the same page, we can see the Docker pull command. This will be used to download the Jenkins image onto the local server.

Step 5: Now, go to the server and run the following command:

sudo docker pull Jenkins

To run Jenkins, you need to run the following command:

sudo docker run -p 8080:8080 -p 50000:50000 Jenkins

Note the following points related to sudo command:

  • We are using the sudo command to ensure it runs with root access.
  • Here, Jenkins is the name of the image we want to download from Docker hub and install on our machine.
  • -p is used to map the port number of the internal Docker image to our main server so that we can access the container accordingly.

6. Dockerfile:

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands. A user could call on the command line to assemble an image. Using docker build, users can create an automated build that executes several command-line instructions in succession.

Creation of Dockerfile:

The following steps explain how we should go about creating a Docker File.
Step 1: Create a file called Dockerfile and edit it using vim.

Please note that the name of the file has to be “Dockerfile” with “D” as capital.

Step 2: Build your Docker File using the following instructions:

#This is a sample Image
FROM ubuntu
MAINTAINER [email protected]
RUN apt-get update
RUN apt-get install –y Nginx
CMD [“echo”,”Image created”]
    The following points need to be noted:
  • The first line “#This is a sample Image” is a comment. we can add comments to the Docker File with the help of the # command.
  • The next line has to start with the FROM keyword. It tells docker, from which base image we want to build your image from. In our example, we are creating an image from the ubuntu image.
  • The next command is the person who is going to maintain this image. Here we specify the MAINTAINER keyword and just mention the email ID.
  • The RUN command is used to run instructions against the image. In our case, we first update our Ubuntu system and then install the Nginx server on our Ubuntu image.
  • The last command is used to display a message to the user.

Step 3: Save the file.

Once we created our Docker File. It’s now time to build the Docker File. The Docker File can be built with the following command:

docker build

This method allows the users to build their own Docker images.
Syntax: – docker build -t ImageName:TagName dir
Options: “-t” is to mention a tag to the image
ImageName – This is the name we want to give to our image
TagName – This is the tag we want to give to our image
Dir – The directory where the Docker File is present.

7. Docker–Container Lifecycle

The following illustration explains the entire lifecycle of a Docker container.

Fig. 5 Docker–Container Lifecycle
Initially, the Docker container will be in the created state. Then the Docker container goes into the running state when the Docker run command is executed.

Following are the list of commands as shown in fig 5

  • Docker kill: command is used to kill an existing Docker container.
  • Docker pause: command is used to pause an existing Docker container.
  • Docker stop: command is used to pause an existing Docker container.
  • Docker run: command is used to put a container back from a stopped state to a running state.

8. DOCKER COMPOSE

Docker Compose is used to run multiple containers as a single service. For example, suppose we had an application which required NGNIX and MySQL, we could create one file which would start both the containers as a service without the need to start each one separately.
Creating Your FirstDocker – ComposeFile:
Now let’s go ahead and create our first Docker Compose file. All Docker Compose files are YAML files. we can create one using the vim editor. So execute the following command to create the compose file:

sudo vim docker-compose.yml

Fig. 6 Docker Compose
Let’s take a close look at the various details of this file:
  • The database and web keyword are used to define two separate services. One will be running our MySQL database and the other will be our nginx web server.
  • The image keyword is used to specify the image from docker hub for our MySQL and Nginx containers.
  • For the database, we are using the ports keyword to mention the ports that need to be exposed.
  • And then, we also specify the environment variables for MySQL which are required to run MySQL.

Now let’s run our Docker Compose file using the following command:

sudo ./docker-compose up

This command will take the docker-compose.yml file in our local directory and start building the containers.
Once executed, all the images will start downloading and the containers will start automatically.

Conclusion

We’ve had a pretty good tour through what Docker is and isn’t. How it can benefit us and our organization. Like other powerful technologies, Docker is not without its downsides, but the result is a big positive for the organization. If we implement the Docker workflow and integrate it into the processes we already have in our organization, we will see definite benefits from it.

Image credits: docker website, docker handbook

The post Docker – Better way to build appeared first on DevOpsTech Solutions.



This post first appeared on Migrating XEN Virtual Machines To The AWS Cloud, please read the originial post: here

Share the post

Docker – Better way to build

×

Subscribe to Migrating Xen Virtual Machines To The Aws Cloud

Get updates delivered right to your inbox!

Thank you for your subscription

×