Master Docker: From Basics to Building Your Own Container Images
This comprehensive guide explains Docker's purpose, compares it with virtual machines, introduces Linux containers, walks through installation, image and container management, and provides step‑by‑step examples—including a hello‑world demo and a full Dockerfile tutorial for building custom images.
Why Docker?
Docker, released in 2013, has become a pivotal technology for simplifying software deployment by packaging applications with their dependencies into portable containers.
1. The environment‑configuration problem
Developers often struggle with differing OS settings, libraries, and environment variables across machines, leading to the classic "It works on my machine" issue. Recreating the exact environment on a new host can be time‑consuming and error‑prone.
2. Virtual machines
Virtual machines (VMs) run a full guest OS inside a host OS, providing isolation but at the cost of high resource consumption, redundant setup steps, and slow startup.
Heavy memory and disk usage
Extra steps such as user login
Long boot times
3. Linux containers
Linux Containers (LXC) solve many VM drawbacks by isolating processes at the kernel level rather than emulating an entire OS. Containers start quickly, use fewer resources, and have a much smaller footprint.
Fast startup
Low resource usage
Small image size
Containers are lightweight virtual environments that provide many of the benefits of VMs without the overhead.
4. What is Docker?
Docker is a popular implementation of Linux containers that offers a simple interface for building, distributing, and running container images. It packages an application and all its dependencies into a single image that can run consistently on any host.
5. Common Docker use cases
One‑off environments for testing or CI pipelines.
Elastic cloud services that can be started and stopped on demand.
Micro‑service architectures where multiple containers run on a single host.
6. Installing Docker
Docker provides a Community Edition (CE) and an Enterprise Edition (EE). For most developers, CE is sufficient. Installation instructions are available on the official Docker documentation site.
Mac: https://docs.docker.com/docker-for-mac/install/
Windows: https://docs.docker.com/docker-for-windows/install/
Ubuntu: https://docs.docker.com/install/linux/docker-ce/ubuntu/
Debian: https://docs.docker.com/install/linux/docker-ce/debian/
CentOS: https://docs.docker.com/install/linux/docker-ce/centos/
Fedora: https://docs.docker.com/install/linux/docker-ce/fedora/
Other Linux distributions: https://docs.docker.com/install/linux/docker-ce/binaries/
After installation, verify with: $ docker version If you need to run Docker without sudo each time, add your user to the docker group: $ sudo usermod -aG docker $USER Start the Docker daemon if it is not running:
$ sudo service docker start $ sudo systemctl start docker7. Images and containers
An image is a read‑only template that contains an application and its dependencies. Docker creates a container (a writable instance) from an image. Multiple containers can be launched from the same image.
Typical image commands:
# List images
$ docker image ls
# Remove an image
$ docker image rm [imageName]Containers also have lifecycle commands:
# List running containers
$ docker container ls
# List all containers (including stopped)
$ docker container ls --all
# Stop a container
$ docker container kill [containerID]
# Remove a container file
$ docker container rm [containerID]8. Hello‑world example
Pull the official hello‑world image and run it:
$ docker image pull hello-world
$ docker container run hello-worldThe container prints a confirmation message and then exits.
9. Writing a Dockerfile
A Dockerfile defines how to build an image. Example for a Node.js Koa demo:
FROM node:8.4
COPY . /app
WORKDIR /app
RUN npm install --registry=https://registry.npm.taobao.org
EXPOSE 3000
CMD node demos/01.jsExplanation of each line:
FROM – base image (Node 8.4).
COPY – copy source files into the image, respecting .dockerignore.
WORKDIR – set working directory.
RUN – install dependencies during build.
EXPOSE – declare the container’s listening port.
CMD – default command executed when the container starts.
10. Building and running the custom image
Build the image with a tag: $ docker image build -t koa-demo . Run a container from the image, mapping host port 8000 to container port 3000:
$ docker container run -p 8000:3000 -it koa-demo /bin/bashOr let the CMD start the app automatically:
$ docker container run --rm -p 8000:3000 -it koa-demo11. Managing containers
Start a stopped container: $ docker container start [containerID] Stop a container gracefully (SIGTERM) or forcefully (SIGKILL):
$ docker container stop [containerID]
$ docker container kill [containerID]View logs: $ docker container logs [containerID] Execute a command inside a running container:
$ docker container exec -it [containerID] /bin/bashCopy files from a container to the host:
$ docker container cp [containerID]:/path/to/file .12. Publishing an image
Log in to Docker Hub, tag the image with your repository name, and push it:
$ docker login
$ docker image tag koa-demo username/koa-demo:1.0
$ docker image push username/koa-demo:1.0After a successful push, the image appears in your Docker Hub repository and can be pulled by others.
13. Additional useful commands
docker container start– restart an existing stopped container. docker container stop – send SIGTERM (and later SIGKILL) to stop a container. docker container logs – view a container’s stdout/stderr. docker container exec – run a command inside a running container. docker container cp – copy files between host and container.
These commands cover the most common Docker workflows for development, testing, and 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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
