catwithtudou

一直是阵雨

🌦️一枚服务端菜狗/ 大部分时间都是 golang 🫖尝试记录和热爱生活/尝试交一些新朋友 📖目前最重要的事情是打破信息壁垒&重新拾起初心和兴趣&输出者
twitter
github

Setting up Docker+Ubuntu environment on Mac to support GDB

1. Background#

Recently, when preparing to debug C language programs using gdb, I found that gdb does not currently support Mac M1. As shown in the following figure:

image

If you want to use gdb on a Mac machine, you can only use a virtual machine or container to run another system type to install and solve the problem.

I previously tried to install Docker Desktop on my local machine to solve this problem, but I found that there were always exceptions and timeouts during the running process, making it basically unusable.

Finally, I found lima to solve this problem. It has a very low cost and good compatibility, allowing Mac to easily run other systems locally.

Here, I will record the relevant usage and the process of building a docker+ubuntu environment using lima, hoping to help students who have similar confusion.

2. Lima#

2.1 Introduction#

Lima launches Linux virtual machines with automatic file sharing and port forwarding (similar to WSL2).
The original goal of Lima was to promote containerd including nerdctl (contaiNERD ctl) to Mac users, but Lima can be used for non-container applications as well.

In simple terms, it is a virtual machine running containerd, similar to WSL2 on Windows.

Containerd is essentially the same as what the Docker engine does, and it is also a graduated project of CNCF.

Containerd is an industry-standard container runtime with an emphasis on simplicity, robustness, and portability. It is available as a daemon for Linux and Windows, which can manage the complete container lifecycle of its host system: image transfer and storage, container execution and supervision, low-level storage and network attachments, etc.

2.2 Installation#

Installation on Mac is very simple:

$ brew install lima

$ lima -v
limactl version 0.20.1

After installing lima, you can do many things with it.

Currently, lima provides multiple templates, such as archlinux, docker, podman, Kubernetes, ubuntu, etc., which basically meet the requirements of all environment setups. You can check them with the following command:

$ limactl start --list-templates

almalinux-8
almalinux-9
almalinux
alpine
apptainer-rootful
apptainer
archlinux
.....

Next, I will focus on describing the process of building a docker+ubuntu environment using lima.

3. Building a docker+ubuntu environment#

3.1 Installing Docker#

Here, we create a directory to save the configuration file of LimaVM:

$ mkdir lima_vm && cd lima_vm

Here, we download and use the docker.yaml file from the official configuration template library. Let's first view its content:

$ curl -o docker.yaml https://raw.githubusercontent.com/lima-vm/lima/master/examples/docker.yaml
$ cat docker.yaml

The configuration file has detailed explanations for each configuration item. Interested students can refer to it. Here, we will use the default configuration file to start the lima VM directly:

If you want to adjust the CPU, memory, and mounted directories allocated to the virtual machine, the configuration file provides corresponding parameters.

$ limactl start ./docker.yaml

image

Please note:

  • This command is usually executed during initialization and does not need to be repeated after successful creation.
  • The above file is only the initial startup configuration. After startup, the configuration file will be automatically generated in the following path:
    • If you need to modify the configuration later, you need to edit the generated configuration file below and restart to take effect.
~/.lima/docker/lima.yaml

3.2 Specific usage#

# View the current running list, including the assigned name, SSH, Status, CPU, Memory, etc.
$ limactl list
# Enter the shell
$ limactl shell docker
# Execute the shell command "docker ps" directly
$ limactl shell docker docker ps
# Stop the VM
$ limactl stop docker
# Delete the VM
$ limactl delete docker

As you can see from the above examples, when executing the docker command, either enter the terminal and then operate, or add the prefix "limactl shell docker".

If you want to execute the docker CLI directly as if you were on your local machine, you need to:

# 1. Install docker CLI locally
$ brew install docker
# 2. Set the docker environment variables
# Name is the name given when lima is started, such as "docker" in the previous example, which is "lima-docker"
# Dir is the path automatically generated after lima is started, such as "~/.lima/docker/sock/docker.sock" in the previous example
$ docker context create lima-{{.Name}} --docker "host=unix://{{.Dir}}/sock/docker.sock"
$ docker context use lima-{{.Name}}
# 3. Execute docker commands directly in the local terminal like docker CLI
$ docker run hello-world

image

3.3 Adding an Ubuntu container#

Currently, there are ready-to-use docker images available. Here, we will install the Ubuntu 20.04 version:

$ docker pull ubuntu:20.04
$ docker images 

image

After installing the image, we can directly create an Ubuntu container:

# Initialize the container
$ docker run --name ubuntu-container -it ubuntu:20.04 bash

Now you can use the Ubuntu operating system to complete your requirements. Here are some commonly used operations that you may need:

# View all current containers and their status, such as the previously run ubuntu-container
$ docker ps -a 
# If the container is not in a running state, you need to start it
$ docker start ubuntu-container
# Enter the terminal of the container
$ docker exec -it ubuntu-container bash

image

3.4 Installing gdb#

After entering the Ubuntu container, you can install gdb to debug C language programs:

# Update the package after entering the container
$ apt update
# Install gdb
$ apt install gdb

References#

https://zhuanlan.zhihu.com/p/476240258

https://blog.crazyfirelee.tw/posts/sharing/lima/

https://github.com/lima-vm/lima/blob/master/examples/docker.yaml

https://stackoverflow.com/questions/67310123/how-to-install-gdb-on-mac-m1-apple-silicon

https://zhuanlan.zhihu.com/p/354794701

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.