Master Docker from Scratch: Build, Deploy, and Run Your First Containerized App
This comprehensive guide walks you through Docker fundamentals, compares virtual machines to containers, explains core concepts, shows step‑by‑step installation, image building, container execution, and best practices, enabling you to containerize a Vue project and deploy it with Nginx effortlessly.
Why Containers Matter
In the era of rich web applications, deployment complexity requires clustering, isolation, gray releases, and dynamic scaling; containerization bridges these needs.
A Story to Illustrate
Imagine building a house by moving stones, cutting wood, drawing plans, and constructing it. When you want to move to the sea, you would have to repeat the whole process. A wizard teaches you a magic that creates a "image" of the house, which you can pack in a backpack and redeploy anywhere instantly. In software terms, the house is the project, the image is a copy of the project, and the backpack is an image repository. This enables rapid scaling: pull the image from the repository and run it anywhere – build once, run anywhere.
Virtual Machines vs. Containers
Virtual Machine (VM) : Software emulates a full hardware system, providing complete isolation with its own kernel, disk, and OS. VMs are heavyweight, take gigabytes of space, and have slower startup times.
Container : Virtualizes only the OS layer, packaging code, dependencies, and configuration into a lightweight, sandboxed unit. Containers run with high resource utilization, fast startup, and can be replicated quickly.
Key differences: VMs isolate entire operating systems and consume more resources; containers share the host kernel, use megabytes of space, and start in seconds.
Understanding Docker
Docker is an open‑source application container engine that packages an app and its dependencies into a portable container, which can run on any Linux machine. Docker uses Linux kernel features such as namespaces and cgroups to isolate processes.
Core concepts:
Image : A read‑only template containing the code, runtime, libraries, and configuration.
Container : A runnable instance of an image.
Repository : A storage location for images.
Docker’s workflow follows "Build, Ship, Run" – build an image once and run it anywhere.
Installation
Command‑line installation (Mac) : brew cask install docker Check version: docker -v Configure registry mirrors (example JSON, stripped of attributes):
{
"registry-mirrors": [
"http://hub-mirror.c.163.com/",
"https://registry.docker-cn.com"
],
"insecure-registries": [],
"experimental": false,
"debug": true
}Install Docker Desktop from the official website and use its GUI for cloning, building, running, and sharing images.
Quick Start: Containerizing a Vue Project
1. Create a Vue project: vue create docker-demo 2. Build the project: yarn build The compiled static files are placed in the dist directory.
3. Create a Dockerfile:
FROM nginx
COPY dist/ /usr/share/nginx/html/
COPY default.conf /etc/nginx/conf.d/default.conf4. Create an Nginx configuration file default.conf (simplified):
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}5. Build the Docker image: docker build -t jartto-docker-demo . 6. Run the container, mapping host port 3000 to container port 80:
docker run -d -p 3000:80 --name docker-vue jartto-docker-demo7. Verify the container is running: docker ps -a 8. Access the application at http://localhost:3000 (or via curl -v -i localhost:3000).
9. Publish the image to Docker Hub:
Log in with docker login.
Tag the image:
docker tag jartto-docker-demo username/jartto-docker-demo:latest.
Push: docker push username/jartto-docker-demo:latest.
Common Dockerfile Instructions
FROM : Base image.
MAINTAINER : Image author.
RUN : Execute commands during build.
ADD : Copy files and optionally extract archives.
COPY : Copy files without extraction.
CMD : Default command when container starts.
ENTRYPOINT : Command that cannot be overridden.
LABEL : Metadata.
ENV : Environment variables.
EXPOSE : Declare listening ports.
VOLUME : Define mount points.
WORKDIR : Set working directory.
USER : Run as specific user.
ARG : Build‑time variables.
Best Practices
Clearly define required base images.
Keep Dockerfile steps minimal and stable.
Specify exact image versions.
Provide reproducible documentation for the build process.
Conclusion
Containerization is an essential skill in the cloud era; Docker is just the beginning. Mastering Docker paves the way to advanced orchestration tools like Kubernetes, Service Mesh, and Istio.
Relevant links:
https://www.docker.com/products/docker-desktop
https://hub.docker.com/
https://docs.docker.com/
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.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
