Your First Step to use Docker on a Non-Linux OS | by Kefei Mo | Towards Data Science

When you read this article, you probably already heard of Docker and decided to give it a try. You probably also have gone to the docker website and ready to choose a Docker engine for your development machine.
If you are a Linux user, congratulations, you can click “Docker for Linux” to install Docker on your Linux machine.
If you are using non-Linux OS just like me, you are probably thinking “Let’s try the Docker Desktop. Why not?”
Wait, don’t click on the Docker Desktop button yet. Docker Desktop might not be the best option for you. And I will tell you why.
In this article, I will tell you different ways to use Docker on a non-Linux operation system. I will try to explain how Docker works. More importantly, I will show you how to set up an environment, so that you don’t have to deal with compatibility and configuration issues in your soon-to-come Docker development journey.
This article is for you if you are new to Docker (or containerization, virtualization) or development on Linux.
It is all about newbie-friendly explanations and hands-on tutorials.
Here is the outline of this article:
How does a container work?
(Docker on Windows/Linux vs Docker for Windows/Linux)
Docker engine, Docker machine, Docker-cli, etc.: all those fuzzy words
Three options for Docker on Non-Linux hosts
Hands-on set Docker environment on Win10 using Virtualbox
P.S.: If you want to jump right in to use Docker, you can skip to the tutorial at the end of this blog. I provided a step-by-step, future proof solution.
A container is a virtual runtime environment that runs on top of a single operating system (OS) kernel and emulates an operating system rather than the underlying hardware.
Let’s consider the following diagrams:
On the left is the native OS without containers, where all the apps share the same dependencies (bins and libs). On the right, the apps are isolated by containers and within each container, the app has its own dependencies.
The introduction of such isolation can bring many benefits regarding security and DevOps. For example, the crash of one container would not cascade to other contains. Isolation also solves dependency conflict issues (i.e., dependency required by app1 conflicts with the dependency of app2) since apps will run inside their own containers with their own dependencies.
A container engine like Docker Engine works by utilizing the resource of the kernel. The thing is, different OSes like Windows and Linux have different kernels. As a result, a container is likely to require different dependencies to work in a Windows OS or a Linux OS.
Since we want to keep a container light-weighted, instead of making individual container works for both Linux and non-Linux system, it is wiser to have different versions of containers that work for either Win OS or Linux OS respectively. Colloquially, we call a container that works on Windows OS a “Windows Container”, which utilizes Win OS kernel. And its counterpart is a Linux container.
So why this has anything to do with today’s topic? Well, at the end of the day, you need to create containers (to be more specific, building images to run containers) that deploy on either a Windows or a Linux host for production use. A containerization technology like Docker can best ship your container from an environment with a Linux kernel to another Linux OS, since all Linux distros share the same kernel. But containerized applications using Windows kernel (i.e., native Windows container) cannot be shipped to a Linux host.
Docker is created to develop and manage Linux Containers, so you don’t have any problem using Docker on your development machine if you are Linux User.
But what about non-Linux users, like you and me? And that’s how hypervisor comes into the picture.
A hypervisor (or virtual machine monitor, VMM, virtualizer) is computer software, firmware, or hardware that creates and runs virtual machines. A virtual machine is the virtualization/emulation of a computer system. For example, if you are using Win10 on your PC, a hypervisor can create a Linux virtual machine that makes you feel like using a Linux system.
Note there are two types of hypervisors:
Type1 (Hyper-V): hypervisor sits on top of the hardware.
Type2 (Oracle VirtualBox): hypervisor sits on top of the Host OS.
I am assuming you want to create an environment for Linux container development on your Windows PC. There are three different solutions to make this work:
Solution#1: Docker Desktop for Windows
Solution#2: Docker on Linux VM
Solution#3: Docker ToolBox (deprecated)
Since solution#3 is deprecated, the core concept of solution#3 is the same as solution#2, let’s focus on the first two solutions. Here is the comparison of the solution architectures:
Let’s take a look at the pros and cons of the two solutions.
Docker for Mac / Docker for Windows (Docker Desktop)
Pros
Out of the box, you are ready to go after install Docker Desktop and follow the configuration instrction
Consider as the default solution, Docker is heavily developing and polishing this solution.
Cons
On certain macOS hardware combos, the volume performance can be a little slow.
You cannot run persistent background processes with WSL2.
It doesn’t feel more “native” if you are not familiar with Windows or MAC command-line tools. Actually, it feels off because you are basically writing Linux-like Docker command on non-Linux terminals.
There are not many tutorials on how to use Docker Desktop or configure Hyper-V
Docker for Mac / Docker for Windows (Docker Desktop)
Pros
It provides a more native environment than a Docker desktop. Plus, there are more Docker tutorials created on a native Linux environment than for Docker Desktop, which is a big attraction for new learns.
Even though Docker is currently working closely with Microsoft, the WSL (Windows Subsystem for Linux) still needs some time to mature. On the other hand, VirtualBox is a much more mature solution. That means fewer compatibility issues.
It is future-proofed since the open-source software is the basis for Docker’s container runtime environment, RunC, and the ContainerD daemon, which is based on the Open Container Initiative (OCI) industry standards and will be further developed as open-source providers under an open-source license.
Cons
There are more overheads to manage the VM.
The VM applications might cause the computer to quickly bog down like molasses.
As you can see, I am on the side of installing Docker your own VM. About the cons of this solution:
Since it is a trade-off between controllability and overhead, and for a development environment controllability is well paid-off, so this is not really a big disadvantage.
It is true VirtualBox can slow down your PC. However, you are not likely to deal with the complexity level in a real productive server when you start learning Docker. When you think you are ready, you can easily deploy the application on a high-performance server or in the cloud.
Finally, we come to the nitty-gritty part of this article. Let’s set up the Docker development environment.
To install Docker on Win10, you need to download the VirtualBox and a Linux OS Image (recommend Ubuntu). In this tutorial, I am using VirtualBox 6.1.18, Ubuntu 20.10 Groovy Gorilla. You can use other up to date version as your choice.
Install VirtualBox on Win10 is pretty straightforward. The following step will start with creating a Linux VM from an image.
Select “Machine”, then click on “New”
Select “Machine Folder” path and “Name” (the VM will locate in the $<Machine Folder>\$<Name> directory
Select “Type” as Linux. Select “Version” as “Ubuntu(64-bit)”. Note: If you name your folder as Ubuntu or something similar, VirtualBox will be smart enough to select “Type” as “Linux” and “Ubuntu(64-bit)” for you.
Click on “Next”
Select memory size for the VM. (Since you can adjust the memory size to the VM later, you can choose the default setting here)
In the “Create Virtual Machine”
Select “Use an existing virtual hard disk file”
If the image (.vdi file) is not listed in the drop-down list, click on the folder navigator button
Choose the image then click “Create”
Now you shall see the VM you just created. Double click to start the VM.
(If you are using the image created by osboxes.org. To login the first time using password “osboxes.org”. )
Trouble shootings: At this moment (early 2021) when I am writing this article, there is a conflict between Window Hyper-V and VirtualBox hypervisor. This conflict will cause “Guru Meditation Critical Error” with something like the following: (
(Watch out for the green turtle. If it appears, something is wrong)
To resolve this, go to “Turn Windows Feature On and Off” and make sure the following are un-selected
Hyper-V
Virtual Machine Platform
Windows Subsystem for Linux (WSL)
One more step before we can actually install Docker Engine on the VM — configure the VM networks, so that Docker Engine can pull and push images from the public registry. It also enables a VM to run containerized services (e.g. a web service) that can be access non-locally.
(When the VM is “Powered Off”), right-click on the VM instance in interest, then click “Settings..”
In the pop-up window, select “Network”
In the “Attached to:” drop-down menu, select “Bridged Adapter”. (You can find more information about VirtualBox adapters in this link.)
Now if you start the VM again, open a terminal (open terminal shortcut Ctrl+Alt+T) you can query IP information by using “ifconfig” (or “ip addr”)
Here, the IP of my VM is 192.168.1.19. If everything works, you shall be able to ping this address from your VM host (i.e., your Win10).
Tips: share clipboard between your VM and the host OS and make it easy to copy-paste commands
Under the “Devices” menu → “Shared Clipboard”, select “Bidirectional”.
In terminal copy/paste is Ctrl+Shift+C/V
Here are the main steps to install Docker Engine on your Ubuntu VM. (Please refer to Docker documentation if there is any update on installing Docker Engine in Ubuntu.)
Uninstall old versions
$ sudo apt-get remove docker docker-engine docker.io containerd runc
Set up the repository
$ sudo apt-get update$ sudo apt-get install \\    apt-transport-https \\    ca-certificates \\    curl \\    gnupg-agent \\    software-properties-common
Add Docker’s official GPG key
Then verify you have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
$ curl -fsSL <https://download.docker.com/linux/ubuntu/gpg> | sudo apt-key add -$ sudo apt-key fingerprint 0EBFCD88pub   rsa4096 2017-02-22 [SCEA]      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88uid           [ unknown] Docker Release (CE deb) <docker@docker.com>sub   rsa4096 2017-02-22 [S]
Use the following command to set up the stable repository.
$ sudo add-apt-repository \\   "deb [arch=amd64] <https://download.docker.com/linux/ubuntu> \\   $(lsb_release -cs) \\   stable"
Install Docker Engine (the package is called “docker-ce”, stands for docker community edition)
$ sudo apt-get update $ sudo apt-get install docker-ce docker-ce-cli containerd.io
Verify that Docker Engine is installed correctly by running the hello-world image.
$ sudo docker run hello-world
Now you shall see something as follows.
Troubleshooting: A typical Network configuration issue can cause an error shown below. Check your VM network configuration and make sure the adapter set to “Bridged Adapter”.
Ad hoc settings
Right now, it will throw an error if you directly run Docker command. (Docker: Got permission denied while trying to ....)
If you would like to use Docker as a non-root user, you should now consider adding your user to the “docker” group. So that you don’t have to use sudo docker every timer. Here is what you can do. (Remember to log out and back in for this to take effect!)
$ sudo usermod -aG docker <your-user>$ sudo -s[sudo] password for ubuntu: root@osboxes:/home/ubuntu# exitexit
This sub-tutorial will mimic the process of provisioning and administering an enterprise filesharing site called NextCloud.
Nextcloud is an open-source software suite that can use storage capacity to save, edit, and consume a wide range of document types, including services like audio/video call hosting.
I choose NextCloud as an example to show you the easiness of deploying applications (in this case, a web service) in a testing/production environment. So, let’s do it.
First, pull the NextCloud image from the Docker Hub registry (it might take a while)
$ docker pull nextcloud
Using the apache image, which contains a web server and exposes port 80.
$ docker run -d -p 8080:80 nextcloud
Now, if you type: “localhost:8080” on your VM’s web server (e.g., Firefox) or even better, try to access NextCloud in your host PC using “IpAddress:8080” (in my case, 192.168.1.19:8080).
Wa-lah, you have a professional file share system. You can create an account and play a little bit with it to see what NextCloud offers you.
Note: the NextCloud demonstration is meant to give you a taste of Docker, but not for production. Here is a blog gives you detailed guidance how to deploy production level NextCloud service using Docker.
This is just one example of creating services using Docker containers. Expand this idea; you can create several VMs, deploy various applications on each VMs and have them cooperate with each other — just like a micro-services architecture.
I hope now you are confident enough and excited to start playing with Docker. To recap, let’s look at some takeaways from this article. In this post:
you appreciate the difference between a Windows Container and a Linux Container
you understand different solutions to develop Linux Contains in a native Windows OS environment using hypervisor as an interface
you learn how to create a Linux VM as an environment for Linux Container development using VirtualBox. Also, you learn to install Docker Engine on the Linux VM and run containers in the VM.
I hope you enjoy this blog. See you next time. ;)