Jay's Tech Bites Logo

Getting Started with Docker: A 2024 Step-by-Step Guide for Beginners

Master Docker: A 2024 Beginner’s Guide to Understanding Containers and How They Simplify App Development

Jay McBride
  • Jay McBride
  • 5 min read
Shipping containers stacked on top of eacher. Docker logo in the sky above with 2024 written in it.
Image by Jay's Tech Bites

Docker has become one of the most essential tools for developers, simplifying software delivery by containerizing applications and their dependencies. In this guide, we’ll break down Docker step by step, explain how it differs from traditional virtualization, and show you how to get up and running quickly.


1. What is Docker?

Before diving into the technical details, it’s important to understand the basics. Docker is an open-source platform that allows developers to automate the deployment of applications inside lightweight containers. These containers package everything your app needs, ensuring it runs consistently across different environments, whether on your local machine, a colleague’s setup, or the cloud.


2. How Docker Works: Containers vs. Virtualization

Traditional Virtualization

In traditional virtualization, virtual machines (VMs) are used to run multiple operating systems on a single physical machine. A hypervisor (e.g., VMware, VirtualBox) sits between the host system and the VMs, managing their resources. Each VM includes a full guest OS, complete with its own kernel, system libraries, and dependencies, making VMs resource-intensive.

Key Drawbacks:

  • Resource Intensive: VMs run entire operating systems, consuming a significant amount of CPU and memory.
  • Slow Startup: Since VMs boot a full OS, they can take several minutes to start up.
  • Complex Management: Maintaining and updating VMs is more complicated because each behaves like a standalone machine.

Docker and Containers

Docker takes a more lightweight approach. Instead of virtualizing entire operating systems, Docker creates containers that run on the host OS. These containers share the host system’s kernel, but are isolated in their own environment, containing only the app and its dependencies.

Why Docker Containers are Different:

  • Lightweight: Containers share the host OS kernel, making them much smaller than VMs.
  • Fast Startup: Since containers don’t need to boot a full OS, they start almost instantly.
  • Portability: Containers run consistently across different environments, eliminating the “works on my machine” problem.
FeatureVirtual Machines (VMs)Docker (Containers)
IsolationFull OS isolationProcess-level isolation (shares OS kernel)
Resource OverheadHigh (full OS, system libraries, etc.)Low (shares host OS kernel)
Startup TimeSlow (boots entire OS)Fast (only runs processes)
PortabilityRequires reconfiguration across systemsRuns the same everywhere
Use CasesComplete OS environments, legacy appsMicroservices, cloud-native apps

Docker’s container-based approach provides significant efficiency gains over traditional VMs. This efficiency is why many developers and organizations have embraced Docker for microservices, CI/CD pipelines, and cloud-native applications.


3. Installing Docker

To start using Docker, you’ll need to install it on your system. Docker is available for Windows, macOS, and Linux.

For Windows and macOS:

  1. Download Docker Desktop from the official Docker website.
  2. Follow the installation instructions.
  3. Launch Docker Desktop and ensure it’s running.

For Linux:

  1. Open your terminal.
  2. Use the package manager (e.g., apt for Ubuntu) to install Docker:
    sudo apt update
    sudo apt install docker-ce docker-ce-cli containerd.io
    
  3. Start Docker:
    sudo systemctl start docker
    sudo systemctl enable docker
    

For more detailed installation steps, check Docker’s official documentation.


4. Key Docker Concepts

Now that Docker is installed, let’s explore the core concepts:

  • Dockerfile: A text file containing instructions to build a Docker image.
  • Image: A read-only template used to create Docker containers.
  • Container: A running instance of a Docker image.
  • Docker Hub: A cloud-based registry where Docker images are stored.

5. Building Your First Docker Container

Let’s walk through building a simple Node.js application inside a Docker container.

Step 1: Create a Node.js App

  1. Create a directory for your project:
    mkdir my-docker-app
    cd my-docker-app
    
  2. Initialize the app and create a basic server:
    npm init -y
    
  3. Add the following code to server.js:
    const http = require('http');
    const server = http.createServer((req, res) => {
      res.write('Hello, Docker!');
      res.end();
    });
    server.listen(3000);
    

Step 2: Create a Dockerfile

Create a Dockerfile to define the Docker image for your app:

# Use an official Node.js image from Docker Hub
FROM node:14

# Set the working directory
WORKDIR /app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy the rest of the app
COPY . .

# Expose the port
EXPOSE 3000

# Run the app
CMD ["node", "server.js"]

Step 3: Build and Run the Docker Container

  • Build the image:
    docker build -t my-docker-app .
    
  • Run the container:
    docker run -p 3000:3000 my-docker-app
    

You can now access your app at http://localhost:3000.


6. Managing Data with Docker Volumes

By default, Docker containers are stateless, meaning any data generated inside the container is lost once it stops. To persist data, you can use Docker volumes.

To create a volume:

docker volume create my-volume

Attach the volume to your container:

docker run -d -p 3000:3000 -v my-volume:/app my-docker-app

7. Pushing Images to Docker Hub

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

  1. Tag your image:
    docker tag my-docker-app username/my-docker-app
    
  2. Log in to Docker Hub:
    docker login
    
  3. Push your image:
    docker push username/my-docker-app
    

8. Wrapping Up: Why Docker?

Docker simplifies development and deployment by creating isolated environments for your applications. It helps developers:

  • Avoid the “works on my machine” problem.
  • Easily replicate production environments locally.
  • Speed up the development process with containerized services.

Now that you’ve built your first Docker container, it’s time to explore its full potential. Whether you’re working on web apps, microservices, or large-scale systems, Docker ensures consistent environments across your dev, test, and production setups.

What will you containerize next? Let me know in the comments!

Comment

comments powered by Disqus

Want more insights delivered right to your inbox?

Stay up-to-date with the latest in tech, development tips, and subscribe to my newsletter. Join the growing community of developers and tech enthusiasts today!

Sign up now and never miss a post!
Jay McBride

Written by : Jay McBride

Welcome to Jay’s Tech Bites! I’m Jay McBride, a tech enthusiast breaking down the latest trends in tech. Whether you're tech-savvy or just curious, I’ll keep it simple and interesting. Let’s explore the tech world together, one bite at a time.

Recommended for You

Chalkboard with the words Fix It written on it. Betwee the two words is a stop watch. In the foreground are flames hinting at the topic of a urgent hotfix.

The Ultimate Hotfix Guide for Developers Using Git: Patches, File Exports, and Cherry-Picking

Master Git Hotfix Techniques: Apply Critical Fixes Fast with Patches, File Exports, and Cherry-Picking.

6 min read

Hand holding endless infinity sign

Rails 8.0 Beta 1: No PaaS Required – The Future of Developer Simplicity

How Rails 8 Beta 1 Revolutionizes Development with Conceptual Compression and Deployment Ease

3 min read