Overview
Docker is an open-source project for automating the deployment of applications as portable, self-sufficient containers that can run on the cloud or on-premises. Build Once and Run Anywhere is the mantra for Docker. Docker is also a company that promotes and evolves this technology, working in collaboration with cloud, Linux, and Windows vendors, including Microsoft. Docker image containers can run natively on Windows and Linux. The blog is mainly focused on explaining Docker, how to install Docker on development environment and how to run the ASP.Net Core application on it.
Existing Build / Deployment Model
Here is the traditional build and deployment model.
Let’s say, we have different machines for Development, QA, Staging, and Production. In this case, we need a system admin to setup machines with the necessary operating system and softwares that are needed to run our application. And then the system admin must manually deploy the build on each server.
Docker’s Model
We can overcome this manual process by packaging software into Standardized units of development, shipment, and deployment.
Here is the build and deployment model using Docker. All the software that is used in our application will be converted as an image and those images will be added to a container. Docker containers will take care of running our application. Docker images can be moved to docker hub from there it can be acquired by any container.
The below section talks about Docker attributes in detail.
Docker Terminology Docker Image
A package with all the dependencies and information needed to create a container. An image includes all the dependencies (such as frameworks) plus deployment and execution configuration to be used by a container runtime. For example, in.Net, the library file which we will get from our build called an image.
Docker Container
Using containers, everything required to make a piece of software run is packaged into isolated containers (units). Unlike VMs, containers do not bundle a full operating system - only libraries and settings required to make the software work are needed. This makes for efficient, lightweight, self-contained systems and guarantees that software will always run the same, regardless of where it’s deployed.
Docker File
A text file that contains instructions for how to build a Docker image.
Docker Hub
A public registry to upload images and work with them. Docker Hub provides Docker image hosting, public or private registries, build triggers and web hooks, and integration with GitHub and Bitbucket.
Docker Trusted Registry
A Docker registry service that can be installed on premises so it lives within the organization data center and network. It is convenient for private images that should be managed within the Enterprise.
Docker Features
- Consistent
One of the foremost advantages of the docker container is that the application will work the same way as it works in staging environment. So, the problem of developers saying “it was working in my machine” gets resolved!!!
- Lightweight
Docker containers are light weight. It will get started very fast and it needs very less RAM to run.
- Sharing
Container images can be shared to Docker Hub or Docker Store where other users can leverage those images.
- Security
Increase app security with containers to isolate apps and guarantee safe transport with image signing and verification of the app as it travels through the lifecycle.
- Portability
Package everything the app needs into a container and migrate that from one VM to another, to server or cloud without having to refactor the app.
- Cost Savings
Increase workload density on servers and VMs to save on infrastructure costs. Gain IT operational efficiency in deployment, maintenance, scale out and roll back.
Get Docker
We can download the docker from www.docker.com site. Download the docker installation file for your OS and get it installed on your machine. Developers can use development environments on Windows, Linux, or macOS. On the development computer, the developer runs a Docker host where Docker images are deployed, including the app and its dependencies
After successful installation of Docker, please make sure it is up and running successfully.
Docker Tool in Visual Studio 2015
In this section, I am going to talk about how to integrate Docker with ASP.Net Core application in visual studio.
- Create a new ASP.Net Core web application.
- Select new Web Application
- Add Docker Support by right click on the project.
- After adding docker support, you will see following new files get added in your project. Those are docker support files.
- Let’s examine each file.
- Dockerfile
This was explained already. To see all docker commands, go to console and type docker -help.
- docker-compose.yml
Docker compose file is a configuration file. It’s used to define the collection of image to be built and run with docker-compose build/run. In addition to that, this file contains the name of the image that is created when the project is run. Note that yml should be pronounced as YAML.
- docker-compose.dev.debug.yml
This configuration file will be used when we run the application in Debug mode. Look at the target operating system attribute in this file. Yes, it’s Linux, Linux image will be created and our application will be running on Linux container. If you want to run it Windows container, we can change it by clicking “Switch to Windows Containers” link on Docker. This is an additional docker-compose file with for iterative changes when your configuration is set to debug. This compose file is used by Visual Studio development tools. Using this file, we can replace the image in a running container. It means we can make change in our application and see the changes in browser without building the application again.
- docker-compose.dev.release.yml
This configuration file is the same as debug.yml but this will be used when we run the application in release mode.
- In this example, image: user/dockerdemo${TAG} generates the image user/dockerdemo:dev when the application is run in Debug mode and user/ dockerdemo:latest in Release mode respectively. You may want to change the user to your Docker Hub username if you plan to push the image to the registry. For example, sakthi/dockerdemo, or change to your private registry url privateregistry.domain.com/ depending on your configuration.
Debugging
- Before running the application open Output window by pressing Ctrl + Alt + O keys so that you will see what is happening behind the scenes. Now run the application using F5 key or select Docker from debug menu.
- The microsoft/aspnetcore image is acquired if NOT already in your cache.
- ASPNETCORE_ENVIRONMENT is set to Development within the container.
- PORT 80 is EXPOSED and mapped to a dynamically assigned port for localhost. The port is determined by the docker host and can be queried with docker ps.
- Your application is copied to the container
- Default browser is launched with the debugger attached to the container, using the dynamically assigned port.
- You can see running docker images and container for DockerDemo app in console.
- Hurray! My application is running in Docker container.
- If you remember, the target OS for this application is Linux. Make the following changes in home controller to make sure your application running on Linux OS.
- public IActionResult About()
- {
- var osNameAndVersion = System.Runtime.InteropServices.RuntimeInformation.OSDescription;
- ViewData["Message"] = osNameAndVersion;
- return View();
- }
- Navigate to About link to see the operating system where our app is running.
Conclusion
This article only talks about the basics of Docker and how to integrate docker with Visual Studio 2015. In my next article, I will explain more about how to integrate Docker with Cloud (Microsoft Azure and AWS).