Docker explained simply (for those who want to get started)

   

If you have ever written code that worked perfectly on your laptop, but stopped cooperating as soon as it was uploaded to the production server or a colleague's computer, you know exactly what I am talking about. It's the classic nightmare: "But it worked on my PC!".

Welcome to the world of Docker, the tool that has radically changed the way we build and deploy software.

🔗 Do you like Techelopment? Check out the website for all the details!

1. The freight transport analogy: Why Docker is revolutionary

Before the invention of standardized shipping containers, loading a ship was chaos: sacks of grain, barrels of oil, and crates of machinery were stowed in different ways, with huge risks and endless time required. Then, someone invented the standardized container. It doesn't matter what's inside: the ship, the crane, and the truck know exactly how to move that metal box.

Docker does exactly this with your software. Instead of worrying about specific server configurations, the operating system, or installed libraries, you package your application along with everything it needs (dependencies, runtime, configuration files) into a "container." If the container runs on your PC, it will run anywhere.

⚠️ A word of caution:

When people say "the container will run anywhere", they mean any machine on which Docker is installed. Just as you need a PDF reader to read a PDF file, to run a container you need the Docker engine to be present on the host system. The good news is that, once Docker is installed, you will no longer have to worry about configurations, library versions, or specific dependencies: the container will already contain everything needed to run exactly as it did on your PC.


2. What is Docker in simple terms?

Docker is a platform that allows you to create, deploy, and run applications within containers.

To understand the fundamental difference between the two pillars of Docker, let's use a culinary metaphor:

  • The Image (The Recipe): It is a static, read-only file that contains the code, libraries, and instructions needed to run the app. It's like a cookbook: it says exactly what you need, but it's not the finished dish.
  • The Container (The Cooked Dish): It is the running instance of the image. When you "run" an image, you create a container. It is the dish you have actually cooked following the recipe, ready to be served.

3. Why you should use it

Here are three reasons why Docker has become the industry standard:

  • Portability: "Write once, run anywhere". Your container doesn't know (and doesn't care) which operating system it is on; it will work the same way.
  • Isolation: Each container has its own clean environment. You can have two projects that require different versions of the same language (e.g., Python 3.8 and Python 3.12) on the same PC without them conflicting.
  • Resource Efficiency: Unlike virtual machines (VMs), which load an entire heavy operating system, containers share the host system kernel, making them lightweight and lightning-fast to start.

4. The two pillars: Dockerfile and Docker Hub

To start using Docker, you need to know two basic concepts:

  • Dockerfile: It is a simple text file that contains the step-by-step instructions to build your "recipe" (your image). It is where you define which operating system to use, which files to copy, and which commands to run.
  • Docker Hub: Imagine it as a sort of "GitHub for Docker images". It is a public registry where you can download ready-made images (like a ready-to-use version of Nginx, MySQL, or Node.js) created by the community or official producers, saving you hours of configuration.

5. Practical example: starting nginx in one command

You don't have to build everything from scratch to get started. Do you want an Nginx web server up and running on your PC in 5 seconds?

Open your terminal and run:

docker run -d -p 8080:80 nginx

What is happening here?

  1. docker run: Tells Docker to start a container.
  2. -d: Runs the container in the background (detached mode).
  3. -p 8080:80: Maps port 8080 of your PC to port 80 of the container (where Nginx listens).
  4. nginx: Tells Docker to download (if you haven't already) the official Nginx image from Docker Hub.

Now open your browser and go to http://localhost:8080. You will see the Nginx welcome page! To stop everything, just find the container ID and use docker stop <id>.


6. Conclusion: Time to get your hands dirty

Docker can seem intimidating at first, but the learning curve is only steep during the first steps. The ability to create isolated and reproducible environments is not just a technical skill, it's a superpower that will simplify your life as a developer every single day.

My advice? Don't just read. Install Docker Desktop today and try running an image. What will be the first service you containerize?


⭐ Before we go, one thing to keep in mind

Containers and Images are NOT the same thing. To understand why I talk about both, think of this distinction:

Consider that you want to build a house:
  • The Image (The Architectural Blueprint): It is the file that contains all the drawings, measurements, instructions, and specifications on how the house must be built. The blueprint itself is static; you cannot live inside it. It is a file (or rather, a package of files) that lives on your hard drive or on Docker Hub.
  • The Container (The Built House): It is the actual house, built by following that blueprint. It is dynamic; you can enter it, you can turn on the lights, you can cook. The container is the "live" and running instance of that image.

Why is it important to distinguish them?

You can have many houses from a single blueprint: With a single "image" (the blueprint), you can start 10 or 100 identical "containers" (the houses) simultaneously.

Immutability: The image is "read-only". If you want to change something in the house (for example, painting a wall), you don't modify the original blueprint (the image): you create a new version of the project or make temporary changes that live only in the container.

The workflow:
  • You write the Dockerfile (the instructions).
  • Docker creates the Image (the ready-to-use file).
  • You run docker run and Docker creates the Container (the active process).



Follow me #techelopment

Official site: www.techelopment.it
facebook: Techelopment
instagram: @techelopment
X: techelopment
Bluesky: @techelopment
telegram: @techelopment_channel
whatsapp: Techelopment
youtube: @techelopment