Since its release in 2013, Docker has been attracting a lot of attention and has been used it extensively in the production environment by many companies.
However, many small companies don’t know exactly what Docker is, what problems it solves, and where the benefits lie. This article explains it in detail to help you understand it, along with easy-to-understand examples of how to use it for our daily work.
1. Different environment running your software.
Generally speaking, before we release our code, we need to go through the development phase, the testing phase, and the preview phase before it can finally go online. We must have encountered such a problem: Obviously a program that can run in our development environment, but it does not work in other environments. Or a program that runs normally on the line can’t run after changing the server.
So we must guarantee two things: the setup of the operating system, and the installation or the version of various libraries and components. Only if they are all correct to make sure that software can run as we want. For example, to install a PHP application, the server must have a PHP environment, it must have many dependencies, and it may need to configure environment variables.
The configuration of the environment is so troublesome that a new machine will have to be started all over again, which takes a long time. So can we solve the problem basically, software can be installed with the environment? That is, when you install it, copy the original environment exactly as it was.
2. Virtual machine
Virtual machine is a solution with an environment installed. It can run one operating system on another, such as Windows on macOS. The running software is unaware of this because the virtual machine looks exactly like the real system.
Although the virtual machine can solve environmental problems well, it has the following problems：
- Waste of resources
The virtual machine monopolizes a portion of memory and disk space. When it runs, other programs cannot use these resources. Even if the application running in the virtual machine only takes 10 MB of real memory, the virtual machine still needs several hundred MB of memory to run for itself.
- Unnecessary steps for running a software.
A virtual machine is a complete operating system, and some system-level steps, such as user login, is not necessary, but they can not be skipped.
- Slow start
As long as it takes to boot the operating system, it takes to boot the virtual machine. It may take a few minutes for the application to actually run.
3. Linux container
Because of these shortcomings of virtual machines, Linux has developed another virtualization technology: Linux Containers, or LXC for short. It is based on cgroup and namespace technology.
Instead of emulating a full operating system, the Linux container isolates processes of our software. In other words, there is a protective layer over the normal process. For the process inside the container, the resources it takes are virtual, thereby isolating it from the underlying system.
Because containers are process level, they have many advantages over virtual machines.
- Fast start
The application in the container is directly a process of the real system, not a process within the virtual machine. So, starting the container is equivalent to starting a process on the native, rather than an operating system, which is much faster.
- Less resource usage
Containers use only the resources they need, not those they don’t. Because a virtual machine is a complete operating system, it inevitably takes up all the resources. In addition, resources can be shared by multiple containers, and the virtual machines are exclusive.
- Small size
The container only needs to contain the components used, while the virtual machine is the packaging of the entire operating system, so the container file is much smaller than the virtual machine file.
In short, containers take up much less resources than virtual machines.
4. What is Docker.
Here is the definition from Wikipedia:
Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers.
Docker is a package of Linux container, providing a simple and easy to use container interface. It is the most popular Linux container solution now.
Docker has following advatanges:
- Provide a disposable and standardized environment
This features can be used for testing other people’s software locally, providing a unit test and building environment for continuous integration etc.
- Provide flexible cloud services.
Because Docker containers can be opened and closed on demand quickly, they are perfect for dynamic dilatation and shrinkage.
From a security view, Docker ensures that software running on the container are completely isolated, giving us complete control over traffic and its management. Docker containers cannot view processes running inside other containers.
5. How to install Docker
Docker is an open source commercial product that comes in two versions: community and Enterprise. The Enterprise edition includes a number of premium services that individual developers typically do not use. The following descriptions are for the community edition.
The following examples are all based on Ubuntu, other systems can refer to docker’s official documentation.
Step1: remove old versions.
$ sudo apt-get remove docker docker-engine docker.io containerd runc
Step2: install dependency package
sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-properties-common
Step3: add official public GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Showing OK means that the addition was successful.
Step4: update the system software.
sudo apt-get update
Step5: install docker engine.
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
If everything goes well, use this below command to check the installation result.
$ docker -v
6. Image file
Image is a binary file. Docker packages the software and its dependencies into an Image file which can be thought of as a template for the container. Docker generates an instance of the container according to the image file. Multiple container instances running concurrently can be created from the same image file.
In our daily development, an image file is often generated by inheriting from another image file and adding some personalization. For example, you can build on Ubuntu’s Image and add PHP into it.
# list all image files $ docker image ls # remove image file $ docker image rm [imageName]
7. Example: Hello world.
Now, let’s take a look at Docker through the simplest image file: “Hello World”.
First, run the following command to fetch the image file locally from the repository.
$ docker image pull hello-world
After successful fetching, you can see the image file locally by running command: docker image ls.
Now we can run this image file.
$ docker container run hello-world
The docker container run image command is to create a running container instance from the image file. Besides, if there is no specified image file found locally, it will be automatically fetched from the remote server. Therefore, the previous docker image pull command is not a necessary step.
If everything works well, we can see this:
Hello from Docker! This message shows that your installation appears to be working correctly. .......
After printing this text, Hello World container stops running and the container quit automatically.
Some containers do not quit automatically because they provide a service. For example, running a nginx container or running a MySQL container and so on.
8. Container file
The container instance created by the image file is a file too, called a container file. That is, once the container is generated, there are two files: the image file and the container file. And closing the container does not remove the container file, it just stops running.
# list the running container $ docker container ls # List all containers on this machine, including those that have been terminated $ docker container ls -a
The output of the above command includes the ID of the container which is required in many places. The container file that has been terminated will still occupy hard disk space and can be deleted using the docker container rm container_id command.
After running the above command, and using the docker container ls –all command, you will find that the deleted container file has gone.
9. Other useful command
Besides above mentioned command, Docker has many more useful commands which are common in our daily work.
- docker container start/stop
The previous docker container run command creates a new container, and each time you run it, a new container is created. Run the same command twice, and two identical container files are created. If you want to reuse containers, use the docker container start container_name command, which is used to start the container files that have been created and stopped running.
For a container that is temporarily unnecessary, you can use the docker container stop container_id command to make it stop.
- docker container exec
The docker container exec command is used to enter a running docker container. If the -it parameter is not added when the docker run command runs the container, use this command to enter the container. Once in the container, you can execute commands in the container’s bash.
$ docker container exec -it container_id /bin/bash
- docker container logs
The docker container logs command is used to view the output of the container, the standard output of the Shell inside the container. If the docker run command runs the container without the -it parameter, use this command to view the output.
Thanks for reading. Pls let me know if you want to know more.