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?¶
"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. |
2. Docker Image: the cake tray
Our Docker Image |
3. Docker Container: the CAKE itself
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.
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
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.
- 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.
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
Sucess! |
And voilá, we got Wordpress! |
No comments: