Hey everyone! In this post, Iâll give you a brief introduction to Docker, but I wonât focus on installation. There are already plenty of tutorials out there that cover how to set up Docker on your OS, so thatâs not the focus here.
Instead, the goal of this post is to:
- Help you understand what is Docker.
- The problem it solves.
- Concepts of a Docker application.
- Walk you through the basic Docker commands and get a simple Nginx server up and running.
So What is Docker? đ€
Letâs start with what Docker is not. Docker is not a Virtual Machine (VM). While itâs common to compare Docker to a VM because it helps us grasp its functionality, in reality, Docker is just a process running on your machine. Nowadays, Docker is more of an ecosystem made up of many tools and software, such as:
- Docker Engine â The core software behind the entire solution. Itâs both the daemon responsible for containers and the client used to send commands to the daemon.
- Docker Compose â A tool used to define and manage multi-container applications based on configuration files.
I didnât mention Docker Machine or Docker Swarm, as theyâre not aligned with the focus of this post, but feel free to look them up if you're interested.
The Problem it Solves? đ€š
We can say that the same Java philosophy of "Write Once, Run Anywhere" applies to Docker, but with a slight tweak. In Dockerâs case, itâs more like "Build Once, Run Anywhere."
Why? Because with Docker, you package your entire applicationâincluding the code, dependencies, and environmentâinto a container. Keep in mind, weâre still going to talk about containers, images, and other Docker concepts in the next section. This ensures that your app runs exactly the same way on different machines, regardless of the operating system or underlying setup. No more "it works on my machine" issues!
Before Docker... đ”
Developers had to manually install dependencies, set up environments, and deal with compatibility problems across different OSes. Moving an app from development to production could be a nightmare.
Other Solutions That Came Before đ ïž
There were tools that tried to solve these issues:
- Virtual Machines (VMs) â Provided isolation but were resource-heavy and slow.
- Vagrant â Helped manage environments with lightweight VMs but still relied on virtualization.
- Configuration Management Tools (Ansible, Chef, Puppet) â Automated setup but didnât fully isolate dependencies.
- LXC (Linux Containers) â Introduced containerization but lacked Dockerâs ease of use and developer-friendly tooling.
Docker simplified everything by making containerization efficient, portable, and easy to use.
Some Docker Concepts â
Images đŒïž
Think of images as blueprints for your application. They contain everything needed to set up and run your app, including the OS, dependencies, and configurations. You can download pre-built images from repositories like Docker Hub, GitHub Container Registry, or Google Container Registry. Or, if you need something custom, you can build your own image with everything your app requires, using a Dockerfile as a recipe.
Containers đŠ
If an image is the blueprint, then a container is the actual running instance of that blueprint. Containers are lightweight, portable, and isolated environments where your application runs. You can spin them up, stop them, and remove them with simple commands.
Dockerfile đ
Is a text file that contains instructions to build a custom Docker image. It defines the base image, the dependencies to install, and the commands to run. Itâs like a recipe for creating your appâs environment in a Docker container.
Lets Cook đł
Finally, letâs get into action! In this section, weâll download an Nginx image, run it, and do some fun testing.
First, letâs check if Docker is correctly installed on your OS by running:
docker -v
If you're on Linux like me and havenât added Docker to the user group, you might need to run the command with
sudo
privileges.
The output should look something like this (though it may vary depending on your version):
Now that weâve confirmed Docker is installed correctly, let's check which images we may already have on our machine. To do this, run:
docker image list
This will display a list of all the images available. Hereâs an example from my setup:
As you can see, I donât have any images on my machine right now, but not for long! Letâs download the Nginx image using the following command:
docker pull nginxdemos/hello
In the image above, we see Docker layers in actionâone of Docker's greatest advantages. We'll dive deeper into this in a future post, but feel free to look it up in the meantime!
Now, when we list the local images again, we get the following result.
Nice! Now that we already have our Nginx image locally, we can run it and see it in action as a container. Technically, we could just run docker container run nginxdemos/hello
. But that wouldnât be much fun because we wouldnât be able to access the running Nginx server from our machine. This happens because, by default, Docker containers are isolated from the host.
To make things more interesting, weâll add some parameters when running the command to properly expose the containerâs port and interact with it.
Now, letâs run:
docker container run -it --rm -p 8080:80 nginxdemos/hello
Hereâs a quick explanation of the parameters used in the command.
Parameter | Description |
---|---|
-i |
Runs the container in interactive mode, allowing user input. |
-t |
Allocates a pseudo-TTY (terminal), making it behave like a regular terminal session. |
--rm |
Automatically removes the container once it stops, preventing clutter. |
-p 8080:80 |
Maps port 80 inside the container to port 8080 on the host, allowing access from the browser. |
And now if we visit http://localhost:8080/
we will see the Nginx Hello World page :)
Yeah, thatâs nice and all, but it could be even more fun! Letâs create our own HTML file and make Nginx serve it instead of the default page.
To do this, just run the following commands on Linux:
mkdir -p ~/tst && echo '<h1>Hello Docker</h1>' > ~/tst/index.html
This will create a file called index.html
inside the a tst
folder in your home directory (~/).
Now, let's run:
docker container run -it --rm -p 8080:80 -v ~/tst:/usr/share/nginx/html nginxdemos/hello
In the command above, we created a volume between our host machine and the Docker container running Nginx using the -v parameter. Now, if we visit http://localhost:8080
again, we will see the "Hello Docker" message.
So thatâs it for now, guys! I know you may have many questions about Docker, volumes, Dockerfiles, how to create our own images, more details about all the parameters we can use when initializing a container, how to manage running containers, and more. Weâll cover all these topics in future posts. This one was just a way for us to dip our toes in the water. Please feel free to drop your comments belowâquestions and feedback are always welcome!
Talk to you soon! đ«¶
Top comments (0)