The Compiler Journal
Visit My Site

Docker for Absolute Beginners: Daily Commands Explained!

πŸš€ Whether you’re developing applications, deploying services, or just getting started with containers β€” this guide gives you a complete, beginner-friendly reference for Docker. With real-world examples and all the daily-used commands explained clearly, you’re about to Docker like a pro πŸš€


🧠 What is Docker?

Docker is a tool that lets you package applications with their environment (libraries, OS, dependencies) into portable containers. It helps you:

  • Avoid “It works on my machine” problems
  • Standardize environments across dev/staging/prod
  • Easily scale and ship apps

πŸ› οΈ Basic Commands

🧱 Image Commands

List available images:

docker images

Shows all images (base OS, app, etc.) stored locally on your machine.

Pull an image from Docker Hub:

docker pull nginx

Downloads the nginx image from Docker Hub if not already present.

Build an image from a Dockerfile:

docker build -t myapp:latest .

Builds an image in the current directory using Dockerfile, and tags it as myapp:latest.

Remove an image:

docker rmi myapp

Deletes the specified image from your local system.


πŸ“¦ Container Commands

Run a container:

docker run -d --name mynginx -p 8080:80 nginx

Runs the nginx container in detached mode (-d), names it mynginx, and maps port 8080 (host) to 80 (container).

List running containers:

docker ps

Lists all currently running containers.

List all containers (including stopped):

docker ps -a

Lists all containers regardless of their status.

Stop a running container:

docker stop mynginx

Gracefully stops the specified container.

Start a stopped container:

docker start mynginx

Starts a container that was previously stopped.

Restart a container:

docker restart mynginx

Stops and then restarts the container.

Remove a container:

docker rm mynginx

Deletes a stopped container permanently.

Execute a command inside a running container:

docker exec -it mynginx bash

Runs bash inside the container interactively (-it), allowing you to explore the container like a mini VM.


πŸ” Inspecting

View logs from a container:

docker logs mynginx

Displays standard output (like console.log or print) from the container.

Inspect container details:

docker inspect mynginx

Gives JSON with low-level details like IP address, mounts, environment variables, etc.


πŸ“‚ Volumes (Persistent Data)

Create a volume:

docker volume create mydata

Creates a named volume that can be shared between containers.

List volumes:

docker volume ls

Shows all volumes on your Docker host.

Mount a volume in a container:

docker run -v mydata:/data alpine

Mounts the mydata volume at /data inside the alpine container.


🌐 Networks

List Docker networks:

docker network ls

Shows all available networks (bridge, host, none, custom).

Create a new network:

docker network create mynetwork

Creates a custom bridge network named mynetwork.

Connect a container to a network:

docker network connect mynetwork mycontainer

Attaches a running container to the specified network.


🐝 Docker Compose

docker-compose.yml example:

version: '3.8'
services:
  web:
    image: nginx
    ports:
      - "8080:80"

Defines a service named web using nginx image, and maps port 8080 to 80.

Start services in background:

docker-compose up -d

Starts all services defined in docker-compose.yml in detached mode.

Stop services:

docker-compose down

Stops and removes all services, networks, and volumes created by docker-compose up.

Rebuild services:

docker-compose up --build

Rebuilds the images before starting the services.


🐳 Docker Swarm (for Production Clustering)

Initialize Swarm:

docker swarm init

Starts Docker Swarm mode and makes the current node the manager.

Join Swarm from another machine:

docker swarm join --token <TOKEN> <MANAGER-IP>:2377

Allows another node to join the Swarm cluster as a worker or manager.

List all nodes in Swarm:

docker node ls

Only from a manager β€” shows all machines in the Swarm and their roles.

Deploy a stack using Docker Compose:

docker stack deploy -c docker-compose.yml mystack

Deploys services defined in docker-compose.yml as a stack called mystack.

List deployed stacks:

docker stack ls

Displays all active stacks in the Swarm.

List services in a stack:

docker stack services mystack

Shows all services that belong to the stack mystack.

Remove a stack:

docker stack rm mystack

Deletes the stack and all its services.

πŸ“Œ Always deploy stacks from the Swarm manager node.


πŸ” Docker Secrets (Store Sensitive Info)

Create secret from a file:

echo "my_db_password" > db_pass.txt
docker secret create db_password db_pass.txt

Creates a secret named db_password using the contents of db_pass.txt.

Create secret using stdin:

echo "my_secret" | docker secret create my_secret -

Creates a secret from piped input, avoiding file creation.

List all secrets:

docker secret ls

Shows all defined secrets in the Swarm.

Use secrets in docker-compose.yml:

services:
  app:
    image: myapp
    secrets:
      - db_password

secrets:
  db_password:
    external: true

Mounts the secret inside the container at /run/secrets/db_password.


🧹 Clean Up Unused Resources

Remove stopped containers:

docker container prune

Remove dangling images (untagged):

docker image prune

Remove unused volumes:

docker volume prune

Remove everything unused (⚠️ dangerous):

docker system prune -a

πŸ‘¨β€πŸ”§ Dockerfile Quick Reference

A sample Dockerfile for Node.js:

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Build the image:

docker build -t my-node-app .

Run the container:

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

πŸ§ͺ Debugging Tips

Access container’s shell:

docker exec -it <container_id> /bin/sh

Check container environment variables:

docker exec <container_id> env

Monitor real-time resource usage:

docker stats

πŸ“š Summary

βœ… Learn daily-used Docker commands
βœ… Build, run, debug, and deploy containers
βœ… Use volumes, networks, secrets, and stacks
βœ… Practice cleanups and write Dockerfiles


🧠 Further Reading:

Happy Dockering! πŸ‹βœ¨