DEV Community

Cover image for Docker: An Easy approach to understanding it
Shorla
Shorla

Posted on • Updated on

Docker: An Easy approach to understanding it

According to IBM, Docker is an open-source platform that enables developers to build, deploy, run, update and manage containers—standardized, executable components that combine application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.

This might seem like a difficult concept to understand, especially if you are new to Docker or deploying apps. This tutorial is for absolute beginners.

In this tutorial, you will learn about Docker and get it installed on your local machine.

What is Docker?

In simpler words than the one stated above, Docker is a software or open-source platform that allows you to test, build or deploy applications. Docker helps to put applications in containers so they can run on either local or virtual machines or be deployed to the cloud. This process is referred to as containerization.

Docker is designed to benefit developers and Devops.

What is a container?

Containers are boxes that do not depend on any operating system. This means that it is portable, and can run anywhere. They are however dependent on docker images. They allow applications to be packaged together with their dependencies and deployed to the cloud as a single unit.
Each Container is isolated from other containers. it runs its software, binaries and configurations.

What is a Docker Image?

One of the problems faced by programmers is captured in the phrase "but this application worked on my development machine". This very problem is what Docker image solves.

Docker image is a read-only executable file that includes everything needed to run an application. These include the source code, dependencies and other files packaged with the application in question. Docker images are referred to as snapshots of a docker container at a specific point in time, because of their read-only quality.

What is a Dockerfile?

To create a Docker image, you need a Dockerfile. A Dockerfile contains a set of instructions that are used by Docker to create a Docker image.
Dockerfile is text-based and has no extension.

Basic keywords in Dockerfile

Several keywords are used in Dockerfile to create the set of instructions needed to create a Docker image. We will take a look at the main keywords to get started with Docker.

An image is made up of layers, so think of each line of code in the Dockerfile as an instruction to build a layer of the image. In Docker, all keywords are typed in capital letters as you will see below.

The basic keywords are:

  1. FROM: The first instruction of the Dockerfile specifies the base layer which is FROM. The FROM keyword is used to specify the name of the base image. In Docker, there is a public repository for Docker images called DockerHub. You can pull the already existing base image from there. It is advised to pull only official images or images from a trusted source.

The FROM keyword is used with the base image tag beside it. when no version of the tag is specified, it pulls the latest version.

FROM openjdk

  1. RUN: The RUN keyword helps to run specific Linux commands in the docker file. For example, It can be used to create a directory. The directory created by the RUN keywords lives inside the container, not on the host machine.

RUN mkdir directoryname

  1. COPY: The COPY keyword command works the same way as cp in Linux. it is used to copy files from the host machine to the image file system.

The difference between the RUN and COPY keywords is that, RUN works within the container while COPY runs first from the host machine to the container.

COPY /path/on/the/machine /path/to/the/container

  1. WORKDIR: The WORKDIR keyword sets the directory where future commands will be executed.

WORKDIR /path/to/directory

  1. CMD: The CMD keyword specifies the command that will execute when you start a container from the image. You should use the CMD keyword with a command you will normally use to run an application. For example, to run a Java application, we use java main.

CMD java main

  1. Comment: The # sign is used to write comments in Dockerfile.

To build an image after writing the Dockerfile, we run:

docker build

There are other keywords but these are the basics needed to get started with Dockerfile.

Installing Docker

Docker can be installed on a mac, windows or Linux operating system machine.
To install Docker on your local machine, click here to the official Docker documentation. Choose your Operating system and follow the steps.

Conclusion

In this article, you learnt the basics of Docker, Docker image and Containers. If you followed the documentation, you have Docker setup on your machine now.

You are closer to running your first application on Docker.

Top comments (0)