As it is the time of cloud technology, and everyone is interested in knowing how these cloud will work. There are mainly categorized the cloud into 3 types namely: Iaas (Infrastructure as a Service), Paas (Platform as a Service) and SaaS (Software as a Service). Out of these 3 categories let’s talk about PaaS with an example of a particular platform called Docker.
What is Docker?
Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. The service has both free and premium tiers. The software that hosts the containers is called Docker Engine.
What is Container?
A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.
By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine.
A container is defined by its image as well as any configuration options you provide to it when you create or start it. When a container is removed, any changes to its state that are not stored in persistent storage disappear.
What is Image?
An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization. For example, you may build an image which is based on the ubuntu image, but installs the Apache web server and your application, as well as the configuration details needed to make your application run.
You might create your own images using Dockerfile or you might only use those created by others and published in a registry, i.e., from the Docker Hub. To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast, when compared to other virtualization technologies.
Now let’s see what are the commands needed to create a Dockerfile and it’s meanings.
- FROM: To pull the base image
- RUN: To execute commands
- CMD: To provide defaults for an executing container
- ENTRYPOINT: To configure a container that will run as an executable
- WORKDIR: To sets the working directory
- COPY: To copy a directory from your local machine to the docker container
- ADD: To copy files and folders from your local machine to docker containers
- EXPOSE: Informs Docker that the container listens on the specified network ports at runtime
- ENV: To set environment variables
Now let us take an example of installing a tomcat server on centos using a Dockerfile. Before creating the Dockerfile let’s see the steps involved in installing the tomcat server and the corresponding commands.
Pull centos from dockerhub | – FROM |
Install java | – RUN |
Create /opt/tomcat directory | – RUN |
Change work directory to /opt/tomcat | – WORKDIR |
Download tomcat packages | – ADD / RUN |
Extract tar.gz file | – RUN |
Rename to tomcat directory | – RUN |
Tell to docker that it runs on port 8080 | – EXPOSE |
Start tomcat services | – CMD |
Let us start to write the Dockerfile. Before we start to create this file we should keep in mind that this file needs to be placed inside the main project folder. For example if your project is created in the location D: Projects{NameOfProject}/ then your Dockerfile should be created inside the {NameOfProject} folder. Here, NameOfProject means your project name.
The Dockerfile looks as follows.
Once your Dockerfile is ready, save and close the file.
Before going to the next step make sure you have installed Docker Hub or Docker Desktop on your PC. If it is not installed please download from the below link to download Docker Desktop.
https://www.docker.com/products/docker-desktop/
Make sure before you build your Dockerfile the Docker Desktop should be in running state.
To build the Dockerfile you need to execute the following command in the command prompt by being in the Dockerfile’s location.
docker build -t {image_name} .
Here image_name will be the name of your docker image that you wish to give.
Once the docker image is created we can check it using the docker image command.
To create a container of the docker image or run the container on docker host the following command need to be executed.
docker run -d –name {name_of_container} -p {external_port}:{internal_port} {image_name}
Here name_of_container will be the container name of your choice, external_port will be the port number of the container which will be exposed externally to the user, internal_port is the port number which will be defined in your Dockerfile and image_name will be your docker image name using which you want to create your container.
In the above image you can see that the container has been created and is running on the port 8081 externally. To check whether the instance is running, we can goto the browser and type http://localhost:8081/.
This is how we can create a Dockerfile and run it. The advantages of using the Dockerfile are:
- It provides the ability to automate all the steps with set of Directives which gets executed during build (refer docker build command) to create final image along with commit of the image.
- It is used anywhere, everything configured and ready to run approach.
- It can be shared with others and updated easily by others. It allows to change the image easily depending on the requirement easily like security hardening, add or update user details etc.
We can also create multiple containers using the same image but we should keep the external port different for each container. With this we can achieve to create multiple instances of the same application on different platforms irrespective of the installed OS.