colima icon indicating copy to clipboard operation
colima copied to clipboard

Feature request: Automatic start on login

Open rfay opened this issue 3 years ago • 12 comments

People are used to Docker Desktop coming up at login. Can we add the same option with Colima?

I experimented a bit with adding a service using homebrew, but wasn't successful. I think it's probably because colima is already setting up its own launchctl. Not sure what the options might be but letting brew services manage the launchctl might be a more integrated solution. My (failed) experiment is at https://github.com/rfay/homebrew-core/blob/601260825034aefbecb50f0a3202f7f0e4f7b197/Formula/colima.rb#L46-L55

rfay avatar Dec 27 '21 20:12 rfay

@abiosoft I have a Homebrew service that works on my machine,

https://github.com/Homebrew/homebrew-core/pull/94189

give it a try! Leave a thumbs up on the PR if it works, or a comment if it doesn't please!

EDIT: it was merged, hopefully it works for everyone. It should, but I guess we can close this issue.

NickHackman avatar Feb 01 '22 01:02 NickHackman

I wanted to do more testing, prior to having the PR merged. This is where I'm lacking in understanding when it comes to Colima.

The service starts in Homebrew and runs effectively, but after some time it restart loops on my machine. I sadly, didn't test for this as I expected once it's up it'll run and do its daemon thing. Looking through logs,

Brew Logs: located at /usr/local/var/log/colima.log

time="2022-02-01T09:38:44-06:00" level=info msg="[hostagent] The final requirement 1 of 1 is satisfied"
time="2022-02-01T09:38:44-06:00" level=info msg="READY. Run `limactl shell colima` to open the shell."
time="2022-02-01T09:38:44-06:00" level=info msg="provisioning ..." context=docker
colima
Current context is now "colima"
time="2022-02-01T09:38:45-06:00" level=info msg="starting ..." context=docker
  * /var/log/docker.log: creating file
  * /var/log/docker.log: correcting owner
  * Starting Docker Daemon ... [ ok ]

Above shows that Colima has started successfully, I never see any sort of error case happening in these logs.

Initial Thoughts

The issue may be that the livecheck doesn't check the health of the application, so the keep alive from Brew is just restarting it constantly.

  livecheck do
    url :stable
    regex(/^v?(\d+(?:\.\d+)+)$/i)
  end

Removing the keep-alive from the brew config doesn't resolve the issue. I don't believe this is the issue, not sure what else could be causing it.

@abiosoft any ideas here? Can you reproduce (if you have a mac)

NickHackman avatar Feb 01 '22 16:02 NickHackman

That log outputs indicates successful startup. How does the keep_alive work in brew, what command is it running to verify, or is it basically monitoring the process?

I would say that keep_alive should be set to false. Colima does not stay in foreground and it can be checked with colima status.

This is most likely the cause of https://github.com/abiosoft/colima/issues/156https://github.com/abiosoft/colima/issues/156.

abiosoft avatar Feb 01 '22 17:02 abiosoft

Will revert in Homebrew, removing keep_alive is still hitting the same situation, the logs say it's starting, but

docker ps

fails, will open another draft and keep that open to ensure it works later tonight. Will reach out then to get approval from others of it working.

EDIT: revert PR https://github.com/Homebrew/homebrew-core/pull/94264

NickHackman avatar Feb 01 '22 17:02 NickHackman

Thanks for your efforts @NickHackman

abiosoft avatar Feb 01 '22 17:02 abiosoft

@rfay's original attempt looks promising. It might just be missing setting PATH and HOME from @NickHackman's version.

carlocab avatar Feb 01 '22 18:02 carlocab

Ah, no, I think we need a way to specify LaunchOnlyOnce in the service block:

     LaunchOnlyOnce <boolean>
     This optional key specifies whether the job can only be run once and only once.  In other words, if the job cannot be safely
     respawned without a full machine reboot, then set this key to be true.

See also https://www.karltarvas.com/2020/09/11/macos-run-script-on-startup.html.

I don't think this is supported in Homebrew yet, but PRs to implement this in https://github.com/Homebrew/brew/blob/423f34dee16e036f685b992a3ba3db29261c8f70/Library/Homebrew/service.rb are welcome.

carlocab avatar Feb 04 '22 14:02 carlocab

@abiosoft Correct my understanding here, but running colima start forks and starts the daemon for the corresponding container runtime? Reading the launchd man pages on MacOS:

Daemons or agents managed by launchd are expected to behave certain ways.

A daemon or agent launched by launchd MUST NOT do the following in the process directly launched by launchd:

  • Call daemon(3).
  • Do the moral equivalent of daemon(3) by calling fork(2) and have the parent process exit(3) or _exit(2).

Looking at the logs of colima start

> msg="[hostagent] The final requirement 1 of 1 is satisfied"
> msg="READY. Run `limactl shell colima` to open the shell."
INFO[0020] provisioning ...                              context=docker
> colima
> Current context is now "colima"
INFO[0020] starting ...                                  context=docker
>  * /var/log/docker.log: creating file
>  * /var/log/docker.log: correcting owner
>  * Starting Docker Daemon ... [ ok ]
INFO[0025] waiting for startup to complete ...           context=docker
INFO[0026] done                                         

From my understanding Colima breaks the above requirements of launchd where it creates a daemon and then colima start exits.

@carlocab do you have experience with other applications that behave this way working with launchd in one way or another?

NickHackman avatar Feb 15 '22 03:02 NickHackman

Not really, but my understanding is that this is why we want LaunchOnlyOnce: we don't really want launchd to manage the colima daemon (because it's complicated -- my impression is that there actually isn't a colima or even a lima daemon, just a background qemu process/daemon that is queried whenever you invoke colima/lima/limactl). We just want launchctl to call it at startup.

This does mean that we don't have the benefit of launchd restarting it when needed (e.g. in event of a crash), but that probably requires some changes in colima or lima to work properly.

carlocab avatar Feb 15 '22 03:02 carlocab

I've been testing Colima with the LaunchOnlyOnce flag in brew (was going to open a PR until I found the above requirements by launchd)

  plist_options manual: "colima"
  service do
    run [opt_bin/"colima", "start"]
    run_type :immediate
    keep_alive false
    launch_only_once true
    environment_variables HOME: ENV["HOME"], PATH: std_service_path_env
    error_log_path var/"log/colima.log"
    log_path var/"log/colima.log"
    working_dir ENV["HOME"]
  end

It's still following similar behavior where it'll start where colima status returns that it's running and then after starting a second or two will pass and then it'll die according to colima status and trying to interact with the docker daemon

Doesn't say anything after it dies in the logs located at /usr/local/var/log/colima.log nor anything new at ~/.lima/colima/serial.log maybe there's something I'm missing when it comes to debugging?

NickHackman avatar Feb 15 '22 03:02 NickHackman

I'd like to get colima running as a service is there anything I can do to assist?

earlbw avatar Mar 24 '22 10:03 earlbw

I'm using Automator as a workaround for now. https://chrisfromredfin.dev/posts/colima-autostart/

chrisfromredfin avatar Sep 14 '22 00:09 chrisfromredfin

@abiosoft I have a Homebrew service that works on my machine,

Homebrew/homebrew-core#94189

give it a try! Leave a thumbs up on the PR if it works, or a comment if it doesn't please!

EDIT: it was merged, hopefully it works for everyone. It should, but I guess we can close this issue.

Hi, @NickHackman need help. I use brew but it says colima not implemented #plist, #service or installed a locatable service file, how can i solve this problem

brew services start colima
Error: Formula `colima` has not implemented #plist, #service or installed a locatable service file

qinf avatar Nov 24 '22 14:11 qinf

I'm using Automator as a workaround for now. https://chrisfromredfin.dev/posts/colima-autostart/

Hi - I just tried this and found that Colina was in usr/local/bin not /opt/homebrew/bin/

Also, I got the error "zsh:2: permission denied: /usr/local/bin/” even though colima stat works just fine in terminal.

Any ideas?

ghost avatar Dec 14 '22 12:12 ghost

(Sidenote: @JCHH2 on arm64 (M1) computers, homebrew puts things in /opt/homebrew, on macOS amd64 it puts them in /usr/local/Homebrew.)

rfay avatar Dec 14 '22 14:12 rfay

If you wanted to enable this for yourself trivially you could add a shell script like the following colima-launcher, alongside .../bin/colima:

#!/usr/bin/env bash

colima start
/opt/homebrew/gnubin/sleep infinity

And modify the plist file to point at the new script.

It comes with the caveat of having no relationship to the forked processes colima creates and therefore not responding to the failure of the colima, lima or qemu processes at all. It's only suitable for a run once invokation but could be cycled with brew services restart colima if failed.

It seems like it could be useful to add a configuration flag to colima to create subprocesses as children instead as it would allow the desired behaviour when being run as a service/agent.

tggreene avatar Mar 02 '23 11:03 tggreene

I am currently starting Colima on macOS with a little wrapper script: https://gist.github.com/fardjad/a83c30b9b744b9612d793666f28361a5#file-how-to-start-colima-automatically-on-macos-md

The script does the following:

  1. Starts Colima and keeps retrying until it succeeds.
  2. It waits until it receives a shutdown signal from launchd and then tries to stop Colima gracefully.

This is less prone to restart loop issues compared to the previous attempts and supports the use-case where the user wants to manually stop colima with colima stop.

@abiosoft It would make things much easier if Colima supported a foreground mode so we could leave it to launchd to manage the process.

fardjad avatar Mar 20 '23 12:03 fardjad

@abiosoft It would make things much easier if Colima supported a foreground mode so we could leave it to launchd to manage the process.

Oh, I never thought of this. Yeah, foreground mode is doable.

abiosoft avatar Mar 20 '23 13:03 abiosoft

I am currently starting Colima on macOS with a little wrapper script: https://gist.github.com/fardjad/a83c30b9b744b9612d793666f28361a5#file-how-to-start-colima-automatically-on-macos-md

@fardjad Your script is great. I just need to change it to work for me. Colima installed with brew.sh

- export PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
+ export PATH="/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"

jd-ucpa avatar Apr 04 '23 09:04 jd-ucpa

@abiosoft I have a Homebrew service that works on my machine, Homebrew/homebrew-core#94189 give it a try! Leave a thumbs up on the PR if it works, or a comment if it doesn't please! EDIT: it was merged, hopefully it works for everyone. It should, but I guess we can close this issue.

Hi, @NickHackman need help. I use brew but it says colima not implemented #plist, #service or installed a locatable service file, how can i solve this problem

brew services start colima
Error: Formula `colima` has not implemented #plist, #service or installed a locatable service file

Looks like the change was reverted: https://github.com/Homebrew/homebrew-core/pull/94264

nick-kang avatar Apr 08 '23 08:04 nick-kang

This is the sole reason preventing me from switching from Docker Desktop to colima 😊 Seems like this is tantalizingly close? 😄

wimleers avatar Apr 20 '23 13:04 wimleers

Would be cool to have this, not a big deal running it manually but would be very nice to have autostart.

ntrp avatar May 25 '23 06:05 ntrp

Would be cool to have this, not a big deal running it manually but would be very nice to have autostart.

It is a big deal if you're serving actual websites to live traffic from Docker 😇 Power interruptions and/or kernel panics should then cause everything to automatically restart. (I've had both happen already.)

(I serve https://wimleers.com like this: browser → Fastly → port forwarding on my ISP's modem since it blocks ports 80 and 443 → Mac Mini → ddev instance in Docker.)

wimleers avatar May 29 '23 11:05 wimleers

Would be cool to have this, not a big deal running it manually but would be very nice to have autostart.

It is a big deal if you're serving actual websites to live traffic from Docker 😇 Power interruptions and/or kernel panics should then cause everything to automatically restart. (I've had both happen already.)

(I serve https://wimleers.com like this: browser → Fastly → port forwarding on my ISP's modem since it blocks ports 80 and 443 → Mac Mini → ddev instance in Docker.)

Well, you can easily add a script to start colima on boot as other have suggested. Of course an official solution would be nicer

ntrp avatar May 29 '23 11:05 ntrp

I'd love to see this too, but it's definitely not a deal-breaker.

In the short term, I've been using this ansible-role to manage the launch agent on OSX. (I use Ansible to end-to-end provision my macs...)

niall-byrne avatar Jun 18 '23 23:06 niall-byrne

I've implemented the foreground mode. It works with already created service in brew (and then reverted) https://github.com/abiosoft/colima/pull/789

regulskimichal avatar Aug 30 '23 16:08 regulskimichal

Hi, the FAQ mentions that "since 0.5.6" Colima autostart is easy with brew services start colima. At the same time, the last released version is 0.5.5. Any idea when 0.5.6 is going to be released?

matan129 avatar Sep 28 '23 11:09 matan129

I just upgraded to 0.5.6 but brew services start colima gives me the error:

Error: Formula colima has not implemented #plist, #service or installed a locatable service file

Weltraumschaf avatar Oct 05 '23 12:10 Weltraumschaf

https://github.com/Homebrew/homebrew-core/pull/149670

regulskimichal avatar Oct 05 '23 12:10 regulskimichal

The commit has been merged. Please verify if it works for you guys.

regulskimichal avatar Oct 09 '23 10:10 regulskimichal