说明

因为mac上虚拟机都不太好用,有些还收费,故使用docker来搭建虚拟机。况且我的Mac是m1芯片,用的是arm架构,虚拟机更是少。

搭建本机与虚拟机互通

mac上docker与linux不同,mac上实际上内部是建了个linux的虚拟机来实现。mac上本机和虚拟机互通使用docker-connect

brew install wenjunxiao/brew/docker-connector


docker network ls --filter driver=bridge --format "{{.ID}}" | xargs docker network inspect --format "route {{range .IPAM.Config}}{{.Subnet}}{{end}}" >> /opt/homebrew/etc/docker-connector.conf
For the first time, you can add all the bridge networks of docker to the routing table by the following command:
  docker network ls --filter driver=bridge --format "{{.ID}}" | xargs docker network inspect --format "route {{range .IPAM.Config}}{{.Subnet}}{{end}}" >> /opt/homebrew/etc/docker-connector.conf
Or add the route of network you want to access to following config file at any time:
  /opt/homebrew/etc/docker-connector.conf
Route format is `route subnet`, such as:
  route 172.17.0.0/16
The route modification will take effect immediately without restarting the service.
You can also expose you docker container to other by follow settings in /opt/homebrew/etc/docker-connector.conf:
  expose 0.0.0.0:2512
  route 172.17.0.0/16 expose
Let the two subnets access each other through iptables:
  iptables 172.17.0.0+172.18.0.0

To start wenjunxiao/brew/docker-connector now and restart at startup:
  sudo brew services start wenjunxiao/brew/docker-connector
Or, if you don't want/need a background service you can just run:
  sudo docker-connector -config /opt/homebrew/etc/docker-connector.conf
docker run -it -d --restart always --net host --cap-add NET_ADMIN --name mac-docker-connector wenjunxiao/mac-docker-connector

搭建centos8容器开发环境

docker pull centos:8

docker network create --subnet=172.16.160.0/24 bridge_for_dev

docker run -itd --privileged=true --restart=always --name centos-dev --hostname centos-dev --net bridge_for_dev --ip 172.16.160.66 centos:8  /usr/sbin/init

以上/usr/sbin/init启动,便可以使用systemclt命令

下面进入容器,开始一系列环境的安装

sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*

sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*

yum clean all
yum makecache
echo "export LC_ALL=C.UTF8" >> /etc/profile
source /etc/profile

sudo

yum install sudo -y

openssh

yum install openssh-server -y

sudo systemctl enable sshd
systemctl start sshd

net

yum install net-tools -y
yum install -y iputils

password

yum install passwd -y

gcc

yum install gcc gcc-c++ gdb -y
yum install automake autoconf libtool make -y
yum install ncurses-devel perl -y
yum install cmake -y
yum install python3 -y

man

yum install man -y
yum install man-pages -y

注意

本来想使用docker建一个x86架构的远程调试环境,但是无论是使用vscode还是使用clion,都无法远程debug,想着估计是系统架构不同,内部又是用qume模拟器来做了一次指令的翻译,过程中可能导致一些权限的降级或者功能的缺失吧。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐