Introduction to Docker

Introduction to Docker

Β·

13 min read

1. What is Docker πŸ‹

Docker is a platform which packages an application and all its dependencies together in the form of containers. This containerization aspect ensures that the application works in any environment.

Picture1-2-300x271.png

So as you can see in the above diagram, each and every application runs on separate containers and has its own set of dependencies & libraries. This makes sure that each application is independent of other applications, giving developers surety that they can build applications that will not interfere with one another.

1.png

In the above diagram when the Dockerfile is built, it becomes a Docker Image and when we run the Docker Image then it finally becomes a Docker Container.

2. Is it a part of DevOps life cycle 🚲

If you read my devops blog, you should definitely get this concept. The DevOps pillar defines the build–Continuous Integration (CI) pipelines using the Dockerfile provided in the code repository. The CI system pulls the base container images from the selected Docker registry and builds the custom Docker images for the application

3. Architecture behind Docker βš™

dmgpglab3k1z23g02cr5.png

o2dmqkdzcscl4atbhoj3.png

3.1 Docker doemon

The Docker daemon listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services. In simple terms Docker Daemon is a part of Docker Engine that basically takes the API request and interact with docker objects according.

0_XKTCmmou4fT6os-c_.png

3.2 Docker client

The Docker client ( docker ) is the primary way that many Docker users interact with Docker. When you use commands such as docker run , the client sends these commands to Docker doemon, which carries them out. The docker command uses the Docker API. The Docker client can communicate with more than one daemon.

3.3 Docker registries

A Docker registry is a storage and distribution system for named Docker images. The same image might have multiple different versions, identified by their tags. A Docker registry is organized into Docker repositories , where a repository holds all the versions of a specific image. There are basically two types of docker registries public docker registry and private docker registry.

0_ESPDtXtvuW2N3uK9.png

4. Why use Docker

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.

Docker is a containerization platform that enables you to create, deploy, and run applications conveniently with the help of containers. An application in a container can be considered as more secure by default compared to the case with bare metal that also effective for this isolated and consistency functionality. It can provide speedly execution functionality that efficiently organizes the entire development lifecycle by providing a standardized working environment. Due to the consistent environment docker can easy maintain the scalability & flexibility functionality.

3puuhxmfe85kcfxn0jtm.png

20cmt5jrcl0m61hh7ri8.png

5. What is containerization

image-blog-jrebel-what-is-containerization.jpg

Before containerization came into the picture, the leading way to isolate, organize applications and their dependencies was to place each and every application in its own virtual machine. These machines run multiple applications on the same physical hardware, and this process is nothing but Virtualization.

But virtualization had few drawbacks such as the virtual machines were bulky in size, running multiple virtual machines lead to unstable performance, boot up process would usually take a long time and VM’s would not solve the problems like portability, software updates, or continuous integration and continuous delivery. These drawbacks led to the emergence of a new technique called Containerization.

But why we use this containerization πŸ€”

  • Containers have no guest OS and use the host’s operating system. So, they share relevant libraries & resources as and when needed.

  • Processing and execution of applications are very fast since applications specific binaries and libraries of containers run on the host kernel.

  • Booting up a container takes only a fraction of a second, and also containers are lightweight and faster than Virtual Machines.

6. What is Docker Hub

Docker-Images-Docker-Hub-Docker-File-and-Docker-Container-Docker-Tutorial-Edureka.png

Docker Hub is like GitHub for Docker Images. It is basically a cloud registry where you can find Docker Images uploaded by different communities, also you can develop your own image and upload on Docker Hub, but first, you need to create an account on DockerHub.

7. What about Integration in case of Docker πŸ”—πŸ‹

Docker-Integrations-What-is-Docker-Container-Edureka-768x357.png

Suppose I can have only a limited number of DevOps tools running in a Virtual Machine. As you can see in the image above, If I want many instances of Jenkins and Puppet, then I would need to spin up many Virtual Machines because each can have only one running instance of these tools. Setting up each VM brings with it, infrastructure problems. I will have the same problem if I decide to setup multiple instances of Ansible, Nagios, Selenium and Git. It will also be a hectic task to configure these tools in every VM.

This is where Docker comes into picture. Using Docker Container, we can set up many instances of Jenkins, Puppet, and many more, all running in the same container or running in different containers which can interact with one another by just running a few commands. I can also easily scale up by creating multiple copies of these containers. So configuring them will not be a problem.

Docker-Integrations-What-is-Docker-Container-Edureka-1-768x276.png

The former is designed to benefit both Developers and System Administrators, making it a part of many DevOps toolchains. Developers can write their code without worrying about the testing or the production environment and system administrators need not worry about infrastructure as Docker can easily scale up and scale down the number of systems for deploying on the servers.

8. What is Docker Engine

Docker-Client-What-Is-Docker-Container-Edureka-768x329.png

docker_eng_arch.png

Docker Engine is simply the application that is installed on your host machine. It works like a client-server application which uses:

  • A server which is a type of long-running program called a daemon process
  • A command line interface (CLI) client
  • REST API is used for communication between the CLI client and Docker Daemon

As per the above image, in a Linux Operating system, there is a client which can be accessed from the terminal and a Host which runs the Daemon. We build our images and run containers by passing commands from the CLI client to the Daemon.

9. What is Docker Container

Docker Containers are the ready applications created from Docker Images. Or you can say they are running instances of the Images and they hold the entire package needed to run the application. This happens to be the ultimate utility of the technology.

10. What is Docker images

Docker-Run-Command-What-Is-Docker-Container-Edureka-768x302.png

Docker Image can be compared to a template which is used to create Docker Containers. They are the building blocks of a Docker Container. These Docker Images are created using the build command. These Read only templates are used for creating containers by using the run command.

11. What is the difference between a Docker image vs a container

theserverside.com/blog/Coffee-Talk-Java-New.. 🚩

12. What is Docker Compose

Docker Compose is basically used to run multiple Docker Containers as a single server. Let me give you an example:

Suppose if I have an application which requires WordPress, Maria DB and PHP MyAdmin. I can create one file which would start both the containers as a service without the need to start each one separately. It is really useful especially if you have a microservice architecture.

13. What is Docker swam

Docker-Swarm-Orchestration-2.png

Docker Swarm is a technique to create and maintain a cluster of Docker Engines. The Docker engines can be hosted on different nodes, and these nodes, which are in remote locations, form a Cluster when connected in Swarm mode. It is type a container orchestration tool but has some low functionality than kubernetes.

14. Difference between Docker compose & Docker swarm

docker-to-swarm-1.png

So docker is a technology that create a isolated environment for application. Bacially it is acually be used in CI/CD process to automated building and deploying the application. In simple words docker is a container platform to configure, build and distribute those build containers. In simple term Docker Compose is a YAML file which contains details about the services, networks, and volumes for setting up the application. So, you can use Docker Compose to create separate containers, host them and get them to communicate with each other. Each container will expose a port for communicating with other containers.

In other hand docker swarm is a alternative to kubernetes that is also a container orchestration tool. It is lightweight and easy to use but the functionality is limited. This technology create and maintain a cluster of Docker Engines, The Docker engines can be hosted on different nodes, and these nodes, which are in remote locations, form a Cluster when connected in Swarm mode.

15. Virtual Machine vs Docker

TR_Virtual_Machine_Container_2.png

Virtual Machine and Docker Container are compared on the following three parameters:

  • Size – This parameter will compare Virtual Machine & Docker Container on their resource they utilize.
  • Startup – This parameter will compare on the basis of their boot time.
  • Integration – This parameter will compare on their ability to integrate with other tools with case.

16. Basic Docker commands

This simple container returns Hello from Docker! to your screen. While the command is simple, notice in the output the number of steps it performed. The docker daemon searched for the hello-world image, didn't find the image locally, pulled the image from a public registry called Docker Hub, created a container from that image, and ran the container for you.

4585.PNG

# The 'docker pull' is a Docker command to download a Docker image or a repository locally on the host from a public or private registry.

$ docker pull ubuntu: 16.0
# This is the image pulled from the Docker Hub public registry. The Image ID is in [SHA256 hash format](https://www.movable-type.co.uk/scripts/sha256.html) this field specifies the Docker image that's been provisioned. When the docker daemon can't find an image locally, it will by default search the public registry for the image. 


$ docker images
# Run the ubuntu interactive environment

$ docker run -it ubuntu
#  To check all the information about running containers

$ docker ps
#  The ps aux command is a tool to monitor processes running on your Linux system. Basically it show all the activity when you running all the container in your local system. 

$ ps aux
#  List running containers.

$ docker container ls

# Basically we attached a bash shell with the running container. This command is not run if the specific mention container is not in  running state.

$ docker container exec -it  <enter the container id> bash
# Stop one or more running containers


$ docker stop <enter the container id>

# It basically show the stopped container

$ docker ps -a

# To remove a particular container

$ docker rm <enter the container id>
# It gives all the information about a particular container

$ docker inspect <enter the container id>

# Fetch the logs of a container

$ docker ugs <enter the container id>
#  Delete all the stopped container 

$ docker container prune -f
#  A minimal Docker image based on Alpine Linux with a complete package index and only 5 MB in size! we download the alpine image and ping google.com.


$ docker run alpine ping www.google.com

# Some container running really long in background, here the best use case for this basically servers. Here we download the alpine image and ping google.com in detouch mode and return the id of the container that running background.

$ docker run -d alpine ping www.google.com

# Print hello of the ubuntu terminal 

$ docker run ubuntu echo hello

# provide all the the running data since 5 second

$ docker logs --since 5s <enter the container id>
#  show the all images

$ docker images
# To remove images

$ docker rmi alpine -f
# port forwarding. Run this command to to forward and serve nginx in the port @ 80

$ docker run -d -p 8080:80 nginx

# To start a specific container

$ docker start <enter the container id>

# Attached a bash shell with the running container. This command is not run if the mentioned specific container is not running.

$ docker exec -it <enter the container id> bash

$ docker commit -m "added names.txt file" <enter the container id> name_ubuntu:1.01

$ docker run -it names_ubuntu

$ docker run -it names_ubuntu: 1.01

$ docker image -q

docker rmi$ (docker images -q)

from ubuntu: 16.04
CMD["echo" "hey this is docker"]
# To Build Docker Image

$ docker build -t myimage

17. Demo πŸ‘¨β€πŸ’»

  • This file instructs the Docker daemon on how to build your image.

-The initial line specifies the base parent image, which in this case is the official Docker image for node version long term support (lts). -In the second, you set the working (current) directory of the container. -In the third, you add the current directory's contents (indicated by the "." ) into the container. Then expose the container's port so it can accept connections on that port and finally run the node command to start the application.

Docker file :

cat > Dockerfile <<EOF
# Use an official Node runtime as the parent image
FROM node:lts
# Set the working directory in the container to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Make the container's port 80 available to the outside world
EXPOSE 80
# Run app.js using node when the container launches
CMD ["node", "app.js"]
EOF
  • This is a simple HTTP server that listens on port 80 and returns "Hello World".

cat > app.js <<EOF
const http = require('http');
const hostname = '0.0.0.0';
const port = 80;
const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello World\n');
});
server.listen(port, hostname, () => {
    console.log('Server running at http://%s:%s/', hostname, port);
});
process.on('SIGINT', function() {
    console.log('Caught interrupt signal and will exit');
    process.exit();
});
EOF
  • The -t is to name and tag an image with the name:tag syntax. The name of the image is node-app and the tag is 0.1. The tag is highly recommended when building Docker images. If you don't specify a tag, the tag will default to latest and it becomes more difficult to distinguish newer images from older ones. Also notice how each line in the Dockerfile above results in intermediate container layers as the image is built.
docker build -t node-app:0.1 .
  • The --name flag allows you to name the container if you like. The -p instructs Docker to map the host's port 4000 to the container's port 80. Now you can reach the server at localhost:4000. Without port mapping, you would not be able to reach the container at localhost.
docker run -p 4000:80 --name my-app node-app:0.1
  • The container will run as long as the initial terminal is running. If you want the container to run in the background (not tied to the terminal's session), you need to specify the -d flag.
curl http://localhost:4000
  • Stop and remove all containers:
docker stop $(docker ps -q)
docker rm $(docker ps -aq)

18. As a beginner, where to start with Docker πŸ’πŸ»β€β™‚οΈπŸ’πŸ»β€β™€οΈ

As the commands written in linux better to start with basic linux commands and familiar with Devops life cycle. Then learn about docker architectures and VM's functionality. Finally go ahead with this blog 😎 and also check out the top resources about Devops, Docker and kubernetes.

19. References

🚩 hub.docker.com/_/hello-world

🚩 docker.com

🚩 docs.docker.com/get-docker

🚩 hub.docker.com/_/alpine

🚩 youtu.be/9_s3h_GVzZc

🚩 youtu.be/17Bl31rlnRM

🚩 youtu.be/8vXoMqWgbQQ

🚩 youtu.be/3c-iBn73dDE

🚩 theserverside.com/blog/Coffee-Talk-Java-New..

20. Get involved πŸ›°

Twitter πŸ–±

LinkedIn πŸ–±

Github πŸ–±

That's all for this blog, I hope you will learn something new. And feel free to share your thoughts and feedback to make this blog more valuable for beginner to advance developers, Thanks for reading.

Feel free to reach out me πŸ‘€

Twitter πŸ–±

LinkedIn πŸ–±

Github πŸ–±

Did you find this article valuable?

Support ADITYA DAS by becoming a sponsor. Any amount is appreciated!

Β