Sitemap

“Containerization Using Docker”

9 min readSep 19, 2023

--

Press enter or click to view image in full size

If you’re new to Docker or simply looking to refresh your knowledge, this blog post serves as a comprehensive introduction to Docker containerization.

Let’s get started!!

Why do we need Containerization?

🔥There is a scenario where a code runs in two different environment by different persons. When the developer runs the code in his system and he gets perfect result hence he forwarding it to testing team, here the same code with the same dependencies but the code doesn’t work, why? What will be the problem? Let’s go in deep on this issue.

🔥Here they using two different environment based on their needs, they may have different OS. Having different environment cause issues when the code has to be run. A developer used different OS, different version of softwares and particular dependencies but the testing person may not have same environment, so in different environment the code behaves differently.

🔥To resolve this issues, containers plays a vital role to create an isolated environment. Containers are portable to deliver software in packages. Here developer writes the code in container and then send this container to the tester, so that the tester will run the code in the same environment everything will work fine.

🔥To launch the Containers we have multiple applications like lxd, MESOS, Docker, rkt out of this Docker is the best containerization tool. It will have a better performance as they are hosted in a single Docker engine.

🔥Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

Benefits of Using Containers

✅Container occupies very less space.

✅ It takes very less time to boot up when compared to virtual machine.

✅ Containers runs steadily having better performance as they runs in single Docker engine.

✅ High scalability and high efficiency

✅ Containers are very easily portable from one system to another.

✅ The data volume can be reused and shared among multiple containers.

What is Docker?

💥Docker is a tool which containerize the application, allows you to separate your application from infrastructure so that we can deliver the software quickly.

💥It provides an ability to create container, in that we can launch OS level virtualization, here we can launch our OS level virtualization to deliver software in packages and these containers are isolated from one another and bundled in your own software.

💥Docker streamlines the development lifecycle by allowing developers to work in standardized environments using local containers which provide your applications and services. Containers are greatly used for continuous integration and continuous delivery (CI/CD) workflows.

Key Concepts:

Before we dive deeper into Docker, let’s familiarize ourselves with some essential concepts:

  1. Docker Images: Docker images are read-only templates that define the application, its dependencies, and runtime instructions. Think of them as blueprints for containers.
  2. Containers: Containers are instances of Docker images that can be run and executed. They are isolated environments where applications can run without interfering with the host system or other containers.
  3. Docker Engine: This is the core component of Docker responsible for building, running, and managing containers. It consists of a server, REST API, and a command-line interface.
  4. Dockerfile: A Dockerfile is a text file that contains a set of instructions for building a Docker image. It specifies the base image, application code, environment variables, and more.
  5. Registry: Docker images can be stored and shared in repositories called Docker registries. Docker Hub is a popular public registry, but organizations can set up private registries for security and control.
  6. Docker Volumes: Docker volumes are a feature in Docker that allows you to persist data generated by and used by Docker containers. Volumes provide a way to manage and share data between containers and the host machine.
  7. Dockerhub: Docker Hub is a cloud-based platform and a central component of the Docker ecosystem. It serves as a public registry for Docker images, providing a place for Docker users to discover, share, and distribute container images.

Installation — Docker Desktop

Let’s see how to install Docker on different operating system like windows, Ubuntu, Mac OS. Sharing you the installation guide URL for Windows and Mac OS Desktop.

📌This page contains download URLs, information about system requirements, and instructions on how to install Docker Desktop for Mac.

📌In this URL you can get the information and instructions on how to install Docker Desktop for Windows.

📌This page contains information about general system requirements, supported platforms, and instructions on how to install Docker Desktop for Linux. URL

📌Docker Engine is available on a variety of Linux distros, macOS, and Windows 10 through Docker Desktop, and as a static binary installation. Find your preferred operating system in the given URL.

Installation On Linux System

As if now we have seen on how to install Docker desktop version. Also let’s have detailed steps on installing Docker on Linux machine. Let’s use an Ubuntu example to begin installing Docker.

To install packages into the Linux system via the following command, the latest packages can be updated to the OS.

sudo− Make sure you run in root user, use sudo command is used to make sure the command runs with root access.

update− Update option ensures that all packages on the Linux system are updated.

$ sudo apt-get update
$ sudo apt-get install docker.io
docker installation command

Docker Commands:

➤ To check the installed Docker version, use the command given below.

$ docker --version
Press enter or click to view image in full size
docker version

➤ To pull an image of an application use the command. Using this image, we can go-ahead and launch a container.

$ docker pull <image name>
$ sudo docker pull ubuntu
Press enter or click to view image in full size

➤ To check the image of an application which we downloaded and its information, use the below command.

$ sudo docker images
Press enter or click to view image in full size

The command “ docker images” lists all the images you have in your current machine.

➤ To create or run a container, use the command given below.

$ sudo docker run <image name>
$ sudo docker run -itd -p 80:80 ubuntu
Press enter or click to view image in full size

Here, “sudo docker run” is to run your container and the flag “-it” it makes the container interactive then -p is to run in the port number which i have given 80:80, followed by the name of the image “Ubuntu”.

This command output will gives alpha-numeric result which is shows docker container has been created.

➤ To show the list of containers we have created running in current instance, use the command below.

$ sudo docker ps
Press enter or click to view image in full size

Here the name of the container will be created as a generic name they will be a randomly assigned name like vigilant gold. To change the container name, run the following command.

$ sudo docker run -it -d --name <new name of the container> <image name>
$ sudo docker run -it -d --name first-container ubuntu

This will create another container with the new name you have given.

Get Deepa Mathan’s stories in your inbox

Join Medium for free to get updates from this writer.

➤ To list all the containers which is running and stopped containers use the command below.

$ sudo docker ps -a

➤ To go inside a container use the command “exec” is given below. Now you are inside the container “Ubuntu” server.

$ sudo docker exec -it <container ID> bash
$ sudo docker exec -it 3e3ba29e0f95 bash
Press enter or click to view image in full size
Inside the container -ubuntu

➤ Now you are inside the container Ubuntu server which is root account. Here I give a sample on how to install Apache2 using linux commands.

Press enter or click to view image in full size

Next, have to start the installed apache2 server. using the command given in screenshot below.

Press enter or click to view image in full size

➤ Check if you are able to access the apache page on your browser. Copy the public IP followed by mention :port number.

<public IP>:port_number
http://3.83.126.153:81/

The apache2 page is accessible on the browser using IP address and the port number.

Press enter or click to view image in full size

➤ To stop the container use the command “stop” is given below.

$ sudo docker stop <container ID>
$ sudo docker stop 3e3ba29e0f95

➤ To create a new image using the existing image for creating new container and to save the changes to docker have to commit the container.

Sudo docker commit <previously created image container ID> <name for the new image>
Sudo docker commit 3e3ba29e0f95 deepa_img
Press enter or click to view image in full size

Launched the container from the new image and mapped to the port number 82 (as port 81 is already in use)

Press enter or click to view image in full size

In this new container you can install apache2/nginx required webserver.

➤ To push the image in to “Docker hub” for that we need to give the login credential of this machine.

$ sudo docker login
username:
Password:
Press enter or click to view image in full size

➤ To push the image in to “Docker hub” use the command given below.

$ sudo docker push <image_name>
$ sudo docker push deepamathanapp1:v1
Press enter or click to view image in full size

Image successfully pushed in docker hub in new repository created app1.

Press enter or click to view image in full size

➤ To pull the image from docker hub use the command given below.

$ sudo docker pull <image_name>
$ sudo docker pull deepamathanapp1:v1
Press enter or click to view image in full size

Hope you are clear on how to push and pull images from docker hub.

Docker File

As we already seen the definition for dockerfile in above Key concepts. Now lets see on how builds images automatically by reading the instructions from a docker file.

Commands to write Dockerfile:

FROM: It helps to pull the base image.

RUN: To execute commands

CMD: To run defaults on the start of the container. It can be replaceable.

ENTRYPOINT: To configure the container which will run as an executable.

WORKDIR: To create the working directory.

COPY: To copy directory from local machine to docker container.

ADD: To copy files and folders from local to docker container.

EXPOSE: To listens on the specified network ports at runtime.

ENV: To setup environment variable.

Using the above given dockerfile commands, Creating a dockerfile where apache 2 installed.

To create dockerfile, make a directory in that create one dockerfile using the command.

$ sudo nano dockerfile

FROM ubuntu
RUN apt-get update
RUN DEBIAN_FRONTEND="non interactive" apt-get install tzdata -y
RUN apt-get install apache2 --y
ENTRYPOINT apachectl -D FOREGROUND

After updating the linux script to install apache 2 server, save and exit the docker file and run it.

$ sudo docker build -t C3
Press enter or click to view image in full size

Image created new repository c3

Press enter or click to view image in full size

Next have to run the docker image in port 83:80

$ sudo docker run -itd -p 83:80 C3
Press enter or click to view image in full size

And finally check the same in browser using the public IP.

Press enter or click to view image in full size

In the same docker file we can run our own HTML file also.

Conclusion:

Docker containerization has transformed the way we build and deploy applications, offering portability, consistency, and efficiency. In this introductory post, we’ve covered the fundamental concepts and benefits of Docker, setting the stage for further exploration into Docker’s practical usage, best practices, and advanced features.

🎯Stay tuned for more Docker-related insights and tutorials to level up your containerization skills!

--

Deepa Mathan
Deepa Mathan

Written by Deepa Mathan

I am ambitious in learning and adapting new skills in information technology, and always want to stay up to date with the latest industry trends.