Recently, I’ve been asked about Docker containers by a few of my friends. After answering the questions a few times, I found a few common questions that interest people the most. Today I will try to answer to these questions in a user friendly way.
1. What is the difference between virtual machines and Docker containers?
Docker uses Linux container technology (LXC) which lets the applications run inside the host OS. Even though the software inside containers thinks it is a separate machine, in fact they use the same computer and share share the resources. Think of it like a big open space in a warehouse where people from different companies work. They divide the space with curtains, so there at least some “borders”, but still have shared entrance, bathroom, air conditioning, etc. On the other hand, virtual machines emulate the whole system, that does minimal sharing with its host. In our analogy, it is like taking that warehouse office and turning it in a few separate offices. With each office having it’s own entrance, bathroom, air conditioning, etc. This will make them isolated and reduce interference, but each would need their own infrastructure. Similarly, virtual machines will have resource overhead for using their own infrastructure while Docker containers will be far less demanding.
2. When should I need to use Docker
Strength of Docker is in minimizing overhead so it is useful in scenarios when you have multiple apps that consume little resources or need to be isolated. If you use micro-services approach in software, where you divide the system in many independent modules, containerization will be a perfect solution for you. If you have a few small apps, each with a lot of dependencies, it will work for you too. For example, I have an RSS server in docker, SQL database for that server in another container, VPN server in a separate container, etc., all running in a single VPS without conflicts. I could fit a few more containers to that server if the need arises. If any of the apps needs more resources I can easily move its container to another server in a matter of minutes.
Another possible benefit is ensuring your app deployment can be automatically reproduced. At the time of writing, Docker containers don’t persist changes which means you can restart container, delete it, create a new one from the same image and they all should work the same. Each time you deploy a new version of your app, Docker goes through instructions from Dockerfile and creates an immutable image. Which means no ad-hoc changes and fixes of the servers, no interference with the existing app or environment that can cause unpredictable behavior. Of course, you can go against this principles by using commit command if necessary, but it defeats the purpose.
3. I found a docker container on Github but it is just 1 kb file. How do I run it?
You probably found a docker file. That files contains instructions on how to create a docker image. From that image you can create a container. Containers and images are different things. Like with film cameras when you develop the film and print photos based on it, where Docker image is a film frame and Docker container is photo of that frame. You can print multiple photos of one film frame, similarly to creating multiple containers from one Docker image.
To create an image:
- put a Dockerfile to a folder
- execute in that folder:
docker build -t username/imagename:version
docker run username/imagename:version
As you know
docker run creates a new container and starts it. Later you can start the created container with
4. I stopped container, run it next day and all the data was gone. What is the problem?
Regular containers do not persist data by design. Instead, you can use Data Container or Data Volume. See the next question.
5. How do I use persistent data in Docker?
Docker has a special kind of containers called Data Container. Other containers can store data there. Alternatively, you can mount a directory from the host OS as a Data Volume. Here is an example, where you mount data_volume folder from ~/dev as /data for the container:
PS. The answers given here are based on the official Docker documentation. Please consider using the official documentation if you faced other issues or have further questions.