Colima in macOS
Published: 2024-06-26 16:16:23

Hey there! If you're like me, trying to set up colima on your M1 Mac and getting stuck with these pesky proxy issues, self-signed certificates and volumes - don't sweat it. I've got a fix for you. 

Once we’ve wrapped up the setup, we’ll give it a whirl using docker compose and two images: mongodb and redis. These two tend to give me a bit of a headache. 🙃

#### Install packages

```shell
brew install \
  colima \
  docker \
  docker-compose \
  docker-buildx
```

#### Setup configs

```shell
# Linking the Colima socket to the default socket path. Note that this may break other Docker servers.
sudo ln -sf ~/.colima/default/docker.sock /var/run/docker.sock

# Docker plugins setup
mkdir -p ~/.docker/cli-plugins
ln -sfn $(which docker-buildx) ~/.docker/cli-plugins/docker-buildx
ln -sfn $(which docker-compose) ~/.docker/cli-plugins/docker-compose
```

#### Setup colima and certificates

NOTE: vz is macOS virtualization framework and requires macOS 13

```shell
# For m1 mac
colima start --cpu 4 --memory 4 --vm-type vz --vz-rosetta --edit --editor code

# For intel mac
colima start --cpu 4 --memory 4 --vm-type vz --edit --editor code
```

NOTE: If you need to configure self-signed certificates add the following provision, if not skip this step. Save & close the file.

```yaml
provision:
  - mode: user
    script: |
      #!/bin/bash
      sudo ln -sf /Users/<YOUR_USER>/certs/self_signed_cert.pem /etc/ssl/certs/self_signed_cert.pem
```

#### Check installs

```shell
# plugins
docker buildx version
docker compose version

# docker
docker ps

# docker certificates
docker pull hello-world

# docker compose volumes
mkdir -p ~/colima_test && cd ~/colima_test

echo 'try {
  db.check.insertOne({ _id: 1, test: true });
  print(db.check.findOne({ _id: 1 }));
} catch (err) {
  print(`ERROR: ${err}`);
}
' > init-mongo.js

echo "name: check

services:
  mongodb:
    image: mongodb/mongodb-community-server:6.0-ubi8
    restart: always
    volumes:
      - ./docker_volumes/mongodb_data_db:/data/db
      - ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
    ports:
      - 27017:27017
    command: ['--replSet', 'rs0', '--bind_ip_all', '--port', '27017']
    healthcheck:
      # Enabling replica set to execute transactions in mongodb
      test: echo \"try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'127.0.0.1:27017'}]}) }\" | mongosh --port 27017 --quiet
      interval: 5s
      timeout: 30s
      start_period: 0s
      retries: 10

  redis:
    image: redis
    restart: always
    volumes:
      - ./docker_volumes/redis_data:/data
    ports:
      - 6379:6379
    command: ['redis-server', '--save', '60', '1', '--loglevel', 'warning']
" > docker-compose.yml

docker compose up -d
docker ps
docker exec -it check-mongodb-1 mongosh --eval 'print(db.check.find())'
# You should see this output (item created in collection):
# [ { _id: 1, test: true } ]

ls docker_volumes/
# You should see this output (new directories created by volumes):
# mongodb_data_db  redis_data

# Clean up the check installs
docker compose down -v
cd ~
rm -rf ~/colima_test
```

Volume note: Colima default behaviour: $HOME and /tmp/colima are mounted as writable.



#### References:

- https://github.com/abiosoft/colima
- https://github.com/abiosoft/colima/blob/main/docs/FAQ.md