The Ultimate Guide on Docker for Beginners [Easiest Way]

Table of Content

  • Introduction to docker
  • Why do you need Docker?
  • Docker Images
  • Docker Containers
  • Why Docker Containers not Hypervisor
  • Docker Client-Server Architecture

Introduction to docker

Docker is an open-source platform that allows you to deploy, run and ship applications. With Docker, it takes quick time to deploy, run and test your applications anywhere in different operating systems. It is a lightweight loosely isolated environment. It has a better cost-effective solution than hypervisors virtual machine as you can use more compute resources.

Docker is written in the Go programming language and takes advantage of several features of the Linux kernel to deliver its functionality.

Docker provides the ability to package and run an application in a loosely isolated environment called a container. Containers are lightweight and contain everything needed to run the application, so you do not need to rely on what is currently installed on the host.

Docker manages the lifecycle of containers starting from developing the applications using containers, which you can use or distribute later and deploy in any environment such as a local data center, a cloud provider, or a hybrid of the two.

Why do you need Docker?

Without Docker : As usual your Operating system are dependent on Hardware and are installed on it. But when you are hosting your applications directly on operating system then applications are dependent on different libraries and other compatible issues. So, when you have two or more applications it becomes difficult to manage so many micro services and takes long time to step up.

Without Docker

With Docker : As usual your Operating system are dependent on Hardware and are installed on it. Now this time you are hosting your applications on Docker rather than directly on operating system then applications have there own libraries and dependencies. So, when you have two or more applications it’s easier to manage as it has its own isolated environment.

With Docker

Docker Images

Docker images basically contain instructions for creating docker containers. Docker images are available in the public registry that is the docker hub and you can also customize the image by adding more instruction in a file which is known as the docker file.

Docker Images are binary data or package that contains application and all its software dependencies and runtimes.

Docker Images can be found on this link.

Docker Containers

Docker applications run inside a lightweight environment which is known as containers and you can run multiple containers in your host it could be windows, Linux, or macOS. Containers are runnable instances of an image. You can create, delete, stop, start containers at any point in time.

Containers are completely isolated environments . It can have its own services, networking interferences, processes, mounts etc. but they all share the same OS Kernel. For Example: the Ubuntu, Fedora, centos they all have same OS kernel i.e. Linux but have different software’s that why there are so many flavors of Linux available such as some are different in GUI or as command line.

OS kernel : OS kernel manages the operations of memory and CPU time. It is core component which acts as a bridge between applications or software’s and data processing performed at hardware level using system calls.

Docker container sharing the same kernel means: docker can run any containers on any operating system that are based and compatible on the underlying operating system kernel.

  • You cannot run Windows based container in the Linux based OS Kernel. For that you will require docker on windows.
  • You can run Linux based container in windows based OS kernel but actually you are using Linux virtual machine on top of windows and then running Linux based container on Linux virtual machine.
Different Operating system sharing the OS Kernel ( Linux )

In the below example: Docker can run any container based on Ubuntu, Fedora or Centos as the underlying OS kernel is ubuntu. So the Containers share all the networking and other OS things from kernel but only software is installed on container.

Why Docker Containers not Hypervisor

Docker containers has only 1 underlying operating system that helps in the cost optimization , utilization of resources , disk space and also helps in less time in booting.

With Hypervisor, you have so many operating system that increases the overload ( disk size) , utilization of resources and also takes more time to boot.

Normal OS – Virtualization – Containerization

There are two docker editions: community edition and the other is enterprise edition. The community edition is free and present in windows, Linux, macOS and cloud( AWS or Azure ) . Enterprise edition comes with more features such as image security, image management.

Docker Client-Server Architecture

Docker uses a client-server architecture where the docker client connects to the Docker daemon which performs all the functions such as building images, running containers, and distributing containers.

Docker client connects to docker daemon using REST API over Unix sockets. For example, when you run the docker run command your docker client first connects with docked daemon which performs the task. Docker daemons can also communicate with other daemons to manage containers.

Docker Daemon: dockerd is the docker daemon which listens to API requests and manages Docker objects such as images, containers, network, and volumes.

Docker registries store docker images. Docker hub is the public registry that anybody can use. Using docker pull or docker run command images are pulled from Docker hub and using docker push they are pushed into docker hub.

Docker commands

  • To run a container from nginx image.
    • If docker image is present on the system docker uses that to run the container else it will pull it from the docker hub and later for subsequent execution same image which got downloaded will be used.
    • By default container runs in Foreground or in attached mode that means container will be attached to the console or the standard out of the docker container and you will see the output of web service on your screen and you wont be able to do anything on the console. Press (Clt + C)to Exit
docker run name_of_the_image
docker run nginx
  • To run a container from ubuntu image. In this case container starts and exits immediately because unlike virtual machines docker are not suppose to host the operating system instead they are supposed to run either any web application or specific tasks or web server.
docker run ubuntu 
  • To run a container from ubuntu image and keep the container running you can allow container to sleep for particular time from an ubuntu image.
docker run ubuntu sleep 5000
  • To run a container from ubuntu image or centos image and keep the container running run the execute command on the container directly as shown below and followed by /cat/etc/*release* but container will exit after you log out from the container as it is not doing anything or running any service.
    • If application needs you to provide input then Dockerzing the application wouldn’t wait for the prompt it just prints the output on the standard output that is STDOUT because by default container doesn’t listhen to the standard input even though you are attached to the console. It doesn’t have terminal to read the input from.
    • You must map standard input of your host to the docker container using the i parameter.
    • With i parameter you have an interactive mode.
    • With t (sudo terminal) parameter you are attached to the container’s terminal. Because application prompts on the terminal and we haven’t attached to containers terminal.
docker run -it centos bash /cat/etc/*release*
  • To run a container in detached mode or in the background and you will get the prompt immediately as container starts. With this mode you need to use -d flag. The container will continue to run in the background.
docker run -d nginx
  • To attach the container back in attached mode.
docker attach name_of_the_container  or container_id
  • To list all the running container’s and some of the information about the containers.
docker ps
  • To list all the container ( Running, Pending, Inactive or terminated etc.)
docker ps -a
  • To stop a container. Stops the container but you don’t get rid of space consumption.
docker stop name_of_the_container
  • To remove the container and get rid of space consumption.
docker rm name_of_the_container
  • To remove multiple containers in the docker.
docker rm container_ID_1 container_ID_2 container_ID_3
  • To list all the docker images
docker images
  • To download or pull the docker image
docker pull name_of_the_image
  • To remove the docker images. Make sure first you get rid of all associated containers that is remove all the containers associated with the images.
docker rmi name_of_the_image
  • To execute a command inside a container
docker exec name_of_the_container cat /etc/host
  • To run a container from a specific version.
docker run image:tag
  • To access the application from web browser you would need to run the docker run command with p flag.
    • p flag will map Port 80 on docker host to Port 5000 to containers Port.
    • In Case 1 – Docker host will listen to application on port 80 on web and internally on Port 5000 on container IP address.
    • In Case 2 – Docker host will listen to application on port 8000 on web and internally on Port 5000 on container IP address.
    • In Case 3 – Docker host will listen to application on port 8001 on web and internally on Port 5000 on container IP address.
    • You can run as many applications you wish to run with different docker host ports but you cannot use the same docker host port again.
docker run -p 80:5000 nginx    # Case 1
docker run -p 8000:5000 nginx  # Case 2
docker run -p 8001:5000 nginx  # Case 3
  • To have a data persistent with the container you should consider mounting a volume from docker host to the container as when you run the MySQL container the volume is created on the /var/lib/mysql directory inside the container and is blown away as soon as you either stop or remove the container.
    • /opt/datadir is the volume created on docker host
    • /var/lib/mysql : Mapping the volume with containers directory
    • mysql is the name of the docker image
    • v flag mounts the volume from host to container.
docker run -v /opt/datadir:/var/lib/mysql mysql
  • To find detailed view of container use inspect command.
docker inspect name_of_the_container  or container_id
  • To find logs of the container use docker logs command.
docker logs container_name or container_id

Creating Your Own Docker Image

Why do you need to create your own docker image? As you might have seen the docker images are just the basic software applications or just the operating system but most of the times you need to create more and more software’s of top of the base image to modify your application. Lets checkout

To create your own Docker Image you need to have a file known as DockerFile where you define instructions that needs to be executed such as base image , update repository, Install dependencies, copy source code, Run the web application etc. Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Each instruction in DockerFile creates another layer that is (All the instructions takes their own memory). While building if any instruction fails and if you rebuild the image then previous instruction which are cached are used to build.

The build is run by the Docker daemon, not by the CLI. The first thing a build process does is send the entire context (recursively) to the daemon and daemon creates the new image.

docker build . 

To Build the docker Image using dockerfile on a different path use f flag.

docker build -f /path/to/a/Dockerfile .
  1. FROM : From instruction initializes new build stage and sets the base image for subsequent instructions. From instruction may appear multiple times in the DockerFile.
  2. ARG: ARG is the only instruction that comes before FROM and FROM takes the arguments.
  3. RUN: The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. RUN are declared with two ways either shell way or executable way.
    • Shell way: the command is run in a shell i.e. /bin/sh. If you need to run multiple command use backslash.
    • Executable way: RUN [“executable”, “param1”, “param2”] . If you need to use any other shell than /bin/sh than you should consider using executable way.
RUN 
/bin/bash -c 'source $HOME/.bashrc; \
echo $HOME'
RUN 

/bin/bash -c 'source $HOME/.bashrc; echo $HOME'
  1. CMD: The main purpose of a CMD is to execute the command within the container just like docker run exec command . CMD has also three forms as shown below. There can be only one CMD instruction in DockerFile. If you list more than one then last will take effect. Also if you don’t specify the executable in CMD then you can also use it along with ENTRYPOINT but make sure to define both CMD and ENTRYPOINT in json format.
    • CMD [“executable”,”param1″,”param2″]
    • CMD [“param1″,”param2”]
    • CMD command param1 param2

CMD defines default commands and/or parameters for a container. CMD is an instruction that is best to use if you need a default command which users can easily override

  1. ENTRYPOINT: An ENTRYPOINT allows you to run the commands as a executable in a container. When you provide the command line arguments to docker run it  will be appended all elements in an exec form ENTRYPOINT, and will override all elements specified using CMD. Lets look at an example.

ENTRYPOINT is preferred when you want to define a container with a specific executable. You cannot override an ENTRYPOINT when starting a container unless you add the --entrypoint flag.

Combine ENTRYPOINT with CMD if you need a container with a specified executable and a default parameter that can be modified easily

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s