Docker for Mac | Know your tools #2
Tags: docker, knowyourtools, productivity, deepunderstanding
Published: 2019-03-01 06:37:54

---
title: Docker for Mac | Know your tools #2
published: true
description: Docker can take you out from production headache. But what Docker can also do is give you a lot of headaches when you are debugging and you don't know how the hell it works. 
tags: #docker #know-your-tools #productivity #deep-understanding
---

![Docker Image](https://thepracticaldev.s3.amazonaws.com/i/8kex8622nc3p5je4c9g6.png)


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.

## What is here for you?

* 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.

---

## The difference

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.

So what is Docker? [Docker](https://www.docker.com/) is just an implementation of this approach. And it is not alone in the market, [Kubernetes](https://kubernetes.io/) is also doing 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.

---


### Now let's dig, on how it works.

![digging deep](https://thepracticaldev.s3.amazonaws.com/i/gk4od66qmuexrsbplzdp.jpg)

---


__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 `-p` option 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. 


__Mac OS__

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__. 

### Docker Toolbox

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.

### Docker for Mac

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__](https://github.com/moby/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](https://stackoverflow.com/a/41281658/6274382) for more information on how you can access those.

---

## Takeaways

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.