Ultimate Guide to Docker M1

Ultimate Guide to Docker M1

What is Docker?

Docker is a Platform as a Service (PaaS) based product that unleashes OS-level virtualization to deliver software in packages known as containers. 

The service has both free and premium options. Docker Engine is the software that allows developers to host their code in a container. It was first introduced in 2013 and developed by Docker, Inc.

Docker on MacBook Pro with M1 Pro Chip in 2022

Running Docker on MacBook Pro with an MI chip has been a severe problem for many developers since its launch. In many cases, Docker shows the worst performance on macOS; consequently, developers are forced to look for alternatives.  

Many developers replace their MacBooks with Linux-based computers to cope with this problem.

Is it Worth Buying MackBook Pro with M1 Chip?

Arguably, the MacBook, with the latest Apple M1 Pro chip and ARM architecture, is an incredibly powerful machine. Upon using this machine for the first time, you’ll experience a huge difference. It is insanely fast and quiet and does not get heated up while you are running memory-intensive software.

When it comes to MacBook compatibility with Docker, there is no one-step solution to this problem. To figure out the solution to this problem, we perform some benchmarking.

Docker Performance on Apple MacBook Pro with M1 Processor

MacBook Pro with an M1 processor is undeniably an incredibly fast, cool, and silent workhouse. It has tremendous processing power and never cools down while running, unlike its previous editions. On top of that, its battery endurance is impressive.

The power and energy consumption are the result of the new M1 processor architecture that relies on the ARM platform. Previous MacBook editions were based on Intel/AMD architectures.

While most of the daily software runs on the M1 processor, there is still some software that might give you a headache. 

Since a vast majority of software developers use MacBook for development purposes and Docker for creating local development environments, let’s dig deeper into the current state of Docker on the M1 MacBook.

Docker on M1 MacBook 

Since Docker and MacBook have never been my best friend because Docker is explicitly designed for Linux users. Although it runs on the Windows platform as well, its performance on macOS is worst.

Even though Docker has been modified and officially released for M1 and many of your Docker projects may be running smoothly on your M1 machine, the performance of your new and expensive MacBook may be pathetic, even when compared to its predecessors. 

Two Problems

The developers of Docker stated two main reasons for this inconvenience. First, file I/O has always been a severe headache for MacOs users while running Docker. Second, poor performance with Docker images depends on AMD/Intel architecture. 

VirtioFs Comes to Rescue

The above-mentioned problems can be fixed with VirtioVS. Luckily, macOS has extended support for VirtioFS, a file system for virtualization that relies on FUSE. The latest versions of Docker are equipped with an experimental build that allows macOS users to activate the VitrioVS support.

When you try it on your M1 MacBook, you’ll experience a noticeable difference in the Docker performance. On top of that, you’ll experience volume mount, and file-based operations are 2 to 10 times faster than before. Eventually, it boosts the PHP/Apache performance and allows importing of huge MySQL dumps seamlessly.

You can read the VirtioFs complete documentation from here. If you want an experimental Docker to build, visit this link: https://github.com/docker/roadmap/issues/7#issuecomment-975447079.

Benchmark Methodology

Docket M1 requires some configuration and setting that could influence the performance. All you need to do is to allocate the resources of your MacOS to different parts of Docker. For instance, you can allocate 4 CPUs and 8 GB of RAM or increase or decrease that according to your need. 

Here is the recommended settings for MacBook to run Docker.

docker m1

Building Docker Images

To make the process easier for our reader, we start with something that cannot be influenced by volume performance: Docker Builds.

docker m1

The above image clearly shows that the difference is enormous. You’ll be surprised that Docker images were built on MacBook pro in just seven minutes, which is relatively lower than the build time on the new VPS. Some might argue that the developer has allocated too many resources to Docker, but that’s not the reason for its exceptional performance.

If the developer runs many I/O disk operations aside from Docker, performance will remain the same. Therefore, Docker M1 best suits developers who cannot afford to waste too much time on code deployment and sharing.

Moreover, while running the Docker application on your M1, you’ll be astonished to see that your machine is not slowing down and reaching 600C.

Based on the evidence we’ve shown earlier, anyone can deduce that Docker runs faster on the MacBook Pro, but to learn more about running Docker on MacBook M1, continue reading this article till the end. 

How Fast Docker Container Will Handle Request on Apple Silicon?

We won’t delve deeper into volume configuration; instead, we’ll just check cached and delegated. While checking both these features, we strongly recommend you set up mutagen because it is very promising and recommended by many developers.

docker m1

Volume setting does not influence performance. It performs slightly better than the old version of the MacBook, but it is still relatively slower for both cached and delegated. What is more interesting here is how fast it works with the mutagen.

If we close all the I/O disk operations, we will likely experience an exceptional performance that we may not experience even with Linux.

Is Docker Desktop with Mutagen Slower than Running Docker on VPS?

Running Docker on VPS is proven and offers many benefits. Running Docker on MacBook M1 with mutagen is an alternative solution we discussed earlier.

On the flip side, VPS is a bit slower and resource intensive, but it can compete with Linux-based servers. Let’s see if VPS can beat M1 pro if we assign it the same CPU core and RAM.

  docker m1

The results are fantastic, it is relatively faster than all the previous VPS, but amazingly it is slower than Docker on MacBook Pro M1 with the mutagen.

How to Install Docker on Mac M1?

To download Docker Desktop Installer, visit the official website:

Select the “Mac with Apple chip” option in the “Download Docker Desktop for Mac.” On clicking the link, downloading of Docker.dmg file will start automatically.

docker m1

Install the Docker File

docker m1

Once the dmg file is downloaded completely, double-click the file, and the following window will pop up.

docker m1

Your next step will be transferring the Docker app into the Application folder.

Subsequently, there will be a series of steps that you need to follow.

docker m1

docker m1

Upon successfully completing the installation, the Docker app will be available in the Application folder.

Running Docker Desktop

Simply double-click on the Docker app so you can run the Desktop Docker app.

docker m1

To confirm whether the Docker app is running smoothly, check the Docker icon in the menu bar that will say Docker is running.

docker m1

How to Build Docker Container on MacBook Pro M1?

Lima is a tool that is used as a container for Mac. Leveraging this tool, you can run Linux operating system on your Mac. It is equipped with automatic file forwarding and flawless file sharing and supports many Linux distributions such as AlmaLinux, Alphine, Arch Linux, Debian, Fedora, SUSE, Oracle Linux, Rocky, and Ubuntu.

Even though Lima is primarily designed for containerization, you can also use it for other tasks.

Mac Computers with Apple Silicon

In 2020, Apple released its first system packed with Apple silicon, a customized arm-based chip for the MacBook Pro. The most recent edition of MacBook pro harnesses the M1 Apple Silicon processor, although it has been used in an Intel processor before.

This new custom chipset could not process Intel instructions without the aid of additional software. Applications that were built for Intel can run on Mac Pro M1 with the aid of emulation software that Apple has made widely accessible, “Rosetta 2”.

Rosetta 2 is phenomenal software, although it has some processing limitations. In that case, we have to use Docker.

Challenges with Docker While Using MacBook M1

A vast majority of developers use Docker for their local development work. By default, Docker supports amd64 by specifying the  – – platform Linux/amd64 for building and running containers. Most packages and images support amd64, but not all the packages can be transitioned.

If you are caught up in a scenario where you need Intel architecture for a container, you can spin up a virtual machine for an appropriate architecture.

Setting Up Lima for Building Docker Images

Lima is the perfect solution if you want to set up an environment where you can build Docker containers without transferring files to another system or resorting to an old MacBook.

You can use the brew command to install Lima on your macOS: brew install lima. Once it is successfully installed, open it using the command: limactl start.

Subsequently, you’ll see the following option on the screen:

Open an editor to review or modify the current configuration

On the line where you find arch: null, replace it with x86_64. By default, it will use the architecture of your operating system. In case you have MacOS with an M1 processor, it will be aarch64. Upon saving the configuration, type the following command:

limactl shell default

sudo systemctl start containerd 

sudo nerdctl run –privileged –rm tonistiigi/binfmt –install all exit

It is required for the guest OS to execute non-native binaries. Now you’re done with the installation. Lima is packed with nerdctl and containerd for developing Docker images.

Creating Alias for Building Docker Images with Lima Easy

You can create a Docker image with the following command: lima nerdctl build, where lima nerdctl is a perfect alternative for the Docker command. To keep your current way of working,  you can add an alias:

alias docker = ‘lima nerdctl’

Type the following command:

echo alias docker=\’lima nerdctl\’ >> ~/.zprofile

The command listed above will make an alias persistent. Eventually, you can use the Docker command on your M1 MacBook, as usual, to build and run images using an emulated Intel processor. 

You can replace the name of alias with Docker if you want to keep using your native architecture for building images.

When you run a Docker image through lima, port forwarding is triggered automatically, so all the ports attached to your container will be sent to the local host. Additionally, you can create many lima instances and use them simultaneously. 

The limactl start command can be applied additional argument: limactl start foobar will create lima instance known as foobar. With LIMA_INSTANCE = foobar lima [command], you can execute multiple commands on that specific instance.

Shortcomings

The MacBook Pro M1 home directory is read-only by default. To bypass this default setting, you have to edit the ~/.lima/default/lima.yaml file and assign the value ‘true’ to the writeable attribute, and restart lima with the command (limactl stop && limactl start).

Since the lima home directory is writable, so if you place your code there, it will eliminate the need to make the M1 home directory editable.

Another problem with running Docker on MacBook Pro M1 is speed. While compiling a code in your container, you’ll experience a noticeable difference between native architecture and emulated ones. Unfortunately, this drawback is not going to resolve anytime soon. 

Copywriter1
Other

Leave a Comment

Your email address will not be published. Required fields are marked *

Share via
Copy link
Powered by Social Snap