Cloud Native 15 min read

Master Docker: From Basics to Advanced Container Management

This guide explains Docker’s lightweight, portable architecture, compares it with traditional VMs, outlines key use cases, and provides step‑by‑step commands for starting, stopping, restarting containers, managing images, backing up data, writing Dockerfiles, and orchestrating multi‑container apps with Docker Compose.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Docker: From Basics to Advanced Container Management

Docker core characteristics

Docker containers are lightweight, share the host kernel, start in seconds, and consume far fewer resources than full virtual machines. This results in faster startup (seconds vs. minutes) and higher resource efficiency (3‑5×).

Lightweight : No full OS, rapid start‑up.

Standardised : Dockerfile‑defined images guarantee identical environments across development, testing, and production.

Portable : The same image runs on Linux, Windows, macOS and integrates with Kubernetes, AWS ECS and other cloud platforms.

Scalable : Combined with orchestration tools, containers can be scaled in seconds to handle traffic spikes.

Isolated : Each container has its own process space, network namespace and filesystem, reducing cross‑container interference.

Comparison with traditional virtualization

Startup time : containers – seconds; VMs – minutes.

Resource consumption : containers – low (shared kernel); VMs – high (full OS).

Isolation level : containers – process‑level; VMs – hardware‑level.

Scalability : containers – automatic via orchestration; VMs – manual and complex.

Typical scenarios : containers – micro‑services, DevOps, CI/CD; VMs – multi‑tenant workloads requiring strong security isolation.

Typical Docker use cases

Micro‑service architecture : Split monoliths into independent containers for easier maintenance.

DevOps pipelines : Ensure consistent environments from development to production.

CI/CD integration : Works with Jenkins, GitLab CI, etc., for automated builds and deployments.

Hybrid / multi‑cloud deployments : Move workloads across clouds without modification.

Big data & AI : Run Spark, TensorFlow and other heavyweight frameworks efficiently.

Container lifecycle commands

Start a stopped container

docker start <container_id_or_name>

Create and run a container

docker run -d --name my_container -p 8080:80 -v /host/data:/data nginx

Common options: -d – run in background (detached). --name – assign a custom name. -p – port mapping (e.g., -p 8080:80). -v – mount a volume.

Stop a container (graceful)

docker stop <container_id_or_name>

Force stop a container

docker kill <container_id_or_name>

Restart a container

docker restart <container_id_or_name>

Common Docker CLI commands

docker version

– show client and server version. docker info – display system information. docker ps – list running containers. docker ps -a – list all containers, including stopped ones. docker logs -f <container> – follow real‑time logs. docker logs --tail N <container> – show the last N lines. docker exec -it <container> /bin/bash – open an interactive shell.

Docker image management

docker images

– list local images. docker images -a – include intermediate layers. docker images -q – show only image IDs. docker pull <image[:tag]> – download an image (e.g., docker pull nginx). docker search <term> – search Docker Hub. docker rmi <image> – remove an image; add -f to force. docker commit <container> <new_image> – save a container as a new image.

Data backup and restore

Export a container as an image

docker commit <container_id_or_name> <new_image_name>

Backup a volume

docker run --rm -v /var/lib/mydata:/backup -v $(pwd):/data busybox tar czvf /data/backup.tar.gz -C /backup .

Restore a volume

docker run --rm -v /home/user/backup:/restore -v $(pwd):/data busybox tar xzvf /data/backup.tar.gz -C /restore

Dockerfile basics

A Dockerfile is a plain‑text script that defines how to build a Docker image. Typical directives include:

FROM – base image.

LABEL – optional metadata (maintainer, version, etc.).

ENV – environment variables.

WORKDIR – working directory inside the image.

COPY – copy files from the build context.

RUN – execute commands (e.g., install dependencies).

EXPOSE – document the port the container listens on.

CMD – default command when the container starts.

# Example Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000
CMD ["python","app.py"]

Docker Compose

Docker Compose defines multi‑container applications in a docker-compose.yml file, handling services, networks and volumes with a single command.

Core features

Define multiple services and their dependencies.

One‑click start/stop with docker-compose up -d and docker-compose down.

Isolated environments per service (environment variables, configs).

Automatic network creation and management.

Volume handling for persistent data.

Sample docker-compose.yml

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
    networks:
      - mynetwork
  app:
    build: ./app
    environment:
      - FLASK_ENV=development
    depends_on:
      - db
    networks:
      - mynetwork
  db:
    image: postgres:13
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: mydb
    volumes:
      - db_data:/var/lib/postgresql/data
    networks:
      - mynetwork
networks:
  mynetwork:
volumes:
  db_data:

Basic Compose commands

docker-compose up -d

– start all services in detached mode. docker-compose down – stop and remove containers, networks and default volumes. docker-compose restart – restart services. docker-compose ps – show service status. docker-compose logs – view aggregated logs (add service name to filter). docker-compose build – build images defined by build: sections. docker-compose exec <service> <command> – run a command inside a running service (e.g., docker-compose exec app bash).

Practical recommendations

Use docker start, docker stop and docker restart to control container lifecycles. Leverage docker logs and docker exec for real‑time debugging and interactive access. For backup, combine docker commit (image export) or tar with volume mounts to export and restore data safely. Adopt Docker Compose for multi‑service projects to simplify deployment, scaling and management.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerDockerfileContainer ManagementcontainersDocker-Compose
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.