What is Docker?
Docker is a set of platform as a service products that use OS-level virtualisation to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating-system kernel and are thus more lightweight than virtual machines.
What is Docker Container?
Docker Container is a standardised unit which can be created on the fly to deploy a particular application or environment. It could be an Ubuntu container, CentOs container, etc. to full-fill the requirement from an operating system point of view. Also, it could be an application oriented container like CakePHP container or a Tomcat-Ubuntu container etc.
Docker is a platform for developers and adminstratioto build, share, and run applications with containers. The use of containers to deploy applications is called containerisation. Containers are not new, but their use for easily deploying applications is.
Containerisation is increasingly popular because containers are:
- Flexible: Even the most complex applications can be containerised.
- Lightweight: Containers leverage and share the host kernel, making them much more efficient in terms of system resources than virtual machines.
- Portable: You can build locally, deploy to the cloud, and run anywhere.
- Loosely coupled: Containers are highly self sufficient and encapsulated, allowing you to replace or upgrade one without disrupting others.
- Scalable: You can increase and automatically distribute container replicas across a datacenter.
- Secure: Containers apply aggressive constraints and isolations to processes without any configuration required on the part of the user.
Docker Client and Server
Docker can be explained as a client and server based application,
as depicted in Figure. The docker server gets the request from
the docker client and then process it accordingly. The complete
RESTful (Representational state transfer) API and a command
line client binary are shipped by docker. Docker daemon/server
and docker client can be run on the same machine or a local
docker client can be connected with a remote server or daemon,
which is running on another machine.
Images and containers
Fundamentally, a container is nothing but a running process, with some added encapsulation features applied to it in order to keep it isolated from the host and from other containers. One of the most important aspects of container isolation is that each container interacts with its own, private filesystem; this filesystem is provided by a Docker image. An image includes everything needed to run an application — the code or binary, runtimes, dependencies, and any other filesystem objects required.
Virtual Machine vs. Docker
Virtualization is an old concept, which has been in used in cloud computing, after IaaS has been accepted as a crucial technique for system constitution, resource provisioning, and multi-tenancy. Virtualized resources play the main role in solving the problems using the core technique of cloud computing. The Figure shows the architecture of the virtual machine.
Hypervisor is lying between host and guest operating systems. It is a virtual platform and it handles more than one operating system in the server. It works between the operating system and CPU. The virtualization divides it into two segments: the first one is Para-Virtualization and the second one is Full Virtualization . Figure depicts the architecture of the Docker Container. Linux containers are managed by the docker tool and it is used as a method of operating system level virtualization. Figure shows that in single control host there are many Linux containers, which are isolated. Resources such as Network, Memory, CPU, and Block I/O are allocated by Linux kernel and it also deals with cgroups without starting virtualization machine.
Benefits of using Containers over Virtual Machines
Now let’s discuss what is the benefit of Docker over VMs.
- Unlike VMs( Virtual Machines ) that run on a Guest OS, using a hypervisor, Docker containers run directly on a host server (for Linux), using a Docker engine, making it faster and lightweight.
- Docker containers can be easily integrated compared to VMs.
- With a fully virtualized system, you get more isolation. However, it requires more resources. With Docker, you get less isolation. However, as it requires fewer resources, you can run thousands of container on a host.
- A VM can take a minimum of one minute to start, while a Docker container usually starts in a fraction of seconds.
- Containers are easier to break out of than a Virtual Machine.
- Unlike VMs there is no need to preallocate the RAM. Hence docker containers utilize less RAM compared to VMs. So only the amount of RAM that is required is used.
Install Docker Desktop
The best way to get started developing containerized applications is with Docker Desktop, for OSX or Windows. Docker Desktop will allow you to easily set up Kubernetes or Swarm on your local development machine, so you can use all the features of the orchestrator you’re developing applications for right away, no cluster required. Follow the installation instructions appropriate for your operating system:
Docker Daily use commands
1. docker –version
This command is used to get the currently installed version of docker
2. docker pull
Usage: docker pull <image name>
This command is used to pull images from the docker repository(hub.docker.com)
3. docker run
Usage: docker run -it -d <image name>
This command is used to create a container from an image
4. docker ps
This command is used to list the running containers
5. docker ps -a
This command is used to show all the running and exited containers
6. docker exec
Usage: docker exec -it <container id> bash
This command is used to access the running container
7. docker stop
Usage: docker stop <container id>
This command stops a running container
8. docker kill
Usage: docker kill <container id>
This command kills the container by stopping its execution immediately. The difference between ‘docker kill’ and ‘docker stop’ is that ‘docker stop’ gives the container time to shutdown gracefully, in situations when it is taking too much time for getting the container to stop, one can opt to kill it
9. docker commit
Usage: docker commit <conatainer id> <username/imagename>
This command creates a new image of an edited container on the local system
10. docker login
This command is used to login to the docker hub repository
11. docker push
Usage: docker push <username/image name>
This command is used to push an image to the docker hub repository
12. docker images
This command lists all the locally stored docker images
13. docker rm
Usage: docker rm <container id>
This command is used to delete a stopped container
14. docker rmi
Usage: docker rmi <image-id>
This command is used to delete an image from local storage
15. docker build
Usage: docker build <path to docker file>
This command is used to build an image from a specified docker file
First, update your existing list of packages and upgrade your system:
sudo apt update
sudo apt upgrade
Next, install the latest version of Docker :
sudo apt install docker.io
Once the installation is completed the Docker service will start automatically. You can check that it’s running with following commands:
sudo systemctl status docker
Check the Docker version by typing:
Verify that Docker is installed correctly by running the
$ sudo docker run hello-world
Deploying Your Application
Copy the application to the staging or production server and do the following
- Navigate to the project directory on the terminal and create a docker image.
Run the following command in the terminal and it will create a docker image of the application and download all the necessary dependencies needed for the application to run successfully
docker build -t <name to give to your image>
- Convert Docker image of the Application into a Running container.
Run the following command in terminal and it will use create a running container with all the needed dependencies and start the application.
docker run -p 9090:80 <name to give to your container>
9090 is the port we want to access our application on.
80 is the port the container is exposing for the host to access.
Below are some useful Docker commands
Stopping a running image
docker stop <id-of-image>
Starting an image which is not running
docker start <id-of-image>
Removing an image from docker
docker rmi <id-of-image>
Removing a container from docker
docker rm <id-of-container>
Now, we will try to create a container that will run Nginx server. We can use the
nginx:alpine image because it is small. To download the image, we use the command
docker pull nginx:alpine.
Now, we can build our Dockerfile by running docker build -t simple-nginx ., Docker will then generate an image based on our Dockerfile.
Now, we can run our image by using docker run -d -p 8080:80 simple-nginx. The -d tells docker to detached, and the -it is used to attach interactive TTY, the -p 8080:80 is used to map the port 80 from the container to our computer port 8080, so if we access localhost:8080 it means we are accessing the container.ip:80. The name simple-nginx is the name we defined when we build our Dockerfile above.
This long number you see under the first command is your running container ID. Here you can also see the -t (or tag) we used when we built or original image. The next command docker ps shows all of your RUNNING containers
Now, the final step lets see if our static website is working that should show from our index.html file. We will use localhost ← hostname we set in the port mapping stage. You could use your local ip address and it will show the same web page.
Even now we are deploying the web page or a html page to the Nginx with port map to 80 and also having a index page in an Html format and now we are opening the file and writing a html code for login page and with curl command we can check the working of the application and even we can access with the External IP Address.
Now we are accessing with the IP Address and also the Port number (192.168.29.211:8000)