Docker series (part 1 of 4): Basics of Docker

17 april 2020 om 13:23 by ParTech Media - Post a comment

You keep hearing about Docker everywhere. We have already mentioned in our article on Container Tools in Visual Studio 2019. Companies across the globe are adopting Docker at an astonishing rate and thousands of applications have been built using Docker as of today.

So what is special about Docker and why is it so popular? The answer lies with the way Docker works.

Docker, when tuned perfectly, can singlehandedly perform the functions of multiple servers thus saving on the cost of buying additional servers. Also, Docker works on the principle of Continuous Integration and Continuous Deployment (CI/CD), a DevOps technology that helps to deploy the code quickly.

And these constitute just the tip of the iceberg.

Docker is much more than these and you will learn more about it here and in the upcoming posts. This is the first part of a four part series on Docker that will cover the basics of Docker.

Table of Contents

What is Docker?

A Docker is a tool that lets you create, deploy and run applications in containers.

Containers are nothing but a piece of software that helps you to virtualize parts of an operating system and helps to run an application by combining all the codes and dependencies into a package.

Consider this example:

Mike, a developer, has been asked to create a web application in his organization. To do this, he sets up an environment with Apache Tomcat server in it. He completes the application and informs Andrew, the tester of the organization to test it on his system. Andrew sets up a Tomcat environment on his system and completes the testing process. Andrew informs Gary, the in-charge of deploying the application into the production environment, about the completion of testing. Gary too ends up setting up the production Tomcat environment in his system.

img

So what has happened here is that an environment has been set up three times which is a complete wastage of time. Also, there is a high possibility of errors creeping in due to different Tomcat version servers installed by the three stakeholders.

This is where a Docker can help. A Docker can generate a single Tomcat environment image that can be used by all the three stakeholders here.

In simple words, an image is a file that can be used to run an application in Docker. You will learn more about images in the subsequent posts. But right now we are going to help you to set up Docker Desktop in your machine.

How to install Docker Desktop?

If you run a Windows machine, then installing Docker Desktop is as easy as installing any software. Here are the steps involved in installing it –

System Requirements

  • Windows 10 64-bit (Pro, Enterprise, and Education)
  • Enabled Hyper-V and containers windows features

To run Hyper-V, you need to have the following –

  • 4GB RAM
  • Hardware virtualization support enabled in BIOS
  • A 64-bit processor which has SLAT (Second Level Address Translation)

Steps to install

  1. Download the Docker Desktop Installer.
  2. Once the file has been downloaded, click on the Docker Desktop Installer.exe to begin the installation.
  3. Complete the setup by following all the instructions on the installation wizard after accepting the license and authorizing the installer to make changes to your system. The changes include installing network components links to the Docker apps and also managing the Hyper-V VMs.
  4. Click Finish to complete the setup

Once the file is installed, click on the Docker Desktop file in the installed directory to start the application.

Now that Docker Desktop is installed, you can start creating an image by adding instructions to a Dockerfile and executing it.

What is a Dockerfile?

A Dockerfile is a text document or a script that contains various commands used to call on the command line to build an image. Docker uses this Dockerfile to read the instructions within it and create images. A Dockerfile is normally used to organize things and simplify the process. The best part about a Dockerfile is that it uses a simple and clear syntax which makes it extremely easy to create and execute. They even allow commenting like other programming languages.

Typically Dockerfiles begin with the FROM command that defines from where the build process should start. This is then followed by other methods, commands, and arguments. Upon executing the file in Docker, a new image will be created that can be then used for creating containers.

Dockerfile Commands

Here are some of the commands that can be used inside the Dockerfile to help in the process of image creation.

  • ADD – This instruction is used to copy a new file from a source on the host and adds them to the filesystem of the image at the set destination.
  • CMD – This instruction is used to execute any specific command within the container. CMD specifies the default arguments of the executing container. There can be only one CMD instruction in the Dockerfile. If there is more than one, only the last one will be considered.
  • ENTRYPOINT – This instruction helps in setting a default application that can be used every time a container is created. In other words, it helps in configuring a container to run it as an executable.
  • ENV – This instruction is used to set the environment variables. It sets a variable to a value which in turn will be used in all the subsequent instructions in the build stage.
  • FROM – This instruction is the most important one in the entire Dockerfile and defines the base image that is used to start the build process. It initializes a new build stage and sets the base image for all the subsequent instructions.
  • MAINTAINER – This instruction is used to define the full name and the email address of the creator of the image.
  • RUN – This instruction will execute all the commands in a new layer. This will be on top of the current image. It will commit the results. The committed image will be then used for the next step in the Dockerfile.
  • USER – This instruction is used to set the username of the user who will run the container.

These are just some popular commands that are commonly used while creating an image with the help of a Dockerfile. There is plenty more for you to explore and use in your next Dockerfile.

The below image is a simple Dockerfile snippet. The code has been added to a text editor and saved without any extension.

img

In this Dockerfile, an Ubuntu image is the base image that has been used to start the build process.

The MAINTAINER describes the name and email address of the creator of the new image.

The RUN command here will execute apt-get update to get the latest package version of Ubuntu.

The last instruction is CMD that will run and execute echo with the supplied parameter. This is executed only when we create a container from the image.

What’s next?

In this post, we have seen the basics of Docker, the steps to install a Docker and how to create a Dockerfile. In the next post, we will dive deeper to understand more about Images and containers. We will also create an image from the above Dockerfile and build a container from the created image.

Stay tuned!