Meet HubJur

Docker Tutorial for Dummies

Today we're going to talk about what Docker is, how you can configure it, and how we'll use it to run some future projects.

Hi, if this is your first time here, our content is focused on INNOVATION FOR LAWYERS and, in particular, this series is focused on PROGRAMMING FOR BEGINNERS.

I also present to you guys and girls the video I made on this topic from my YouTube channel, which is where I share some thoughts, tips and projects I am currently working on with other tech-fanatics, whether you are a developer, lawyer or enthusiast. Please also consider subscribing to my channel to not miss future videos and to join our live "coding sessions" and a nice, relaxing tech-related chat -- I promise to keep things interesting. The content is in Portuguese, but you can watch my videos with subtitles (maybe I will switch to English in the future, if this makes sense).


All set? Then tag along!


What is Docker?¶


Imagine that you programmed beautiful code, everything working, without bugs. You finally upload your code to the final environment (production) and, to your dismay, nothing works!

"But it works on my machine" 
- every dev ever

This is something more common than you might think. The problem with an apparently perfect program when it is sent to the final environment can have a number of reasons: different server configurations, different versions of auxiliary programs or the environment itself, operating system, package updates... It is not possible to know exactly how your program will behave on another machine.

Docker was born to solve this (among other things). Imagine taking your local development machine and packaging absolutely all of its programs and settings, then running exactly this "package" in the final environment. Docker does this. With Docker you can be sure that if your local machine and the remote machine (server) use the same image (that is, the same "package" of settings) they must run your program in the same way. It's like you're uploading your machine to the server.



ILLUSTRATING DOCKER WITH AN ANALOGY

There are many analogies on the Internet to explain what Docker is and how it works and each of its particularities. The Docker symbol itself (the whale that carries a bunch of containers) is an analogy on how it works. 

So let's create an analogy for what Docker is and each of its main features: LET'S BAKE A CAKE!



1. Docker: the Kitchen

Imagine you want to bake a CAKE. And to make your cake, you obviously need the entire structure to make that cake: oven, countertop, cutlery. You need a KITCHEN.

To bake a cake, we have to have the structure!

Having a kitchen with minimal tools is the equivalent of having the Docker platform installed on your machine. Docker provides the basic features needed to run a system. If we want to make the cake viable we must first ensure that the house has a kitchen, just as if we want to run "machines" through the Docker system, we need to have Docker installed. And so let's install Docker first.

🐳 Installing Docker

Originally, Docker is a platform released for the Linux operating system and, for that reason, there are many great tutorials to install it in the most different distributions. You can, for example, follow this great tutorial from Digital Ocean that teaches you step-by-step how to install the docker on Ubuntu.

By the way, don't have a Digital Ocean account yet? It's a great service if you want to have a cloud server, with a full linux (or windows) machine. You can upload your code and have an online service 100% of the time at a very affordable cost. If you want to create an account there and try out the service, I highly recommend it -- use my referral link and get a $100 credit to get started: https://m.do.co/c/629292485ee8 

However, considering that many possibly use Windows or MacOS, there is now a very practical solution for installing Docker which is Docker Desktop.

Docker Desktop

Docker Desktop installation was very simple and I had to restart the computer after it was finished. When the program was restarted, an error message alerted me to the need to update the WSL2Kernel (the "Windows Subsystem for Linux") which is nothing more than an application that allows Windows to execute a series of Linux commands directly.

WSL2 alert message

And the link to download the WSL2 Linux Kernel package

Following the link indicated in the alert message, I downloaded the WSL2 package and installed it. After that, when restarting Docker Desktop everything was fine, like this:


Let's test Docker now. In VSCode (or in Powershell itself), we run the command that Docker mentions there on its home page. You will see that Docker will download the informed image, will run the container (we will explain all this later) and will make the container available on port 80 of our machine.

Running Docker in our terminal, everything working fine 🥳 

And when running the command, Docker Desktop already recognizes the running container

Finally, by accessing the localhost address, we were able to view this page that was generated by the container we've set up.


For those who are lost, what have we done here? We install the Docker platform on our machine and run a command that fetches a "picture" of a complete machine from the Internet and replicates it on our local machine. The machine we "replicate" here is an identical copy of the machine that the person who created this image made available. If we run the same command from another computer, the machine created will be identical too. And that's the power of Docker.


2. Docker Image: the cake tray

Since we've already ensured that we have the kitchen to prepare the cake / Docker installed, let's decide which CAKE TRAY we will use. After all, there's no way to bake a cake without having a way to bake it.

Our Docker Image

With the cake tray we can create as many cakes as we want, and they will always come out the same: in this case, round and short. The Docker image has the same rationale: with an image you can generate numerous "containers", which are produced based on this "cake shape". The image (tray) works as a stamp for each container (each cake).

3. Docker Container: the CAKE itself

As we mentioned above, each CAKE produced with the tray is a CONTAINER. The cake itself has all the properties of a cake, it works like a cake and can be eaten like a cake. You can bake a cake in your home's kitchen, and by using the same image (tray) to bake a cake in your mother's house, you can create a cake with the same characteristics (round and short).




In other words, if you want to bake a cake just like the previous one, just use the same tray: you don't need to buy (or worse, make) another tray. Containers will always follow what is defined in the image


4. Docker Registry: the Kitchen Shelf

Imagine that you have several cake trays. You can keep them on a SHELF IN THE KITCHEN, where they'll be stored for easy access whenever you want a round cake, a square or a triangle cake.


The Docker Registry works the same way: you can store your Docker images there and when you need to use them, just take them.

Creating a Docker Registry

To create your Docker Registry, or the shelf that will hold the cake trays, you can create a DockerHub account, and we can choose the Personal plan (free) for now.

Create your DockerHub account

We can choose the Personal account for now4

With the registration on DockerHub we can also log into the account on Docker Desktop, leaving the application then connected to the cloud.



5. Dockerfile: the cake recipe

Although our image always comes out with the same shape, round and short, we can have a different cake batter depending on the case. So, even if the base is the same and the cake looks more or less the same at the end, the CAKE RECIPE brings important and detailed instructions so that the contents of the tray (the cake batter) are successfully prepared and the cakes come out of the way we hope.

The recipes for our cake are there

This cake recipe is the file called Dockerfile: it contains instructions about fundamental aspects that must be observed in our image (in the dough that goes into the mold) so that the result is as expected (the cake comes out tasty).

In the Dockerfile you have information about the base that will be used (for example, if we will use as a base a ubuntu linux image, linux mini distribution with node, etc.) and about the application that will be launched (files that compose it, volumes, database).

If you follow the recipe correctly, you will be able to successfully bake a cake every time.

The developer's job is, in large part, to write this cake recipe, making sure that with the well-written recipe the cake will be prepared accurately. Who prepares the cake is a robot -- after all, this kitchen is very futuristic and with the Dockerfile the cakes are prepared in a few seconds, always identical!



6. docker-compose: the party!

Imagine now that we can bake a cake as we want, always in the same way. But at a party, we don't just have cake.

We have soda, sweets, snacks and everything else. Imagine that just like the cake, each of these treats has its own "tray", its own "recipe" and can also be created over and over again.

Let's partyyyyy!!!

We can then set up a "party", linking the "cake" with the other items. To set up this party, we passed the instructions to a program called docker-compose. docker-compose is a program that reads a file with these instructions, and creates all the containers we need (cake, candy, snacks...) to set up our party.

docker-compose example

docker-compose then assembles all the elements and links them together in a way that makes sense for a "system" to work. In other words, he organizes the party.



Docker benefits¶

In an era without Docker it was common that before starting to work a developer (or someone specialized in it) would spend a few hours (or days) configuring the machine and the whole system so that it was working 100%.

Also, uploading a new version of the system to production was a task that took a long time, it was something very tense and often didn't work! (the famous "hey, it worked on my machine"). Differences in the system, some configuration or version of incompatible programs, in short, everything could be a reason to crash the system...

Cake is always the same!

With Docker you can raise the same "cake" in a test environment and once tested and sure it works, you can raise an identical "cake" in production with the peace of mind that it will be up and running.



USING DOCKER IN REAL LIFE

To give you an idea of the power of Docker, let's use it in practice. Be aware that many of the major projects on the Internet today are "dockerized" and you can use them effortlessly.

To exemplify what we can do, let's run a container with a Wordpress image.

If you don't know, Wordpress is a popular platform to create online content, allowing you to create blogs, online stores (e-commerce), portfolios, professional websites and much more. It is a "more complete" alternative than existing platforms today such as Blogger, Tumblr, Wix and others, allowing a high degree of customization and having many plugins available.

We've already talked about how you can build a professional website in the article: How to create a professional website step by step, but we didn't go into using Wordpress. We'll do that in future content.
For this content, just know that installing Wordpress depends on your machine having a web server installed (such as Apache or Nginx) with PHP programming language support and a database such as MySQL or MariaDB. See here on the official website:

Para este conteúdo, basta você saber que a instalação do Wordpress depende que a sua máquina tenha um servidor web instalado (como Apache ou Nginx) com suporte à linguagem de programação PHP e um banco da dados como MySQL ou MariaDBVeja aqui no site oficial:




Therefore, if you want to use Wordpress you must have these services installed on the machine where you will run it. But let's see now what we can do with Docker. Accessing the official image on DockerHub for Wordpress, we found the following:


Instead of setting up an entire machine with PHP, Apache and MySQL, let's create a file to run with docker-compose that:
  • Loads Wordpress;
  • Create a MySQL Database (we're going to pass the database access password in the docker-compose file itself);
  • Links Wordpress and Database to work together.

Our docker-compose assembling database and Wordpress

I will also leave this docker-compose file on GitHub, in the repository I created for this content, which you can access here: https://github.com/Dev-Lawyer/docker-for-beginners. I leave here the recommendation that you analyze the file because I left several comments that can be useful for understanding what I'm talking about here (sorry, they are in Portuguese for now but you can use Google Translate if needed).
Now, to have a Wordpress working, just open the terminal and run the following command from the same folder as the docker-compose.yml file.

docker-compose up

From there, just access http://localhost:8000 from the browser and you will have access to Wordpress, being able to configure it the same way you would if you had installed everything.
Wordpress setup as in its default installation

Sucess!

And voilá, we got Wordpress!

We were then able to get Wordpress upright simply by running a simple command. And the best part: if we need to run this configuration again on a new machine, just run the docker-compose command.


DOCKER MAKES YOUR LIFE EASY

As you can see, Docker makes your life easier and can be very beneficial for your routine as a developer. After working with Docker, you probably won't want to go back to setting up infrastructure "from scratch" anymore.

It is also possible to create more complex structures to work with Docker, with the construction of multiple stage containers and interesting installations that leverage the automation of the "deploying" of the system to production (CD/CI). We will talk about this in the future, in specific content.

That's it, keep studying and exploring what these amazing tools have to offer.

Happy coding!

/* End of the post, see you next time */
Docker Tutorial for Dummies Docker Tutorial for Dummies Reviewed by Octavio Ietsugu on October 24, 2021 Rating: 5

No comments:

Powered by Blogger.