DEV Community

Cover image for A Simple Guide to Containerization for Beginners: Docker.
Mukhil Padmanabhan
Mukhil Padmanabhan

Posted on

A Simple Guide to Containerization for Beginners: Docker.

With time, developers look for easier ways to manage and deploy applications. And one such tool that gained a lot of traction in the past few years is – Docker. But if you’re new in development; you’d ask ‘what is docker?’ OR why everyone’s talking about it? Don’t worry! You’ve come to the right place! In this blog; we’ll learn what is docker, why it is so helpful and then we’ll go through step by step guide on how we can containerize a simple web application.


What is Docker?

Docker is a platform that let developers to easily pack, ship, and run any application as a lightweight container. Containers are completely transportable and can be executed consistently on any machine setup with docker, not matter if they are a local machine or cloud virtual machines.

Think of Docker as a container for your code. Not unlike how shipping containers can ship goods around the world in a standard container, Docker containers allow you to run your app in any container without being concerned about what's inside.


Why Docker is Useful

Docker solves a lot of common problems that developers face:

  • Consistency Across Environments: Suppose you developed an application on your local machine and everything is working perfectly fine but when you deployed it to a server, everything broke. Docker provides a solution for this, it creates a standardized environment (container) in which your app will be running exactly the same way, no matter where you deploy.
  • Isolation: Each container of Docker is isolated from other containers, that means if you are running multiple services (for example, database and web-app), they won’t be interfering with each other.
  • Lightweight and Fast: Docker containers are lightweight as compared to traditional virtual machines (VMs) as they share the host operating system, so they start up quickly and use less resources.
  • Portability: As Docker containers have everything in them that is required to run an app, you can easily move them between different environments i.e. from your laptop to a server on the cloud without any problems.

Containers vs. Virtual Machines (VMs)

To understand why Docker is so powerful, lets compare containers to traditional virtual machines (VMs). Both Docker containers and VMs are used to isolate applications but there a few key differences:

Feature Docker Containers Virtual Machines (VMs)
Boot Time Starts in seconds Takes minutes to start
Size Lightweight (MBs) Heavy (GBs)
Isolation Shares the host OS Full OS for each VM
Resource Usage Efficient, low overhead Requires more resources
Portability Easy to move between environments Harder to migrate

In simple words, a VM is like a fully furnished apartment; it includes everything starting from the walls to furniture (the full operating system and the application) whereas, Docker container is like just a furnished room; it’s small, lightweight and contains only what’s required for the applications along with the shared common resources (the OS) with other containers.


How Docker Works

Docker uses a client-server architecture:

  • The Docker client is what you, the developer, interact with (e.g., using commands like docker run).
  • The Docker daemon (server) does the heavy lifting by managing containers.
  • Docker images are blueprints for containers. When you create a Docker container, you're basically creating a running instance of a Docker image.

Key Concepts:

  • Docker Images: A snapshot of everything your app needs to run — think of it as a template for containers. Images can be built from a file called a Dockerfile.
  • Docker Containers: A running instance of a Docker image. You can think of it as an isolated environment where your app runs.
  • Docker Hub: A repository where you can find pre-built Docker images. You can either pull existing images from Docker Hub or push your own.

Step-by-Step Guide to Containerizing a Simple Web Application

Let’s go over how you would containerize a simple web application using Docker. In this example, we’ll create a basic Node.js web application and then containerize it with Docker.

Step 1: Set Up Your Web App

First, we’ll create a simple Node.js app.

- Initialize your Node.js app:

Image description

- Create the web server by installing express and writing the following code in app.js:

Image description

Step 2: Write a Dockerfile

Now, we’ll create a Dockerfile to tell Docker how to build an image for this app.

Create a file called Dockerfile (no file extension) and add the following code:

Image description

This Dockerfile does the following:

  • Uses the Node.js base image.
  • Sets the working directory inside the container.
  • Copies the necessary files from your local machine into the container.
  • Installs the Node.js dependencies.
  • Exposes port 3000 (so you can access the app).
  • Defines the command to run the app.

Step 3: Build Your Docker Image

Next, you need to build the Docker image from the Dockerfile. Open your terminal and run:

Image description

This command tells Docker to build an image using the current directory (.) and tag it as my-docker-app.

Step 4: Run Your Docker Container

Once the image is built, you can run your app inside a Docker container:

Image description

This command tells Docker to run the container and map port 3000 on your machine to port 3000 inside the container.

You can now visit http://localhost:3000 in your browser, and you should see the message "Hello, Docker!".

Step 5: Pushing to Docker Hub (Optional)

If you want to share your Docker image with others, you can push it to Docker Hub.

First, tag the image:

Image description

Then, push it to Docker Hub:

Image description

Now, anyone can pull and run your Docker image from Docker Hub!


Benefits of Docker

  • Consistent Development Environment: Your app will run the same everywhere (local, staging, production).
  • More Improved Collaboration: Teams can share Docker images, helping make collaboration on even large projects possible.
  • Simplified Deployment: Docker makes deploying your applications to different environments incredibly easy, and guarantees that your application will run the same way everywhere.
  • Resource Efficiency: Containers are lightweight and require fewer resources than traditional VMs which makes them an ideal choice for modern microservices architecture.

Conclusion: Why Docker is Important for Modern Development

Docker changes the way applications are built, tested, and deployed by providing a consistent, lightweight, and portable environment for running software. For developers, Docker makes it easier to configure and manage applications as well as dependencies and deployment making it easier to focus on what matters most. This ultimately allows them to focus on building better software.

Whether it’s a small project or a large-scale distributed system, Docker can help make your life easier and your workflow more efficient. When you have a good understanding of containerization, you’ll be in a better position to tackle the complexities that come with modern software development.

I hope this post has helped demystify Docker for you! Be sure to follow along as we dive into some more advanced Docker topics, as well as other awesome tools for developers.

Top comments (2)

Collapse
 
detzam profile image
webstuff

why do all of you copy all the fies from root in the container? i mean you relly need dokerfile in the container?

Collapse
 
mukhilpadmanabhan profile image
Mukhil Padmanabhan

@detzam Copying all the files from the root is a common approach in simple Docker setups, but you're right, it’s not always necessary. Ideally you should only copy what is needed for the app to run, like the source code and dependencies, to keep the container lightweight. As for the Dockerfile, you don’t actually need it inside the container; it’s just used to build the image. You can exclude unnecessary files by using a .dockerignore file, similar to .gitignore, to make sure only essential files are copied