Preface
Recently, I began learning about Docker containers, and I would like to share some of the experiences I've gained. If there are any mistakes, please feel free to point them out. I've read a lot of articles about Docker online, but most of them were quite vague and hard to understand. So, let me give you a simple analogy.
You’ve spent a lot of time setting up Python, various libraries, databases, and configurations on your computer, and finally, your programming assignment runs successfully. But when you share the code with your classmate, he just can't run it. It’s either the Python version is different, some libraries are missing, or the database paths don't match. Even though it’s the same code, it keeps throwing errors on his computer, and all you can say is, “It works fine on mine.” Later, when the teacher needs to grade your assignment, she has to set up the environment again, and if she misses even one library or has a version mismatch, the program won’t run. The teacher ends up having to try over and over. This is, of course, very frustrating. Suddenly, you have an idea: “What if I could just pack my entire environment—Python, local libraries, databases, configuration files, and even the program itself—into a 'USB drive,' so that anyone could just plug it in and run everything directly, without having to set up the environment again?” This is exactly what Docker does. Docker acts like a virtual USB drive. Others can run everything you wrote just by using this "USB" without needing to rebuild the environment. It packages everything needed to run the program into a "image box," and once others get this box, they can run the same "box" on any computer. The container generated from it will be identical no matter where it's run, eliminating the issue of “it works on my computer, but not on yours.”
1. Docker Containers vs Virtual Machines
Some people might say, “We already have virtual machines. VMware works great. Why not just set up a virtual machine environment and let others use it?”
Before container technology, this was a good solution, but it wasn’t ideal.
To give some context, cloud computing is based on virtual machine technology. Cloud providers purchase a lot of hardware, build data centers, and use virtual machines to split the hardware resources. For example, they can create 100 virtual machines from a single physical server and sell them to many customers, which is what we commonly call VPS. The VPS you buy from cloud providers are actually virtual machines running on their hardware.
Virtual machines work by hardware virtualization, creating completely isolated operating systems. Each virtual machine runs its own OS, and the OS consumes a significant portion of system resources. Different projects require separate virtual machines, which is time-consuming and resource-intensive. In contrast, Docker doesn’t need a full operating system for each container. As long as you are running a Linux-based OS, such as CentOS or Ubuntu, you can directly use containers that others have created. Docker is much more efficient in terms of resource usage, allowing you to run many more containers on the same physical server, significantly improving resource utilization.
2. Installing Docker
Docker supports installation on multiple operating systems, including Linux, Windows, and macOS. Here's how to install it on Ubuntu:
Update the package index:
sudo apt updateInstall necessary packages for HTTPS support:
sudo apt install apt-transport-https ca-certificates curl software-properties-commonAdd Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker-archive-keyring.gpgAdd the Docker repository:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"Update the package index again, and install Docker CE:
sudo apt update sudo apt install docker-ce
After installation, you can check if Docker is installed successfully by running:
docker --version
3. Three Basic Concepts of Docker
Before using Docker, it’s important to understand a few core concepts:
Image: Think of an image as a template. It contains everything you need to run a container, such as the program, runtime environment, and dependencies. An image is like a recipe that tells Docker how to create the container. You can download pre-built images from Docker Hub or create custom images based on existing ones.
Container: A container is a running instance of an image, like a dish that you’ve cooked using a recipe. Each container is independent, with its own file system, network, and process. Even if the containers are created from the same image, each one runs independently and can be managed separately.
Repository: A repository is a place where images are stored. Docker Hub is the most commonly used public repository, where developers can find, download, and share images. Companies can also set up their own private repositories to store and manage custom images for internal use.
4. Conclusion
This is the Docker-related knowledge I’ve learned so far, along with my personal understanding. I hope this helps, and I welcome any feedback or corrections!