Run Docker on macOS M1 machine

Abhijit Roy
5 min readOct 24, 2022

--

CI/CD Tools: Docker

Containers on a Docker hub being served by a ship as server host machine

Hello folks!

Have you heard about “A large ship carrying containers”?

Of course, yes! Right?

So, here is a similar mechanism that you can do in the world of Software Engineering.

Now, the question must be:

“Why do we even need a shipping tool, rather developer can just install the required tools in order to use the package or library?”

Good Q.!

So, let’s dive in…

Docker is all about shipping your software (app, package, library) that can be run on different machine OS: Linux, Windows, macOS with both popular architectures: Intel/AMD, ARM/M1.

Since Docker Engine only runs on Linux, developers who use Windows and macOS for software development cannot run the engine until they spin up a virtual machine (VM) that runs linux.

Luckily, Windows have a linux VM running setup done in Win 10. And for macOS, one has to spin up a linux VM using lima.

linux VMs on multiple OS

This article is about running Docker on macOS M1 machine.

For M1 machine, we have only 1 option left to run VM i.e. Lima.

Lima

A CLI based Linux VM that runs on top of macOS M1 machine. It’s similar to running Ubuntu (via WSL) on top of Windows, if you are someone like me coming from Windows ecosystem.

Here, I am referring to Virtualization. Following is the difference in comparison to emulation:

- Virtualization: Install OS2 (with arch-1) on OS1 (with arch-1) => Same architecture

- Emulation: Install OS2 (with arch-2) on OS1 (with arch-1) => Different architecture

Here is my installation guide for lima.

Docker CLI

Docker CLI is FREE! So, I would recommend this as default.

Docker Desktop

It’s now PAID. After all, Docker Inc. (the company behind) also has to make revenue. Hence, not recommended for developers.

Docker

We shall install docker on top of linux VM, but not macOS.

So, after installing lima following the instruction, just start an instance of linux VM.

[abhi3700@MacBook-Pro] ~/F/coding/github_repos/My_Learning_DevOps (main) ⚡ 
❯ lima
abhi3700@lima-default:/Users/abhi3700/F/coding/github_repos/My_Learning_DevOps$

Here, you can see that I am able to get into linux VM now on CLI in a mac M1 machine.

Install docker on linux VM:

$ sudo apt install docker.io

A small tip 💁 here:

It’s better to alias docker with `sudo docker` to avoid permission issues.

$ nano ~/.bashrc

Add this line & save:

alias docker='sudo docker'

& then activate:

$ source ~/.bashrc

Confirm docker is running:

confirm docker is running on linux VM over mac M1

Docker Hub

It is like Github that contains containers (public/private) for other developers (irrespective of which machine they use) to download/pull the image/repository & use for their purpose.

So, you need to create an account & also a repository for a software app/package/lib.

Now, login inside linux VM via CLI:

$ docker loginLogin with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.Username: abhi3700Password:WARNING! Your password will be stored unencrypted in /root/.docker/config.json.Configure a credential helper to remove this warning. Seehttps://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded

Example: Hello Docker!

Use VS Code as the editor inside linux VM. For my guide, refer here.
I have got
- VS Code installed over mac M1
- Docker over linux VM on mac M1

There’s no point installing Docker CLI over mac M1 as we would be using linux VM instead.

Now, let’s jump onto an example how to use docker.

  1. Create a directory hello-docker & open VS Code inside it.

2. Now, create a file — app.js inside the project.

console.log("Hello Docker!");

3. Add a `Dockerfile` file inside `hello-docker` directory

FROM node:alpineCOPY . /appWORKDIR /appCMD node app.js

Here is the explanation:

  • L-1: pull node:alpine docker image.
  • L-2: copy everything to an existing `app/` folder
  • L-3: now, make `app/` folder as work directory
  • L-4: run `app.js` using node

4. Build the docker image via `$ docker build -t hello-docker .` inside lima linux VM terminal.

5. Check images via `$ docker images ls`. The image with latest

$ docker image lsREPOSITORY     TAG       IMAGE ID       CREATED         SIZEhello-docker   latest    dbbcb40a83b7   5 minutes ago   167MBnode           alpine    9bcdf8fa2b21   2 days ago      167MB

Hence, we can see that the image `hello-docker` is created.

All the images are stored in this location: `unix:///var/run/docker.sock: Get “http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json"`. Nothing is present in the repo.

6. Run the docker image via `$ docker run hello-docker` inside lima linux VM terminal.

$ docker run hello-dockerHello Docker!

7. Login using docker hub credentials via `$ docker login` inside lima linux VM terminal.

8. Tag the docker image via `$ docker tag dbbcb40a83b7 abhi3700/hello-docker:latest` inside lima linux VM terminal. And then the image list would be like this:

$ docker image lsREPOSITORY              TAG            IMAGE ID       CREATED      SIZEabhi3700/hello-docker   latest   dbbcb40a83b7   3 days ago   167MBhello-docker            latest         dbbcb40a83b7   3 days ago   167MBnode                    alpine         9bcdf8fa2b21   5 days ago   167MB

9. Now, create a repository in the name of `hello-docker` in docker hub under username/account: `abhi3700`. Hence, the host would be `docker.io/abhi3700/hello-docker`.

10. Publish the docker image to docker hub via `$ docker image push abhi3700/hello-docker:latest` inside lima linux VM terminal.

The push refers to repository [docker.io/abhi3700/hello-docker]c61658740dfd: Pushedd0f090d9a0c6: Mounted from library/node8678965b03c8: Mounted from library/node7c50737eaab2: Mounted from library/node5d3e392a13a0: Mounted from library/nodehello-docker: digest: sha256:7929aa35b2234697d88c7aa01ceef3d2cd8cf6d7a4579aaa76bd8f03afd5e5b0 size: 1365

11. In the docker hub, the image would be published & it looks like this:

abhi3700/hello-docker image published in docker-hub

12. Now, if someone wants to use your image, they just need to pull the image from docker hub via `$ docker pull abhi3700/hello-docker` inside any other linux VM terminal using this.

13. Now, the VM doesn’t have node. So, just run the docker image via `$ docker run abhi3700/hello-docker`. So, you don’t need any further tool installation or something but docker.

That’s it for now…

You can find the entire code here in Github.

Till then, stay tuned! Take care 🙂.

See you on my next blog.

Thanks for giving your precious time to this article.

--

--

Abhijit Roy
Abhijit Roy

Written by Abhijit Roy

Blockchain (Rust, Solidity, C++, JS/TS) || Data Science (Python) || Bot (Python) || Backend (JS, TS)

No responses yet