This article is converted by 简悦 SimpRead, original article at blog.argcv.com
On macOS, Docker Desktop is a very popular choice, but it is not the only option.
On macOS, Docker Desktop is a very popular choice, but it is not the only option. If you do not want to use Docker Desktop, or you are looking for a more lightweight alternative, Colima is a good choice. Colima is a container runtime management tool for macOS and Linux, providing a minimal setup process and supporting multiple container runtimes, including Docker and Kubernetes. This article will introduce how to use Colima to enable Docker and Docker BuildKit, allowing you to efficiently develop containers without relying on Docker Desktop.
1. Introduction to Colima
Colima is an open-source project aimed at providing a lightweight container runtime environment for macOS and Linux, supporting both Intel and Apple Silicon architectures. It has the following features:
- Simple and easy-to-use CLI interface: Colima offers a concise command line interface, enabling users to quickly start and manage containers with simple commands.
- Automatic port forwarding: No need to manually configure port mappings, Colima handles it automatically.
- Supports multiple container runtimes: Besides Docker, it also supports Containerd, Incus, and Kubernetes.
- Multi-instance support: Can run multiple container instances simultaneously to meet different development needs.
Colima’s GitHub repository is available at https://github.com/abiosoft/colima, where you can find more detailed information.
2. Installing Colima and Docker
1. Install Colima
Colima supports various installation methods, with Homebrew being the most recommended. Open the terminal and run the following command:
brew install colima
If you want to use the latest version, you can install the development branch:
brew install --HEAD colima
2. Install Docker
Colima depends on the Docker client to interact with the container runtime. Install Docker via Homebrew:
brew install docker
After installation, the Docker client will automatically have its environment variables configured, requiring no extra setup.
3. Starting the Docker Environment
Once installed, you can start a container environment containing Docker and Kubernetes through Colima by running the command:
colima start --kubernetes
This command will start a virtual machine that includes Docker and Kubernetes runtime environments. By default, Colima allocates 2 CPUs, 2GiB of memory, and 100GiB of storage space for the VM. If you need to adjust these parameters, you can set them with additional flags during startup. For example, to start a VM with 4 CPUs and 4GiB of memory:
colima start --cpu 4 --memory 4
You can also use colima stop to stop the VM or colima delete to delete the VM and create a fresh instance.
4. Configuring and Using Docker BuildKit
Docker BuildKit is a modern build tool that provides faster build speeds and more features. Starting from Docker version 23.0.0, BuildKit is enabled by default, meaning you no longer need to enable it via environment variables (such as DOCKER_BUILDKIT=1). However, if you want to use the advanced features provided by docker buildx, you may need to install the docker-buildx plugin manually.
1. Install Docker Buildx
docker-buildx is an official Docker extension plugin that supports advanced BuildKit features like multi-platform builds and cache optimization. Install docker-buildx via Homebrew:
brew install docker-buildx
After installation, you can immediately run the following command to verify if docker-buildx works properly:
docker-buildx version
If you see output similar to the following, it means docker-buildx has been successfully installed:
Extended build capabilities with BuildKit
2. Configure Docker Buildx
Although docker-buildx is installed, the docker buildx command is not available by default. This is because docker buildx is an extended command that needs to be added to Docker’s CLI plugins directory via a symbolic link. Run the following command to complete this step:
mkdir -p $HOME/.docker/cli-plugins
ln -sf /opt/homebrew/bin/docker-buildx $HOME/.docker/cli-plugins/docker-buildx
After completing the symbolic link, you can use docker buildx commands directly to access BuildKit features. Verify it by running:
docker buildx version
If everything is normal, you will see version information for docker buildx.
3. Using BuildKit
Now you can directly use BuildKit’s advanced features inside your Dockerfile, such as:
RUN --mount=type=bind,target=/path/to/cache,source=./local/cache \
/path/to/cache/build.sh
5. Summary
With Colima and Docker Buildx, we can easily enable Docker and BuildKit on macOS without relying on Docker Desktop. Colima provides a lightweight container runtime environment supporting multiple runtimes and architectures, while Docker Buildx offers powerful features for Docker builds. This combination not only saves system resources but also delivers greater flexibility and extensibility. If you are looking for a lightweight container development environment, Colima and Docker Buildx are definitely worth trying.