Master Docker: From Basics to Building Your Own Images
This comprehensive guide introduces Docker’s origins, core concepts such as images, containers, and registries, explains installation on major platforms, demonstrates essential commands, shows how to run and manage containers, and walks through creating, tagging, pushing, and importing custom images using Dockerfile.
Quick Start
Docker began as an internal side project at dotCloud.
It is written in Go.
The goal is a lightweight OS‑level virtualization solution.
Docker builds on Linux container technologies such as LXC.
Containers start in seconds, far faster than traditional VMs.
High resource efficiency allows thousands of containers on a single host.
The following images compare Docker with traditional virtualization: containers share the host OS kernel, while VMs virtualize the hardware layer.
Containers consume minimal extra system resources, delivering high performance with low overhead. Running ten applications requires ten VMs in a traditional setup, but only ten isolated containers with Docker.
Main advantages:
Faster delivery and deployment – containers are the smallest unit.
More efficient kernel‑level virtualization.
Easier migration and scaling.
Simpler management.
Installation
The official website provides installation guides for Mac, Linux, and Windows. Follow the official documentation.
Kitematic (Visual Docker Container Management on Mac & Windows) offers a graphical interface for beginners.
Daemon
When running the Docker daemon you can change the binding interface with the -H flag, e.g.: sudo /usr/bin/docker -d -H tcp://0.0.0.0:2375 To avoid typing the long command each time, set the environment variable:
export DOCKER_HOST="tcp://0.0.0.0:2375"Web UI
Common web management tools include Shipyard and Portainer.
Basic Concepts
Image
A read‑only template used to create containers.
Based on a Union file‑system layered structure.
Can be built, updated, or pulled from public repositories.
Container
A running instance created from an image, with a writable top layer.
Can be started, stopped, and deleted; each container is isolated.
Essentially a lightweight Linux environment with its own process, user, and network spaces.
Registry
A centralized storage for images, public or private.
The largest public registry is Docker Hub.
Users push their own images to a registry and pull them on other machines.
Conceptually similar to Git repositories.
Docker follows a client/server architecture; the client sends requests to the Docker daemon.
Typical Use Cases
Standardize, optimize, and accelerate local development and build pipelines.
Ensure consistent runtime results across environments.
Create isolated environments for testing.
Isolation Features
File‑system isolation – each container has its own root FS.
Process isolation – containers run in separate process spaces.
Network isolation – separate virtual interfaces and IP addresses.
Resource isolation via cgroups – CPU, memory, etc., are allocated per container.
Common Commands
Check Docker status: sudo docker info List running containers: docker ps (add -a for all, -l for the latest)
Simple Example
Run an Ubuntu container and print a message:
docker run ubuntu:14.04 /bin/echo 'Hello wdx!'Start an interactive Bash session: docker run -t -i ubuntu:14.04 /bin/bash Options: -t allocates a pseudo‑TTY. -i keeps STDIN open.
Inside the container you can run ps or top to see only the processes you started.
Image Management
Pull an image: docker pull ubuntu:12.04 Pull from a specific registry: docker pull dl.dockerpool.com:5000/ubuntu:12.04 List local images:
docker imagesRunning an Image
Search Docker Hub for the whalesay image and run it:
docker run docker/whalesay cowsay boo
Building Your Own Image
Create a directory and enter it: mkdir wdxtub; cd wdxtub Create a Dockerfile (e.g., with gedit Dockerfile).
Start from a base image: FROM docker/whalesay:latest Add commands, e.g., RUN apt-get -y update && apt-get install -y fortunes.
Specify the default command: CMD /usr/games/fortune -a | cowsay.
Build the image: sudo docker build -t wdx-whale . Verify with docker images.
Run the custom image:
docker run wdx-whalePushing to Docker Hub
Create a repository (e.g., wdxtub/demo) on Docker Hub, tag the image, log in, and push:
docker tag 26ac9649d7da wdxtub/wdx-whale:latest docker login --username=wdxtub [email protected] docker push wdxtub/wdx-whaleExporting and Importing Images
Export an image to a tar file: docker save -o wdx-local-whale.tar wdxtub/wdx-whale Import it later:
docker load --input wdx-local-whale.tarRunning Containers in Background
Use -d to run a container detached:
docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"View logs with docker logs <container_id>. The container’s lifetime depends on the command executed, not on the -d flag.
Stopping and Restarting
Stop a container: docker stop <container_id> Restart a container:
docker restart <container_id>Import/Export and Deletion
Export a container’s filesystem: docker export <container_id> Import a snapshot as an image: cat ubuntu.tar | sudo docker import - test/ubuntu:v1.0 Delete containers (add -f to force removal of running containers):
docker rm $(docker ps -a -q)Repositories vs. Registries
A repository stores images; a registry is the server that hosts one or more repositories. For example, in dl.dockerpool.com/ubuntu, dl.dockerpool.com is the registry and ubuntu is the repository.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
