Run the container on your Mac with Lima

Linux China 2021-10-14 04:29:48

Lima Can help overcome in Mac The challenge of running containers on .

In your Mac Running containers on can be a challenge . After all , Containers are based on Linux Unique technology , Such as control group and namespace .

Fortunately, ,macOS Have a built-in Hypervisor hypervisor, Allow in Mac Running a virtual machine on a computer (VM). Hypervisor is an underlying kernel function , Instead of a user oriented function .

hyperkit Is one that can be used macOS The hypervisor runs the virtual machine  .hyperkit Designed as a “ It's very simple ” Virtual machine runner . And VirtualBox Different , It's not fancy UI Function to manage virtual machines .

You can get hyperkit, This is a minimalist way to run the container manager Linux Distribution version , And put all the parts together . But there will be many changes , And it sounds like there's a lot of work . Especially if you want to use  vpnkit ( An open source project , Used to create a virtual machine network that feels more like part of the host network ) Make network connections more seamless .


When    When these details have been solved , There is no reason to make these efforts again . Give Way  lima  One of the easiest ways to run is to use  . You can use this command to install  lima

$ brew install lima

After installation , It may take some time , Just have some fun . In order to make  lima  Know you're ready , You need to activate it . Here is the command :

$ limactl start

If this is your first run , You will be asked if you like the default , Or whether to change any of them . The default value is very safe , But I like living on the crazy side . That's why I jump into an editor , Modify from the following :

- location: "~"
# CAUTION: `writable` SHOULD be false for the home directory.
# Setting `writable` to true is possible but untested and dangerous.
writable: false

become :

 - location: "~"
# I *also* like to live dangerously -- Austin Powers
writable: true

As the comment said , This can be dangerous . It is sad , Many existing workflows rely on mount to be readable and writable .

By default ,lima  function  containerd  To manage containers .containerd  The manager is also a very concise Manager . Although using a wrapped daemon , Such as  dockerd, To increase these beautiful work efficiency is very common , But there is another way .

nerdctl Tools

nerdctl  The tool is Docker Direct replacement of the client , It puts these functions on the client , Not on the server .lima  Tools allow you to run directly from within a virtual machine without having to install locally  nerdctl.

After that , You can run a container ! This container will run a HTTP The server . You can be in your Mac Create these files on :

$ ls
$ cat index.html

Now? , Mount and forward port :

$ lima nerdctl run --rm -it -p 8000:8000 -v $(pwd):/html --entrypoint bash python
[email protected]:/#

In a container , Run a simple Web The server :

$ lima nerdctl run --rm -it -p 8000:8000 -v $(pwd):/html --entrypoint bash python
[email protected]:/# cd /html/
[email protected]:/html# python -m http.server 8000
Serving HTTP on port 8000 (<>) ...

At the other end , You can check that everything looks good :

$ curl localhost:8000

Go back to the container , There's a record HTTP Log information of client connection : - - [09/Sep/2021 14:59:08] "GET / HTTP/1.1" 200 -

One file is not enough , So we have to do some optimization . Execute on server  CTRL-C, And add another file :

Keyboard interrupt received, exiting.
[email protected]:/html# echo goodbye &gt; foo.html
[email protected]:/html# python -m http.server 8000
Serving HTTP on port 8000 ( ...

Check if you can see the new file :

$ curl localhost:8000/foo.html


To sum up , install  lima  It will take some time , But when it's done , You can do the following :

  • Run container .
  • Mount any subdirectory in your home directory into the container .
  • Edit the files in these directories .
  • Run the network server , stay Mac The program looks like , They are in localhost Running on .

It's all through  lima nerdctl  Realized .


author :  Topic selection :  translator :  proofreading :

This paper is written by    Original compilation ,  Honor roll out

Please bring the original link to reprint ,thank
Similar articles