What is Docker

In this post, we are going to take a brief introduction to Docker, we will see what Docker is, how Docker is different from a virtual machine, and of course the benefits Docker brings us. Finally, we’ll look at how to install Docker and a small example.

 

1 - What is Docker

Docker is a platform that allows us to simplify processes to build applications, distribute them, and also lets us run them in different environments and systems.

By environments, I mean development, pre-production, and production servers or environments.

 

But it’s not just the environment that can vary, also the system itself – it’s common to have our development computers on Windows or Mac, whereas servers are mostly Linux, as are AWS, Google Cloud, and even Azure runs its own version of Linux.

docker en diferentes sistemas

1.1 - How Docker works

When we talk about Docker, we are really referring to two concepts, images and containers. These two concepts are the basis of Docker.

If you’ve noticed the Docker logo, the whale has several containers on top, and this is for the following reason.

 

The power of containers lies in helping us deploy applications in a much simpler way.

A few years ago, deploying a large application would take up a huge amount of time; you’d have to manually delete a certain folder on X server, then move the files, etc. Of course, these actions could be automated with scripts, but not all companies use scripts.

Basically, every company and every developer does things their own way.

 

Docker provides us with a consistent technique for making these deployments across different environments, which gives us a lot of benefits as developers.

 

1.2 - What is a Docker image

As I mentioned earlier, Docker is based on two concepts – images and containers. When we talk about images, we are talking about something used to build a container.

An image contains the necessary files to run the application we want to run on a particular server. For instance, Ubuntu or Windows.

Then there’s our framework, or the database, the files, etc.

 

In our specific case, if we have a website written in ASP.NET, we have the framework written in the image as well as the code in the image.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1

WORKDIR /home/app
COPY bin/Debug/netcoreapp3.1/publish .

ENTRYPOINT ["dotnet", "WebPersonal.BackEnd.API.dll"]

Docker doesn’t download all the libraries of the operating system, instead it’s a way to indicate which server we want it to run on. For our specific case, only the Net Core libraries will be downloaded.

 

The image by itself does nothing; it is the definition used to create a container that is executed.

 

1.3 - What is a Docker container

If we base ourselves on the previous point, we can see that a container is the instance of the image where the application is running.

docker image and container

It can be any type of application, from our own web application, to a cache server, or a database.

 

The great benefit of using containers is that each container is completely isolated from the rest, which means each one can be created, instantiated, stopped, or deleted individually.

Another big benefit is how fast and easy it is to create a container.

 

2 - Difference between a Docker container and a virtual machine

When we start developing with Docker, the first thing that comes to mind is virtual machines that we’ve worked with during studies at university or in training, because given the previous point it looks the same: it’s a machine that contains an operating system and an application inside.

 

First, a virtual machine runs on our operating system as a “guest” through the Hypervisor. Also, running multiple virtual machines, depending on your hard disk or memory, can be crazy. They work, but you need significant amounts of RAM, CPU, and gigabytes on your HDD, etc.

 

On the other hand, Docker containers run on the Docker engine, which runs directly on the operating system. This means the size is much smaller than a normal virtual machine.

maquina virtual vs docker

A Docker container contains on its own all the system files the application needs in order to run without issues. But at the same time, it contains nothing extra that it doesn’t need.

 

Of course, since they are individual containers, the files in container 1 have nothing to do with those in the container for app2. For example, we can create app1 in .NET and app2 in JavaScript.

Later in another post, we’ll see how to make a container with a database for our integration tests.

 

3 - Running a web application in Docker

For this example, I’m going to run the web application I’m building for the Web C# course.

To do this, we just need to create a file called Dockerfile. I’ll leave the explanation of the contents of that file for another post dedicated to it.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1

WORKDIR /home/app
COPY bin/Debug/netcoreapp3.1/publish .

ENTRYPOINT ["dotnet", "WebPersonal.BackEnd.API.dll"]

All you need to know for this post is that the web application runs on ASP .NET Core 3.1 so we need to indicate that in the FROM section.

We specify the working directory in our Docker, remember it’s LINUX, and indicate to copy the files from the publish folder, having previously run dotnet publish in powershell.

Finally, we point to the entry point, which is the API’s .dll.

Then, we build our Docker image and run it in its container.

docker build -t webpersonal .

 

When executing the Docker command, it will download all the necessary files for the application to run.

docker example

 

Now, we run our container with the following command:

docker run -p 8080:80 webpersonal

And we see how the application is listening on port 8080 and we can make calls to the API without any issue. For the example, I’ve created an endpoint.

[ApiController]
[Route("api/[controller]")]
public class HealthController : ControllerBase
{
    [HttpGet("ejemplo-docker")]
    public string Get()
    {
        Console.WriteLine("docker");
        return "true";
    }
}

docker example

 

As we have seen in the API code, we have a Console.WriteLine("Docker"); – this is because if we go to the Docker dashboard (from the icon in the taskbar), we can see the container execution.

docker dashboard

As well as certain information, or for example the ability to stop/delete containers. Most commonly, we’ll perform all these actions from the command line.

 

4 - Benefits of Docker

The first and clearest benefit is that it keeps the original environment clean. But what do I mean by this?

 

For example, to run an application that contains a web app written in node js, a console app written in .net, and both connect to a mysql database.

 

Under normal circumstances, we’d have to install .net on the OS, along with Docker and the database. But if we use Docker, the only thing we need is a system capable of running Docker, and we’ll create a container containing .NET with the .NET console app, a container with node for the web app, and a container with the database.

 

Because each container runs independently, any user who runs the container, or if we run it on a server, will get the same result, so the classic "it works on my machine" problem is a thing of the past.

 

Many times, such as with Amazon Web Services, we’ll develop locally in Visual Studio, and when it’s time for deployment we’ll use ECS, which is basically Docker.

 

5 - Installing Docker

First let’s clarify that we’re going to work with Docker for Windows, since Docker for Windows is not exactly the same as Docker for Linux and Mac.

If you’re a Linux or Mac user, Docker has good tutorials for how to install it. The process for Mac is similar, since you also need a virtual machine, but for Linux it can be run natively.

 

5.1 - Docker on Windows

Basically what we do is run a small Docker on our windows, but not just any Windows will do, it needs to have the following characteristics

  • Windows 10
  • 64 Bits
  • Home, Pro, or Enterprise version
    • May 2021 Update: Docker for Windows is now available for the Windows Home version as well.

If you previously installed Docker Toolbox or Docker Machine, it may cause issues, so I recommend uninstalling them.

 

Docker for Windows is not recommended for use in production; for that, you need the Linux version. The commands are the same, the only difference is that it allows scalability and a few more features. The Docker team values stability over additional features.

Docker for Windows runs on Linux, inside a Hyper-V virtual machine.

docker inside hyper-v

But we need to be clear that this lets us run Docker commands in PowerShell and have them work correctly, which is amazing.

 

5.2 - Install Docker on Windows

First, we need to have Hyper-v installed, because there are other methods for installing docker that don’t use Hyper-V but leave your PC cluttered with extra installations and don’t work properly.

To install Hyper-V, you need Windows 10 Pro or Enterprise.

Once you have Windows 10 Pro, go to control panel -> programs and features -> turn windows features on or off

There, look for the folder labeled "Hyper-V" and select it.

By clicking OK, Hyper-V will be installed.

 

5.2.1 - Possible problems with Docker on Windows

Sometimes, this isn’t enough and when we reboot the PC it says it can’t run Docker.

One of the most common cases is because you need to enable the "hardware virtualization machine" functionality in the BIOS, called "Intel VT/VT-x" or "AMD-V".

You also need to make sure that "Second level address translation" (SLAT) and "Data execution protection" are enabled as well.

 

5.3 - Downloading and installing Docker for Windows

Once we’ve reached this point, all that remains is to go to the Docker website and download the version you need for your machine, always choosing the stable version, and install it.

 

Once installed, you’ll see Docker available in the taskbar, that little whale icon.

docker taskbar

You can click on it to open the interface, but I personally have been working with Docker for over a year and haven’t opened the interface even once. All we care about is the command line.

 

To check if Docker is properly installed, open PowerShell and run the command docker version, which will give you a response similar to the following:

Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:23:10 2020
 OS/Arch:           windows/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:29:16 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

As you can see, it indicates that the client is running on Windows but the Docker server is running on Linux because of the Hyper-V virtual machine we installed in point 5.2.

 

Conclusion

Using Docker during both the development stage and when deploying to production is extremely useful, as it greatly simplifies our lives.

 

In C# the need isn’t as strong since Visual Studio itself compiles and lets you debug applications effectively, but in other languages like PHP, it saves you from having to install PHP+Apache in your development environment.

 

Running tests inside Docker ensures you have a closed and controlled environment for their correct operation.

 

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

Uso del bloqueador de anuncios adblock

Hola!

Primero de todo bienvenido a la web de NetMentor donde podrás aprender programación en C# y .NET desde un nivel de principiante hasta más avanzado.


Yo entiendo que utilices un bloqueador de anuncios como AdBlock, Ublock o el propio navegador Brave. Pero te tengo que pedir por favor que desactives el bloqueador para esta web.


Intento personalmente no poner mucha publicidad, la justa para pagar el servidor y por supuesto que no sea intrusiva; Si pese a ello piensas que es intrusiva siempre me puedes escribir por privado o por Twitter a @NetMentorTW.


Si ya lo has desactivado, por favor recarga la página.


Un saludo y muchas gracias por tu colaboración

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café