Skip to main content

Docker 101: Understanding Containers from Scratch

Docker Basics and Docker Compose Explained

Docker Through My Lens

Introduction to Docker

Docker is a platform designed to create, deploy, and run applications inside containers. Containers bundle an application with all its dependencies, ensuring consistency across different environments.

Unlike virtual machines, containers are lightweight and share the host operating system kernel, making them efficient for development, testing, and deployment.

Basic Docker Commands

To start using Docker, here are some essential commands:

  • docker run [image] – Runs a container from the specified image.
  • docker ps – Lists running containers.
  • docker ps -a – Lists all containers, including stopped ones.
  • docker stop [container_id] – Stops a running container.
  • docker rm [container_id] – Removes a container.
  • docker images – Lists available Docker images.
  • docker rmi [image_id] – Removes a Docker image.

Creating Your First Docker Container

You can run a simple container by pulling a test image and running it:

docker run hello-world

This command downloads the hello-world image if it’s not already on your system and runs it, printing a confirmation message.

Dockerfile: Building Your Own Image

A Dockerfile is a text file that contains instructions to build a Docker image. It automates image creation and ensures consistency.

Example Dockerfile for a Node.js application:

# Dockerfile
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]

Explanation:

  • FROM node:18 – Uses the official Node.js image version 18 as the base.
  • WORKDIR /app – Sets the working directory inside the container.
  • COPY package*.json ./ – Copies package files for dependency installation.
  • RUN npm install – Installs Node.js dependencies.
  • COPY . . – Copies the rest of the application files.
  • EXPOSE 3000 – Declares port 3000 for external access.
  • CMD ["node", "index.js"] – Defines the command to run the app.

Building and Running Your Image

Build the image using:

docker build -t my-node-app .

Run the container with:

docker run -d -p 3000:3000 my-node-app

Step-by-Step: Create Your Own Docker Container

Let’s build a simple Docker container step by step using a basic "Hello World" Node.js app.

  1. Create a project directory:
    mkdir my-node-app
    cd my-node-app
  2. Create a simple app file:
    echo "console.log('Hello from my container!');" > index.js
  3. Write a Dockerfile:
    FROM node:18
    WORKDIR /app
    COPY index.js .
    CMD ["node", "index.js"]
  4. Build your Docker image:
    docker build -t my-node-app .
  5. Run the container:
    docker run my-node-app

    You should see the message: Hello from my container!

This simple example shows how you can package a basic Node.js script inside a container and run it anywhere.

Why Use Docker Compose?

Most applications require multiple components, like a web server and a database. Managing these containers individually can be tedious and error-prone.

Docker Compose solves this by defining and running multi-container Docker applications via a simple YAML file (docker-compose.yml).

Compose allows you to start, stop, and manage all related containers as a single service with one command.

Key Docker Compose Commands

CommandDescriptionExample
upCreate, build, and start containersdocker-compose up
startStart existing containersdocker-compose start
stopStop running containersdocker-compose stop
downStop and remove containers, networksdocker-compose down
buildBuild or rebuild containersdocker-compose build

Example: E-Commerce Application Using Docker Compose

Suppose you have an e-commerce site running on an Apache web server and a MySQL database storing customer data.

You could manually run:

docker network create ecommerce
docker run -p 80:80 --name webserver --net ecommerce webserver-image
docker run --name database --net ecommerce -e MYSQL_ROOT_PASSWORD=secret mysql:latest

This approach is repetitive and error-prone if you want to scale or manage containers frequently.

Instead, with Docker Compose, you can define your services in a docker-compose.yml file:

version: '3.3'
services:
  web:
    build: ./webserver
    ports:
      - '80:80'
    networks:
      - ecommerce
  database:
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=helloworld
      - MYSQL_DATABASE=ecommerce
    networks:
      - ecommerce

networks:
  ecommerce:

Running docker-compose up spins up both services connected via the ecommerce network automatically.

Docker Socket Explained

The Docker daemon (which manages containers) listens on a Unix socket file, usually /var/run/docker.sock.

This socket acts like a communication channel between Docker clients (like the CLI) and the Docker service.

Real-World Scenario

In CI/CD pipelines, tools like Jenkins often run inside Docker containers but need to control Docker on the host to build or run containers.

By mounting the Docker socket inside the Jenkins container:

docker run -v /var/run/docker.sock:/var/run/docker.sock jenkins

Jenkins can issue Docker commands to the host’s Docker daemon as if it were running directly on the host.

Security Considerations

  • Exposing the Docker socket to containers is a serious security risk.
  • If compromised, attackers can control all Docker containers and the host system.

Conclusion

Docker and Docker Compose are essential tools that simplify the development and deployment of applications by using containers.

Understanding Dockerfiles, basic commands, and how to manage multi-container applications with Compose helps you build consistent, portable environments.

Finally, understanding internal components like the Docker socket helps when automating container management but always be cautious of the security implications.

Comments

Popular posts from this blog

How to Pass CompTIA Security+ SY0-701 in 2 Months (839 Score Breakdown + Resources)

How I Scored 839/900 on CompTIA Security+ SY0-701 — 2-Month Prep Strategy That Actually Worked Score: 839/900  |  Exam: CompTIA Security+ SY0-701  |  Prep Time: 2 Months  |  Total Questions: 76 (including 3 PBQs) I'm not going to sugarcoat it — CompTIA Security+ is not easy, but it is very passable with the right strategy. I cleared it with an 839 out of 900, and in this post I'll share exactly how I did it, domain by domain, so you can replicate the approach without wasting time. My 2-Month Study Plan Month 1 — Domain-by-domain study: Read, take notes, and build comparison tables and mnemonics for tricky concepts. Month 2 — Heavy practice testing: Full focus on practice tests and PBQ simulations. Time management drills every session. The biggest mistake people make is spending 90% of their time reading and only 10% practicing. I flipped that in month 2 — and it made all the difference. Domain 1 — General Security Concepts What to focu...

Email Security Deep Dive: 13 Steps to Keep Your Emails Safe

Email Security Checklist The Email Security Checklist 1. Enable SPF (Sender Policy Framework) What it is: SPF is like a guest list for your email domain. It tells the world that only specific servers are allowed to send email for your domain. How it works: Publish an SPF record in DNS. When someone receives an email claiming to be from your domain, their mail server checks if the sending IP is listed in the SPF record. If the IP is not listed, the email is rejected or marked as spam. Example SPF record: v=spf1 ip4:203.0.113.0/24 include:_spf.google.com -all Only servers in the specified IP range and Google’s mail servers can send emails for this domain. Others are rejected. Points to Note: Prevents attackers from spoofing your domain and sending phishing or spam emails. 2. Enable DKIM (DomainKeys Identified Mail) What it is: DKIM is a digital signature for each email, ensuring that the message hasn’t been tampered with. Ho...

Deep Dive into Cybersecurity: Security+ Level Knowledge Without the Certificate

📚 My Cybersecurity Learning Journey Key Topics from a 17-Hour Security+ Course 🔹 CIA Triad Explained Confidentiality: Ensuring that sensitive data is only accessed by authorized users. This is often achieved using encryption and access controls. Integrity: Ensuring data is accurate and untampered. Techniques like hashing, checksums, and digital signatures help validate that data hasn't been altered. Availability: Making sure systems and data are accessible when needed. Achieved through backups, redundancy, load balancing, and fault-tolerant design. 🔹 Types of Threats Malware: Includes viruses, ransomware, worms, and trojans that compromise devices or networks. Social Engineering: Manipulating users into giving up confidential info. Example: Phishing emails. Insider Threats: Employees or contractors misusing access, accidentally or intentionally. Advanced Persistent Threats (APTs): Long-term targeted attacks, often by well-funded threat actors. Zero...

Job Scams / Telegram Scams in India

Awareness is necessity Job scams and telegram scams have become a rampant issue in India, with many unsuspecting individuals falling victim to these fraudulent activities. These scams not only cause financial loss but also result in emotional distress and a loss of trust in online platforms. In this blog, we will discuss the ongoing job scam and telegram scam in India, and provide some tips on how to identify and avoid falling prey to these scams. The job scam in India has been on the rise, with scammers posing as recruiters or employers offering lucrative job opportunities. These scammers often target job seekers who are desperate for employment and are willing to take any opportunity that comes their way. They use fake job postings, promising high salaries and attractive benefits to lure in their victims. Once the victim is hooked, they are asked to pay a fee for processing their application or for other reasons. In reality, there is no job, and the victim en...

Beware of Instagram Shopping Scams: Tips to Stay Safe

Beware of Instagram Shopping Scams: Tips to Stay Safe In recent years, Instagram has evolved into a popular marketplace, allowing users to discover and purchase products directly through the platform. While this has made shopping more convenient, it has also opened the door to a rise in scams and fraudulent activities. Here’s what you need to know about Instagram shopping scams and how to protect yourself. What Are Instagram Shopping Scams? Instagram shopping scams typically involve fake accounts or websites that mimic legitimate brands. Scammers often create attractive posts featuring trendy products at unbeatable prices, luring unsuspecting shoppers into making purchases. Once you’ve placed an order, you might receive subpar merchandise, or worse, nothing at all. I'm receiving a lot of calls nowadays about scams, and people are asking how they can get a refund and whether there is any chance of getting their money back. Common Types of Scams are Fake Accounts, Phishing Links and ...

Exploiting and Securing GitLab: Lessons from a TryHackMe Lab

Perimeter security isn’t enough—because sometimes the threat is already inside. In this blog post, I’m sharing what I learned from a hands-on TryHackMe lab on GitLab security . It revealed how a simple internal misconfiguration—like open registration or overly permissive repo access—can lead to major data exposure inside an organization. I’ll walk you through the red team perspective on exploiting a misconfigured GitLab instance , and then flip the script to explain how you can secure your own internal build systems . Scenario: Inside the Walls of a Large Organization Think of a large organization—like a bank—with thousands of employees and multiple teams handling development, IT operations, and security. To keep intellectual property (IP) secure, these organizations often host self-managed GitLab instances on their internal network. But here’s where things can go wrong: GitLab is hosted internally Allows anyone on the internal network to register Has some projects...