Skip to content

Instantly share code, notes, and snippets.

@kosmatov
Created May 30, 2024 20:07
Show Gist options
  • Save kosmatov/012968a86405a31fe07d5be3ef94ef5d to your computer and use it in GitHub Desktop.
Save kosmatov/012968a86405a31fe07d5be3ef94ef5d to your computer and use it in GitHub Desktop.

Getting started with docker inside colima on Mac OS

NOTE: This is experimental way to setup the project

How it works TL;DR

Colima is a wrapper for Lima (Linux Machines) VM on Mac OS. Colima provides transparent access to container runtime inside a Lima VM so it looks like you work with container runtime on your host machine — you can use docker and docker-compose the same way as on Linux. Your home directory mounts directly inside the Lima VM in read/write mode. It provides a good performance with sshfs mounts but colima declare a much better performance with virtiofs

Requriements

Mac OS version 13.0 or later with installed homebrew

Installation

Install docker, docker-compose and colima

brew install --cask docker docker-compose colima

Edit colima default template

$EDITOR ~/.colima/_templates/default.yaml

Minimum recommended values:

cpu: 4
disk: 60
memory: 4
arch: host
runtime: docker
vmType: vz
mountType: virtiofs

Usage

Run project apps

To run project apps use the next command:

docker-compose up

To see information about running containers run:

docker-compose ps -a

Colima maps service ports to localhost so the apps will be available on your host machine

Run bundle, rspec and rubocop

There are a few ways to use this setup depends on your workflow. The main idea is run bundle command by docker-compose inside console container which provides test environment in your project

Sample docker-compose config:

console:
  build:
    context: docker/
    dockerfile: Dockerfile.dev
  environment:
    RACK_ENV: development
  volumes:
    - .:/app
    - ~/.bash_history:/root/.bash_history
    - ~/.pry_history:/root/.pry_history
    - ~/.gitconfig:/root/.gitconfig
  working_dir: /app
  command: bash

docker/Dockerfile.dev:

FROM ruby:latest

RUN gem install solargraph &&\
  solargraph download-core

1. Use docker-compose

Examples:

docker-compose run --rm console bundle exec rubocop
docker-compose run --rm console bundle exec rspec spec/lib
docker-compose run --rm -e PROFILE=1 -e LOG=1 console bundle exec rspec spec/lib

2. Add shell aliases

For zsh:

alias dc-bundle="docker-compose run \$([ -n \"\$ENV\" ] && echo \" \$ENV\" | sed -r 's/[[:space:]]+/ -e /g') --rm console bundle \$@"

Example commands:

dc-bundle exec rubocop
dc-bundle exec rspec spec/lib
ENV="PROFILE=1 LOG=1" dc-bundle exec rspec spec/lib

Make it faster with a running console container:

alias dc-console="docker-compose ps -a --status running | cut -d\  -f1 | grep console | head -1"
alias dc-bundle="docker exec -ti \$([ -n \"\$ENV\" ] && echo \" \$ENV\" | sed -r 's/[[:space:]]+/ -e /g') -w /app/$(dc-dir) \$(DC_CONSOLE=\$(dc-console) [ -n \"\$DC_CONSOLE\" ] && echo $DC_CONSOLE || echo console) bundle \$@"

3. Configure your IDE or editor

Use commads from p.1 or aliases from p.2 to configure your editor

Run LSP server

Use LSP server for Ruby development if you still don't. LSP server solargraph must be installed in console container

Configure your editor

Example plugin config for lazyvim:

local function root_dir()
  vim.fn.finddir(".git/..", vim.fn.expand("%:p")..";")
end

return {
  "neovim/nvim-lspconfig",
  dependencies = {
    { "folke/neodev.nvim", opts = { setup_jsonls = false } },
  },
  opts = {
   servers = {
      solargraph = {
        cmd = {
          "docker-compose",
          "run",
          "--rm",
          "-v",
          root_dir() .. ":" .. root_dir() .. ":z",
          "-w",
          root_dir(),
          "console",
          "solargraph",
          "stdio"
        }
      },
    },
  },
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment