Docker the big beast in microservice and distributed system world. So in my current going internship, I was asked to deploy my Kafka Ruby consumer to staging in a docker container environment.
Seems easy to me according to what I have heard about Docker. But the experience I had is totally different.
- The clean and clear knowledge of how Docker works on Mac.
Why you need this? you asked because you are developing on Mac and the production instance is Ubuntu. Ya both are UNIX based operating system but there is a difference in between them in terms of how Docker run on them.
- And if you don't want to do the research by yourself by digging in the official docs of Docker for both environments, then this my sir/madam is the thing you are looking for. And also it will help a lot in debugging because you know what's going on in there.
If you are new to docker then let's get the floor clean first. There is a thing called container-based solutions. What this approach does is making a bundle of your app's code, its dependencies, environment and sometimes executable. And call that bundle as a container. Now everything the app needs to run properly is there in that bundle or container. From now onwards we will call it a container.
So now what we need is an operating system kernel to run this application on hardware. Did you get the hit? These are not virtual environment machines over your host machine. No virtual machine, do remember this.
Now if you go on the official websites you will see words like scaling, automatic deployment, better architecture, and all sort of DevOps problem jargons.
Docker does that but those things are right now out of the scope of this blog.
Linux OS (Ubuntu)
I think docker was actually build to work in a Linux environment. That's why it works great on Ubuntu. How you asked?
Docker container running on Ubuntu is just like a usual process running for Kernel.
- If we expose the port from the container with
-poption we can access it on the host.
docker container run -p <host port>:<container port>
- If we used a volume (memory space outside the container) for a container in Ubuntu we can see and access that from our host machine. Because it is on your host machine and you can access that just like another file. Seems obvious, yes it is but for Ubuntu.
First, understand this that for Mac OS Docker has two way to go.
- Docker Toolbox (The old way)
- Docker for Mac (The new way)
If you stuck at a point and you went over to stack overflow there might be a chance you will see an answer which is supposed to run on Docker Toolbox and you are running that on Docker for Mac and eventually messed up everthing.
Everywhere you read they say that docker does not run on a VM, it runs on the host OS. Well here comes the twist.
Docker Toolbox do run a VM of Linux on Mac OS on which your Docker containers run.
So if you are using this toolbox remember that you have a virtual OS of Linux in between your Mac OS and docker container. How this affects your system, you asked?
Now if you expose a port form Docker container it will be exposed to the Linux OS and not directly to Mac OS. Now you need to expose that port from Linux OS to Mac OS. See, extra work and a headache.
If your Docker container uses a volume, it will be on Linux OS and not on your MacOS. Which means you can't access it, knowing that it exists.
If you want to see that, then you need to screen into that Linux OS and then access that volume. See another headache.
With the new version, for mac, they have removed the heavy Linux layer from in-between which uses VM.
Instead, now docker is a native application but it still uses a native virtualization framework called HyperKit.
How does it affect you?
If you expose the port from the container it will be available to your host. So no more OS exposing in between.
But we still can't access the volumes of Docker containers directly from the host. They are still inside that lightweight Linux layer. See this for more information on how you can access those.
If you run into some weird problem with docker container then do remember the followings:
In Ubuntu (or other Linux) there is no middle layer between docker containers and host OS. And I guess 90% of the time your production OS is also Ubuntu. But on your MacOS there is a layer in between.
Although Docker team has worked wonderfully to make it as native as it can be but there is still a light layer of Linux in between MacOS and containers.
Also check that the solution on stack-overflow might be for Docker Toolbox and not for Docker for Mac (new version).
If it can help anyone struggling to make things work with docker, it serves the purpose.
Thank you so much for reading, and let's discuss more in the comment section. Throw away your thoughts on this and point out if I made mistake. Let's learn together.