What is Docker and how to use it in practice

What is Docker and how to use it in practice

If you’re tuned into the software community, you’ve likely heard of Docker. With a tremendous surge in popularity recently, it continues to attract users at a rapid pace, including many global enterprises whose infrastructure relies on it.

But what lies behind this popularity and how does it work? Follow along with this article to delve into the concept, what this technology provides, and how to explore Docker with some hands-on experience. Check out the topics below:

  • What is Docker?
  • Consistency enables easy project management
  • Provides scalability
  • How does Docker work?
  • How do I use Docker?
  • Conclusion

What is Docker?

Docker allows you to run software in an isolated environment called a container. A container is similar to a virtual machine (VM), but operates in a completely different manner.

Thus, while providing much of the isolation that a VM offers, containers use only a fraction of the resources available in your product suite.

Consistency Enables Easy Project Management

Let’s say you’re coding a web application and developing it on your local machine, where you test it. In some scenarios, you run it on a test server and soon you’ll deploy it on a large production server for the world to see.

Wouldn’t it be great if you could consistently spin up the same environment, exactly the same way, on all your devices? Well, if the answer is “yes,” Docker can help.

If your web application runs properly within a Docker container on your local box, it can run on your test server, on the production server, and anywhere else.

This makes managing project dependencies incredibly easy. Not only is it straightforward to handle external libraries and modules called directly by your code, but the entire system can be configured to your liking.

So, if you have an open-source project where a new user wants to download and run it, using Docker, the process is as simple as starting a container.

Provides Scalability

Now, if you use Docker to create services with varying demands (such as websites or APIs), scaling your provisioning is incredibly easy by simply spinning up more containers (provided everything is architected correctly).

For this, there are various frameworks that enable orchestrating container clusters, such as Kubernetes and Docker Swarm, but that’s a topic for another post. So, stay tuned to our blog content!

How Docker Works

Well, many people believe Docker is a way to run a virtual machine, but it’s not quite like that. A virtual machine runs on simulated hardware, i.e., a fully self-contained operating system.

However, containers inherently share the host’s kernel. This means containers have incredibly better performance than virtual machines, depending on what they’re doing.

Thus, it’s possible to create hundreds, if not thousands, of containers on your PC. Moreover, starting up a container will take seconds or less, compared to minutes for many VMs.

As containers are lightweight, it’s common practice to run all aspects of an application in different containers for greater maintainability and modularity.

For instance, you can have different containers to spin up the database, redis, nginx, and so forth, all managing to communicate with each other seamlessly.

How Do I Use Docker?

In order to exemplify Docker usage, let’s build a minimal web server in a container. In the interest of keeping it simple, we’ll use Flask, a web microframework in Python.

Here’s the program we want to run: main.py. You can create a new folder, which we’ll call app, and save this file there:

from flask import Flask app = Flask (__ name__) 

@ app.route (‘/’)

 def home ():

     return ‘Hello from dentro de um contêiner Docker’

if __name__ == ‘__main__’:

      app.run(host = ‘0.0.0.0’, port = 80)

In this case, you don’t need to worry if you’re not familiar with Flask or Python, as all you need to know is that this code returns a string on localhost:80.

However, before getting this app running, we need to understand what a container is. They are defined by an image (like a recipe), being just a running instance of an image. Thus, you can have multiple containers running from the same image.

Using Docker Hub

In any case, there is a public collection of images called Docker Hub, which contains official contributions but also allows anyone to make their own contributions.

Additionally, we can fetch an image from Docker Hub and extend it to do what we want. To do this, we need to write a Dockerfile – a list of instructions to build an image.

The first thing we’ll do in our Dockerfile is specify which image we want to use/extend. As a starting point, it would make sense to choose an image that already has Python installed. We’ll use one with Python 3.7 running on Debian stretch.

Here’s our Dockerfile:

FROM python: 3.7-stretch

COPY ./ /app

WORKDIR /app

RUN pip install Flask

CMD [“python”, “main.py”]

In the first line, we specify which image we would like to run, then we copy the contents of the folder we are in to a folder called app. Right after that, we install the Flask package in the container and run the command “python main.py”.

At this point, we have a hierarchy configuration like this:

app └──main.py Dockerfile

Now, you can execute the command `$docker build .`, which tells Docker to look for a Dockerfile in the current directory (`.`). But to make it a bit easier to run, let’s give a name to our built image, called a tag.

$ docker build -t flask-hello .

Getting It to Work

Since this is the first time we’re using this Python image from the hub, the download takes a minute or two, but in the future, it will be available locally. After this, we have a new image to produce a container running `$docker run flask-hello`

Our container runs successfully, and we get some output from Flask, indicating that it’s ready to serve our page. But if we open a browser and visit localhost, there won’t be anything to see.

The reason, of course, is that our server is running inside the container, which has an isolated network stack. Therefore, we need to forward port 80 to be able to access our server.

So, let’s kill the container with CTRL + C and run it again with port forwarding.

$ docker run -p 80:80 flask-hello

And it works!

Conclusion

The first time you visualize this process, it may seem a bit verbose. But believe me, there are tools like docker-compose to automate this workflow, and they are very powerful when running multiple containers/services.

Oh! And here’s a secret: when you start understanding Docker, it’s like you’re part of an elite club. You can easily download and run anyone’s software without needing configuration, with just a few keystrokes.

In summary:

  • Docker is a fantastic way to run code in a reproducible, cross-platform environment.
  • Once you set up your images, you can run your code anywhere, instantly.
  • Building different components of an application, in different containers, is a great way to make your application easy to maintain.
  • Although this article has focused on web services as an example, Docker can help in many places.
  • Containers, container orchestration, and scalable deployment are extremely important areas for software companies, and it’s worth keeping an eye on them!

If you want to learn more about similar topics, check out other content on our blog and sign up to receive information about our open positions. Here at Ubistart, we always have new opportunities!

Follow our newsletter!

Follow our newsletter!

Recent articles:

By browsing, you accept the cookies that we use on this website to improve your experience. More information.

Ao navegar neste site, você aceita os cookies que usamos para melhorar sua experiência. Mais informações.