DEV Community

Omkar Sharma
Omkar Sharma

Posted on

Docker Basics : Problem Statement, Concepts & Commands

Problem Statement - What Problem Does Docker Solve?

“Same code works on my machine but not on yours.”

Why does this happen?

Environment Replication Issues

Example Scenario

  • You worked on a project for the past 3 months.
  • During development, you installed multiple dependencies, libraries, and packages.
  • Now you want to onboard another developer.
  • You shared the source code and asked them to install dependencies with exact versions.

Challenges

  • Hard to remember specific versions over time.
  • New developers may install latest versions instead of required ones.
  • Latest versions may introduce breaking changes.
  • Suppose you put extra effort to remember the version and dependencies but what if they are using diff OS in that case if Windows CLI you are using will not work in MAC.
  • Example:
    • Your project uses Node.js 14.x
    • Another developer installs Node.js 18.x
    • Result: Application may break or behave differently.

Omkar Sharma

Docker solves this by standardizing environments so applications run the same everywhere.

Installation of Docker CLI & Docker Desktop

Download Docker Desktop from:
https://www.docker.com/products/docker-desktop/

Docker Desktop includes:

  • Docker CLI
  • Docker Engine
  • Docker Compose
  • GUI Dashboard

Understanding Images vs Containers

Docker Images

  • Preconfigured read-only templates.
  • Include:
    • Base OS (optional)
    • Runtime (Node, Python, Java, etc.)
    • Dependencies
    • Application setup
  • Used to create containers.
  • Images act as blueprints.

Docker Containers

  • Running instances of Docker Images.
  • Package application and dependencies together.
  • Lightweight compared to VMs.
  • Share host OS kernel (not a full OS like VMs).
  • Containers are running applications created from images.

Running Ubuntu Image in a Container

docker run -it <image_name>
Enter fullscreen mode Exit fullscreen mode

Example:

docker run -it ubuntu
Enter fullscreen mode Exit fullscreen mode

If the image is not available locally, Docker pulls it from Docker Hub automatically.


Useful Docker Commands

List Containers

docker container ls -a
Enter fullscreen mode Exit fullscreen mode

List Images

docker image ls
Enter fullscreen mode Exit fullscreen mode

Enter an Existing Running Container

docker exec -it <container_name> bash
Enter fullscreen mode Exit fullscreen mode

Multiple Containers

Start a Container:

docker start <container_name>
Enter fullscreen mode Exit fullscreen mode

Stop a Container:

docker stop <container_name>
Enter fullscreen mode Exit fullscreen mode

Key Concept

  • Multiple containers can run from the same image.
  • Each container has an isolated environment.
  • Example:

    • Create directory in Container 1
    • Create directory in Container 2
    • Both remain isolated from each other.

Omkar Sharma

Port Mapping

docker run -it -p <local_machine_port>:<container_port> <image_name>
Enter fullscreen mode Exit fullscreen mode

Example:

docker run -it -p 3000:3000 omkarsharma2821/omkar-node-app
Enter fullscreen mode Exit fullscreen mode

Omkar Sharma

Omkar Sharma

Allows access to container services from local machine.

Environment Variables

docker run -it -e PORT=4000 -p 4000:4000 <image_name>
Enter fullscreen mode Exit fullscreen mode

Used to configure applications dynamically inside containers.

Dockerization of a Node.js Application

Initialize Node Project:

npm init
Enter fullscreen mode Exit fullscreen mode

Install Express:

npm install express
Enter fullscreen mode Exit fullscreen mode

Build Docker Image:

docker build -t omkar-node-app .
Enter fullscreen mode Exit fullscreen mode

As if now the img is avaialbel locally to push to hub.docker.com you need to run the following coommands

Login first

docker login

Enter fullscreen mode Exit fullscreen mode

Tag your image with latest

docker tag <local_image_name>:latest <dockerhub_username>/<repo_name>:latest
Enter fullscreen mode Exit fullscreen mode

Example

docker tag omkar-nodejs:latest omkarsharma2821/omkar-node-app:latest

Enter fullscreen mode Exit fullscreen mode

Push to docker hub

docker push <dockerhub_username>/<repo_name>:latest

Enter fullscreen mode Exit fullscreen mode

Example

docker push omkarsharma2821/omkar-node-app:latest

Enter fullscreen mode Exit fullscreen mode

Summary

  • Docker ensures consistent environments across machines.
  • Images are templates.
  • Containers are running instances.
  • Supports isolated environments, easy onboarding, and scalable deployments.
  • Essential for modern development workflows and microservices architecture.

✍️ Author: Omkar Sharma

📬 Feel free to connect on LinkedIn or explore more on GitHub

Top comments (0)