Master Docker from Zero: Build, Ship, and Run Your First Container
This guide walks you through Docker fundamentals, comparing virtual machines and containers, explains core Docker concepts, shows how to install Docker, create a Dockerfile for a Vue app, build and run the image with Nginx, and shares best‑practice tips for publishing and optimizing containers.
1. A Story to Illustrate Docker
Imagine building a house, then needing to move it to the sea; a magician gives you a "image" that you can copy and carry in a backpack—just like Docker images that let you replicate applications instantly.
2. Virtual Machines vs. Containers
Virtual Machine : emulates full hardware, each VM includes its own kernel, consumes gigabytes of resources, and starts slowly.
Container : virtualizes the OS layer, shares the host kernel, starts quickly, uses only megabytes of space, and provides high resource utilization.
Containers are lighter and faster because they run on the Linux kernel directly, while VMs rely on a heavyweight hypervisor.
3. Understanding Docker
Docker is an open‑source container engine that packages an application and its dependencies into a portable image. Its three core concepts are Image, Container, and Repository.
4. Core Concepts
Build, Ship and Run Build once, Run anywhereDocker is a tool, not a container itself.
The three Docker concepts: Image, Container, Repository.
Docker relies on Linux namespaces and cgroups; on macOS/Windows it runs a Linux VM underneath.
An image is an executable package; a container is the runtime instance of that image.
5. Installing Docker
On macOS you can install via Homebrew Cask: brew cask install docker Check the version: docker -v Configure a registry mirror by writing a JSON file to the Docker daemon configuration:
{
"registry-mirrors": [
"http://hub-mirror.c.163.com/",
"https://registry.docker-cn.com"
],
"insecure-registries": [],
"experimental": false,
"debug": true
}6. Quick Start with a Vue Project
Create a Vue project and build it:
vue create docker-demo
yarn serve
yarn buildThe compiled static files are in the dist directory.
7. Writing the Dockerfile
FROM nginx
COPY dist/ /usr/share/nginx/html/
COPY default.conf /etc/nginx/conf.d/default.confExplanation: FROM nginx uses the official Nginx base image. COPY dist/ /usr/share/nginx/html/ copies the built static files into the Nginx document root. COPY default.conf /etc/nginx/conf.d/default.conf replaces the default Nginx configuration with your own.
8. Building and Running the Image
Build the image: docker build -t jartto-docker-demo . Run a container mapping host port 3000 to container port 80:
docker run -d -p 3000:80 --name docker-vue jartto-docker-demoVerify the container is running with docker ps -a and access the app at http://localhost:3000.
9. Publishing the Image
Log in to Docker Hub, tag the image, and push it:
docker login
docker tag jartto-docker-demo <username>/jartto-docker-demo:latest
docker push <username>/jartto-docker-demo:latest10. Best Practices
Clearly define required images.
Keep Dockerfile steps minimal and ordered.
Specify exact image versions.
Document the build process for reproducibility.
Refer to Docker’s official best‑practice guides for deeper optimization.
11. Summary
Containerization is essential in the cloud era; Docker is the entry point, leading to advanced orchestration tools like Kubernetes, Service Mesh, and Istio. Mastering Docker opens the door to powerful, portable, and efficient application deployment.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
