Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Automatic start on login #96

Closed
rfay opened this issue Dec 27, 2021 · 33 comments
Closed

Feature request: Automatic start on login #96

rfay opened this issue Dec 27, 2021 · 33 comments

Comments

@rfay
Copy link
Contributor

rfay commented Dec 27, 2021

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

@NickHackman
Copy link

NickHackman commented Feb 1, 2022

@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.

@NickHackman
Copy link

NickHackman commented Feb 1, 2022

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)

@abiosoft
Copy link
Owner

abiosoft commented Feb 1, 2022

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 #156.

@NickHackman
Copy link

NickHackman commented Feb 1, 2022

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 Homebrew/homebrew-core#94264

@abiosoft
Copy link
Owner

abiosoft commented Feb 1, 2022

Thanks for your efforts @NickHackman

@carlocab
Copy link

carlocab commented Feb 1, 2022

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

@carlocab
Copy link

carlocab commented Feb 4, 2022

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.

@NickHackman
Copy link

@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?

@carlocab
Copy link

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.

@NickHackman
Copy link

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?

@earlbw
Copy link

earlbw commented Mar 24, 2022

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

@chrisfromredfin
Copy link

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

@qinf
Copy link

qinf commented Nov 24, 2022

@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

@ghost
Copy link

ghost commented Dec 14, 2022

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?

@rfay
Copy link
Contributor Author

rfay commented Dec 14, 2022

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

@tggreene
Copy link

tggreene commented Mar 2, 2023

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.

@fardjad
Copy link

fardjad commented Mar 20, 2023

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.

@abiosoft
Copy link
Owner

@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.

@jd-ucpa
Copy link

jd-ucpa commented Apr 4, 2023

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"

@nick-kang
Copy link

@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: Homebrew/homebrew-core#94264

@wimleers
Copy link

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

@ntrp
Copy link

ntrp commented May 25, 2023

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

@wimleers
Copy link

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.)

@ntrp
Copy link

ntrp commented May 29, 2023

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

@niall-byrne
Copy link

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...)

@regulskimichal
Copy link
Contributor

I've implemented the foreground mode. It works with already created service in brew (and then reverted) #789

@matan129
Copy link

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?

@Weltraumschaf
Copy link

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

@regulskimichal
Copy link
Contributor

Homebrew/homebrew-core#149670

@regulskimichal
Copy link
Contributor

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

@yakimant
Copy link

yakimant commented Oct 9, 2023

Works for me, thanks @regulskimichal !

@Weltraumschaf
Copy link

Works for me, too. Thanks!

@rfay
Copy link
Contributor Author

rfay commented Nov 13, 2023

Fixed in v0.6.0, thanks!

@rfay rfay closed this as completed Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests