Docker
跳至導覽
跳至搜尋
Docker是一套提供容器(英語:Container)的軟體。
安裝
可以用這個一行指令直接安裝:
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).