Colima and Docker on macOS
Docker is a very popular tool to package applications and their dependencies into containers. Colima is an alternative to Docker Desktop for macOS and Linux.
Time to time, I use docker containers for my personal projects. Since my use case is not daily work, I keep forgetting the commands. So, I wanted to put some of the relevant commands into a note.
Installation and Setup
On macOS, the easiest way is to use Homebrew.
brew install colima docker docker-compose docker-Buildx
Create Docker CLI plugins folder:
mkdir -p ~/.docker/cli-plugins
symlink docker-compose:
ln -sfn $(brew --prefix)/opt/docker-compose/bin/docker-compose ~/.docker/cli-plugins/docker-compose
symlink docker-buildx:
ln -sfn $(brew --prefix)/opt/docker-buildx/bin/docker-buildx ~/.docker/cli-plugins/docker-buildx
Starting Colima
Colima runs containers in a virtual machine with 2 CPUs, 2GiB memory and 60GiB storage (which can be changed). An image will be downloaded on the first run.
colima start
output:
INFO[0000] starting colima
INFO[0000] runtime: docker
INFO[0000] preparing network ... context=vm
INFO[0000] starting ... context=vm
INFO[0021] provisioning ... context=docker
INFO[0021] starting ... context=docker
INFO[0027] done
Custom VM
The following creates a VM w/ 4 CPUs, 4GB RAM, and 100 GiB storage space,
colima start --cpu 4 --memory 4 --disk 100
Emulate x86_64
The following creates a Colima profile named amd64 that emulates x86_64 architecture,
colima start --profile amd64 -a x86_64
Native virtualization on macOS
In order to use Apple's native virtualization framework (requires macOS 13+):
colima start --arch aarch64 --vm-type=vz
arch: architecture (aarch64, x86_64)vm-type: virtual machine type (qemu, vz)vz: Apple's virtualization framework.vz-rosetta: enable Rosetta for amd64 emulation
List
colima ls
output:
PROFILE STATUS ARCH CPUS MEMORY DISK RUNTIME ADDRESS
default Running aarch64 4 4GiB 32GiB docker
Setting Colima context
docker context ls
output:
NAME DESCRIPTION DOCKER ENDPOINT ERROR
colima colima unix:///Users/acm/.colima/default/docker.sock
colima-amd64 * colima [profile=amd64] unix:///Users/acm/.colima/amd64/docker.sock
default Current DOCKER_HOST based configuration unix:///var/run/docker.sock
To set the docker context,
docker context use [NAME]
Containers
List containers
docker ps -a
output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b552ba797fe3 hello-world "/hello" 20 seconds ago Exited (0) 19 seconds ago naughty_kapitsa
Remove container
docker rm [CONTAINER]
Copy files/folders
docker cp container_id:/tmp/foo/tmpfile.txt .
docker cp ./tmpfile.txt container_id:/tmp/foo/
container_id can be found via docker ps -a command.
Images
Running hello-world image
If the hello-world image is not found, it will be downloaded.
docker run hello-world
output:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
70f5ac315c5a: Pull complete
Digest: sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
Interactive w/ terminal and auto-remove:
Allocates a pseudo-TTY and keeps STDIN open, and automatically removes the container when it exits.
docker run --rm -it [IMAGE]
List images
docker images
output:
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest b038788ddb22 4 months ago 9.14kB
arm64v8/rockylinux 8 1632c4ad6456 3 months ago 220MB
arm64v8/rockylinux 9-minimal d9b75e890280 3 months ago 129MB
Remove images
docker rmi [IMAGE]
Build image
For building custom images, we use a Dockerfile, such as the following;
# syntax=docker/dockerfile:1
FROM arm64v8/rockylinux:8 AS builder
WORKDIR /bundle
COPY src .
COPY CMakeLists.txt .
RUN dnf install dnf-plugins-core epel-release -y
RUN dnf config-manager --set-enabled powertools
RUN dnf install ninja-build -y
RUN dnf install git cmake -y
# RUN dnf group install "Development Tools" -y
RUN dnf install binutils bison flex gcc gcc-c++ gdb glibc-devel libtool make rpm-build -y
RUN cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S . -B ./build/Release
RUN cmake --build ./build/Release
Building an image called docker-test:
docker build -t docker-test .