安装docker前置
注意需要先查看内核信息(需要是aarch64)
uname -m
查询当前网卡名称(以下均以网卡名eth0为例,请注意替换)
ifconfig
1.切换树莓派镜像源
sudo nano /etc/apt/sources.list
## 将镜像源替换为:
# deb https://mirrors.tuna.tsinghua.edu.cn/debian buster main contrib non-free
# deb https://mirrors.tuna.tsinghua.edu.cn/debian-security/ buster/updates main contrib non-free
# deb https://mirrors.tuna.tsinghua.edu.cn/debian buster-updates main contrib non-free
sudo nano /etc/apt/sources.list.d/raspi.list
## 将镜像替换为:
# deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui
2.更新包
sudo apt-get update
sudo apt-get upgrade
3.重启树莓派
reboot
docker安装
# 一键安装
curl -fsSL get.docker.com -o get-docker.sh && sh get-docker.sh
-
查看docker版本
docker version -
添加当前用户到docker组,以便无需sudo即可运行Docker命令
sudo usermod -aG docker ${USER}
- 测试
# hello world
docker run hello-world
# Debian的Docker容器,并为你提供一个命令行shell
docker run -it debian bash
# Debian Buster版本的Docker容器
docker run -it debian:buster bash
安装openwrt容器
创建docker网络
打开网卡混杂模式,使网卡可以接收任何流量
ip link set eth0 promisc on
上面的命令是临时性的,重启失效。如果打算把openwrt设置成开机自动启动的话,需要改成永久有效。编辑/etc/rc.local文件,#!/bin/sh 下面或在 exit 0 之前。添加一行:
# 打开网卡混杂模式
ip link set eth0 promisc on
创建docker网络,网络驱动程序为macvlan,网络命名为macnet,子网和网关根据实际情况修改。
docker network create -d macvlan --subnet=192.168.1.0/24 --gateway=192.168.1.1 -o parent=eth0 macnet
拉取openwrt镜像
docker pull unifreq/openwrt-aarch64:latest
拉取完毕之后查看一下拉取结果: docker images
创建并运行openwrt容器(创建openwrt_start.sh脚本)
#!/bin/bash
IP="192.168.1.2" #旁路由ip地址
MACADDR="02:42:C0:A8:00:00" #MAC地址,随机生成
IMG_TAG=latest
KERNEL_VERSION=$(uname -r)
docker run --name openwrt-${IMG_TAG} --restart always --network macnet --mac-address ${MACADDR} --ip ${IP} -d --privileged=true --ulimit nofile=16384:65536 -v /lib/modules/${KERNEL_VERSION}:/lib/modules/${KERNEL_VERSION} unifreq/openwrt-aarch64:$IMG_TAG
执行前给脚本赋值权限,并执行
sudo chmod +x openwrt_start.sh && sudo ./openwrt_start.sh
查看openwrt容器运行状态,如果STATUS是UP则说明运行成功
docker ps -a
进入openwrt容器
docker exec -it openwrt-latest /bin/sh
编辑配置文件
vim /etc/config/network
修改lan口内容
config interface 'lan'
option type 'bridge'
option ifname 'eth0'
option proto 'static'
option ipaddr '192.168.1.2'
option netmask '255.255.255.0'
option ip6assign '60'
option gateway '192.168.1.1'
option broadcast '192.168.1.255'
option dns '192.168.1.1'
重启网络
/etc/init.d/network restart
docker常用命令
docker -v #查看版本
#docker网络
docker network ls #查看docker的网络环境
docker network rm macnet #删除docker的网络
systemctl start docker #启动docker
systemctl stop docker #停止docker
systemctl restart docker #重启docker
systemctl status docker #查看docker状态
systemctl enable docker #开机启动
docker ps #查看正在运行容器
docker ps -a #查看所有容器
#容器操作
docker start [容器id] #启动容器
docker stop [容器id] #停止容器
docker rm [容器id] #删除容器
#文件拷贝:
#1、从docker容器中拷贝出来
docker cp tomcat:/usr/local/tomcat/webapps/ /usr/local/mysofts/tomcat/
#2、从宿主机考进docker
docker cp /usr/local/mysofts/tomcat/ tomcat:/usr/local/tomcat/webapps/
#交互式命令
#使用docker时不能直接通过路径进入到docker中
#进入命令
docker exec -it <CONTAINER_ID> /bin/bash
#查看日志
docker logs -f -t --tail=100 <CONTAINER_ID>
#退出:
exit
产考文档: