Ranjit Saini
6 min readApr 30, 2020

--

What is docker and why use it?

Docker is a containerization platform that packages your application and all its dependencies together in the form of a docker container. Docker has become a popular term in the IT industry. Docker makes the process of application development very easy and efficient and resolve a lot of issue related to deploying application.
It is a platform that provide containers that is used to make it easier to create, deploy, and run applications by using containers. Docker container technology was launched in 2013 as an open source Docker Engine. You can run your Docker container on any OS-compatible host (Linux or Windows) that has the Docker runtime installed.

Why we need Docker?

Problems before Docker.
1. An application works in developer’s laptop but not in testing or production.This is due to difference in computing environment between Dev,Test and Prod.
2. Developing an application requires several of microservices in one machine. So if you are starting five of those service then you require five VMs on that machine. So there is a lot of wastage resources such as Ram, Processor and disk space are not utilized by microservice which is running on VMs.

How Docker Solves these problem…
We can resolve this problem by using Docker container. So here each of these Docker container contains dependency for one microservice. Docker enabled running multiple micro-services in a single virtual machine.
Docker container are lightweight alternative of virtual machine that mean you don’t need to pre-allocate any Ram and any disk space, it depend upon the requirements. Docker container are developed by the developer. Docker provides a consistent computing environment throughout the SDLC.

What is Docker Container?
It is a unit of software that stores up code and its dependency so that application run fast and reliably from one computing environment to different ones. A Docker container image is a lightweight, standalone, executable package of software that has everything you need to run an application like code, runtime, system tools, system libraries, and settings.
Docker file builds a Docker image and that image contains all the project’s code. You can run image to create many Docker containers as you want.This image can be uploaded on Docker hub (git repository or cloud hostage service) and any one can pull this image and build a container. So if your application is working on development but not working properly on production then this can be easily resolve by below workflow because you have the same environment throughout the SDLC on Dev, Test and Production.

Docker image is a read only file that contains code, libraries and dependencies other files needed for an application to run. It is one of the great feature of Docker. You can read-write copy of this file by using container and also you can create multiple Docker image from one base image.
You can create an image from Docker file by using command —docker build
Create a container layer from an image, use the command- docker create

Benefits of Docker:
1. Compatibility and Maintainability- Docker images run the same no matter which server or whose laptop they are running on. Your production infrastructure will be more reliable and easier to maintain.
2. Multi-Cloud Platforms- Docker’s greatest benefits is portability.Docker containers can be run inside an Amazon EC2 instance, Google Compute Engine instance, Rackspace server, or VirtualBox, provided that the host OS supports Docker.
3. Isolation- Docker ensures your applications and resources are isolated and segregated. Docker makes sure each container has its own resources that are isolated from other containers..
4. Security- Docker ensures that applications that are running on containers are completely segregated and isolated from each other, granting you complete control over traffic flow and management.

Step to create first Docker container with an ASP.NET web app:

  1. Download and install Docker Desktop by using below link https://download.docker.com/win/stable/Docker%20for%20Windows%20Installer.exe
    After installation done, restart the system if needed.
    NOTE: to run Docker on Windows, you need Windows 10 Enterprise, Professional, or Education; and the Hyper-V virtualization enabled (if you don’t have virtualization enabled, the install wizard will ask for it).
  2. You can check Docker details after installation on your system by using below command
    a. >docker version
    b. >docker info
    c. >docker run hello-world
    d.>docker ps or docker container ls (give the docker container files info)
    e.>docker images
  3. When Setup has been done then enter your valid login details if you don’t have account then go to https://hub.docker.com/ and signup and create a Repository to linked with your Github account.
  4. Open Visual studio 2017 or above and create ASP.NET project and select Docker Support option if appeared.
  5. If docker support option was not appeared then open the Solution Explorer and right click on the Project name and add Docker Support file by using Add option.
  6. Dialog box will appear and choose which one your target OS (Window or Linux) for Docker file. It will create a Docker file which defines how to create the container image for your project.
  7. Open the command prompt on the project file location where .dockerignore exist and run the below build command:
    docker build -t projectname . -f projectname/dockerfile ,this process will take few minutes.
  8. If you will facing the issue like below then please open the PowerShell and check with order of connected internet order.

Run the command to check the active internet connection state.
Get-NetIPInterface -AddressFamily IPv4 | Sort-Object -Property InterfaceMetric -Descending
and set the order of your working internet connection
Set-NetIPInterface -InterfaceAlias ‘Wi-Fi’ -InterfaceMetric 1 and when order are set then run the docker build command again.

9. When build has been run successfully then run the command to check the image is working fine docker run -d -p portnumber:80 projectname:tag-name

10. Open the Docker desktop and go to the running image as per above port and click on the “Open in browser” option.

11. Now change the name of the image and link with docker repository by using command docker tag imageid hub-userid/docker-hub-repository:new-image-tag-name and then push the image at Docker hub by using command docker push imageid hub-userid/docker-hub-repository:new-image-tag-name . This process will take few minutes and after you can see your image at your Docker-hub account.

12. Now stop all your running images from Docker desktop and deleted the existing images using docker system prune -a command and pull the image again to see its working fine by using command docker pull docker-hub-userid/docker-hub-repository-name:new-image-tag-name

13. Run the downloaded image by using command docker run -d -p yourportnumber:80 imagename:tag-name

Conclusion
You can refer to this post every time you need a simple and concrete example on how to create your first Docker application. If you have any questions or feedback, feel free to ask.

Happy reading! Happy coding!

Thank you.

--

--