Christmas with Docker
by Alina Pustan
Christmas is almost here, so what better time to make an app that keeps track of the days until the happiest season? In this article I will introduce you to Docker, a great tool for developers which allows them to deploy, develop and test code more easily than ever. In the example below, I will build and run an ASP.NET Core docker image, which shows how many days are left until the Christmas day.
“A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, run-time, system tools, system libraries, settings. Available for both Linux and Windows based apps, containerized software will always run the same, regardless of the environment.”
Containers are a solution to the problem of how to get software to run reliably anywhere.
They have been around for a while now, but they became popular with Docker.
What is Docker?
The Docker platform is the only container platform to build, secure and manage the widest array of applications from development to production both on premises and in the cloud.
Docker delivers an engine for innovation that modernizes without disruption while reducing total costs by more than 50%.
Benefits of using Docker
●Containerization allows for greater modularity. Rather than run an entire complex application inside a single container, the application can be split in to modules (such as the database, the application front end, and so on).
●A container may be only tens of megabytes in size, whereas a virtual machine with its own entire operating system may be several gigabytes in size.
●Containerized applications can be started almost instantly.
●Consistent development environments for your entire team. The development environment is the exact same as the production environment.
●You only need Docker to develop. You don’t need to install a lot of language environments on your machine.
Here is the example:
First, choosing an image: In order to run anything in Docker, you need an image. Using the https://hub.docker.com/ platform, you can pull official images or user created images into your container and use the image accordingly for you project.
Two Docker images are required to create a Docker image for an ASP.NET Core 2.0 application: microsoft/aspnetcore-build and microsoft/aspnetcore. I used the command shown on the page of the image to pull the image from the hub.
Check the pulled images:
Create a new asp.net Core project and modify the project however you want:
Move inside the application folder and run the project
See the results:
To get the application running in a container I created my own image with the help of a dockerfile.
“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. Using docker build users can create an automated build that executes several command-line instructions in succession.”
Use multi-stage builds
Multi-stage builds are a new feature with Docker Version 17.05 that makes the build process much easier. The reason there is a build image and a runtime images is,
because the user wants a slimmer image at runtime. Since this is a very common requirement for a lot of languages, Docker provided the multi-stage builds. This means you can now define build and runtime image in one single Dockerfile and can copy the produced binaries from the build image into the runtime image.
Inside the dockerfile:
Build the image:
The build process takes the dockerfile and runs each line and for each one creates a new image layer. The result is a series of layers which when put together at runtime make a docker container.
The “local/demo” image is the optimized image, build on top if the microsoft/aspnetcore image.
The “” image is the intermediate image that was created based on the microsfot/aspnercore-build and it is no longer useful.
Remove the unused data with the command:
This command will remove all stopped containers, all networks not used by at least one container, all unused images and all build cache. Before deleting such containers, networks, images or cache, you need to consent it.
Start the container based on the image created: here the application runs inside the container
In the browser at localhost:5080 is the result:
*project run on the 21st of November