Docker
跳转到导航
跳转到搜索
Docker是一套提供容器(英语:Container)的软件。
安装
Debian的testing可以直接安装系统的Docker:
sudo apt install -y docker.io; sudo apt clean
Ubuntu上可以用这个一行指令直接安装:
sudo mkdir /etc/apt/keyrings; curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg; echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list; sudo apt update; sudo apt install -y docker-ce; sudo apt clean
另外arm64的平台可以这样安装:
sudo mkdir /etc/apt/keyrings; curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg; echo "deb [arch=arm64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list; sudo apt update; sudo apt install -y docker-ce; sudo apt clean
如果是Raspberry Pi的话(32-bit OS),可以这样安装:
sudo mkdir /etc/apt/keyrings; curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg; echo "deb [signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/raspbian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list; sudo apt update; sudo apt install -y docker-ce; sudo apt clean
docker compose(docker-compose)
另外可以加装Docker Compose套件,这个套件有两个版本,一个是Plugin版本,另外一个是独立的档案:
sudo apt install -y docker-compose docker-compose-plugin; sudo apt clean
这两个用法都可以用,但要注意版本有可能不一样造成差异:
docker compose
docker-compose
docker-machine
目前已经deprecated[1]。没有套件可以装,直接安装binary[2]:
base=https://github.com/docker/machine/releases/download/v0.16.2 && curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine && sudo mv /tmp/docker-machine /usr/local/bin/docker-machine && chmod +x /usr/local/bin/docker-machine
范例
启动一个简单的Ubuntu环境:
docker run -i -t --rm ubuntu:latest /bin/bash
或是特定版本:
docker run -i -t --rm ubuntu:jammy /bin/bash
然后把APT的服务器换掉:
perl -pi -e 's/archive.ubuntu.com/tw.archive.ubuntu.com/g' /etc/apt/sources.list
apt update -y
装些简单的东西:
apt install -y curl vim-nox wget
如果要让container内可以存取到host主机的内容,有两类方法,一种是直接在网络界面层打通--network=host
,但这个方法只能在Linux上使用:
docker run -i -t --rm --network=host ubuntu:latest /bin/bash
另外一种是透过hostname的方法打通,内部的机器可以使用host.docker.internal
这个名称存取。但要注意因为跨了界面,不会算在lo
流量(lo
的流量通常都会pass all),需要在iptables或是UFW设定对应的规则:
docker run -i -t --rm -add-host host.docker.internal:host-gateway ubuntu:latest /bin/bash
常用指令
docker system prune
参考文献
- ↑ Docker Machine is now in maintenance mode #4537. [2022-07-13] (English).
- ↑ Install Docker Machine | Docker Documentation. [2021-07-21] (English).