aboutsummaryrefslogtreecommitdiff
path: root/docs/compatibility/inter-distros/distrobox.md
blob: 28f37aab7598560c7e6e1f9456ba488c16520154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Distrobox

[Distrobox](https://distrobox.it/)是用于管理镜像的工具,它整合了`podman`、`docker`与`lilipod`,通过虚拟化技术,使得你能够在容器中无缝运行其他任何Linux发行版,并且能够运行不同发行版所适配的应用。

## 安装

- Debian: `sudo apt install distrobox`
- Fedora: `sudo dnf install distrobox`
- Arch: `sudo pacman -S distrobox`

## 使用

### 换源

在使用之前,我们强烈推荐您为Podman换源。

```bash
sudo nano /etc/containers/registries.conf
```

在文件最下方追加以下内容(来自[Vanilla-Flavors](https://github.com/Vanilla-Flavors/china-image/blob/dev/includes.container/etc/containers/registries.conf.d/mirrors-china.conf))。

```
# Docker Hub
[[registry]]
prefix = "docker.io"
location = "docker.io"

[[registry.mirror]]
location = "dockerproxy.net"

[[registry.mirror]]
location = "hub1.nat.tf"

[[registry.mirror]]
location = "dockerproxy.cool"

[[registry.mirror]]
location = "docker.kejilion.pro"

[[registry.mirror]]
location = "docker.1ms.run"


# GitHub Container Registry
[[registry]]
prefix = "ghcr.io"
location = "ghcr.io"

[[registry.mirror]]
location = "ghcr.nju.edu.cn"

[[registry.mirror]]
location = "ghcr.dockerproxy.net"

[[registry.mirror]]
location = "ghcr.1ms.run"
```

之后,无论是拉取来自`docker.io`还是`ghcr.io`的镜像,均不需要换源,直接输入原地址即可。

### 创建并进入镜像

```bash
distrobox create --image 镜像地址 --name 名称
```

不过,我们不推荐您直接运行该命令来创建容器。在初次运行容器时,Distrobox会自动为其更新包并安装相应组件,但由于中国大陆网络环境,您有可能会卡在`Installing basic packages...`这一步。因此,我们需要在容器创建时通过`--pre-init-hooks`参数提前换源。

```bash
distrobox create --image 镜像地址 --name 名称 \
--pre-init-hooks "换源指令"

# 例如:
# distrobox create --name debian-box --image debian:latest \
# --pre-init-hooks "sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources && apt update"
```

???+ info "常见发行版镜像地址"
    - Debian: `debian:latest`
    - Ubuntu: `ubuntu:latest`
    - Fedora: `fedora:latest`
    - Arch Linux: `archlinux:latest`

安装好镜像后,您可以通过如下命令进入镜像:

```bash
distrobox enter 名称
```

或者,您也可以通过[Ptyxis](https://flathub.org/zh-Hans/apps/app.devsuite.Ptyxis)或GNOME 终端左上角加号来快捷进入。

???+ question "我是不是卡死了?"

    在初次运行容器时,您有可能会卡在`Installing basic packages...`这一步。
    
    这时,您可以通过Podman的日志来查看容器的运行状况。

    ```bash
    podman logs 名称
    ```

    大多数时候,容器是在正常安装软件包,稍等片刻即可。

    但是,如果日志中的最后一行是`+ /usr/bin/distrobox-host-exec -Y test`,您极有可能需要手动提前安装`distrobox-host-exec`的依赖`host-spawn`,并且在宿主机中安装`flatpak`。

    如果您已经创建了镜像,请您首先移除。

    ```bash
    podman rm 名称
    ```

    ??? question "为什么不能直接打断再重新运行?"
        Distrobox在初次运行镜像时会进行一系列的配置(如用户、挂载等),而`distrobox-host-exec`的配置仅仅是其中间一行。如果贸然打断,下次打开容器可能会发生意想不到的错误,如`ioctl`异常等等。

    请在上文的`--pre-init-hooks`中加入如下指令(已使用转义字符替换):

    ```bash    
    ARCH=\$(uname -m) && \
    VERSION=\$(curl -sL https://api.github.com/repos/1player/host-spawn/releases/latest | grep -oP '\"tag_name\": \"\K[^\"]+') && \
    curl -sL \"https://github.com/1player/host-spawn/releases/download/\${VERSION}/host-spawn-\${ARCH}\" -o /tmp/host-spawn && \
    sudo install -m 0755 /tmp/host-spawn /usr/bin/host-spawn

    # 例如:
    # distrobox create --name debian-box --image debian:latest \
    # --pre-init-hooks "sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources && apt update && \
    # ARCH=\$(uname -m) && \
    # VERSION=\$(curl -sL https://api.github.com/repos/1player/host-spawn/releases/latest | grep -oP '\"tag_name\": \"\K[^\"]+') && \
    # curl -sL \"https://github.com/1player/host-spawn/releases/download/\${VERSION}/host-spawn-\${ARCH}\" -o /tmp/host-spawn && \
    # sudo install -m 0755 /tmp/host-spawn /usr/bin/host-spawn"
    ```

### 常用命令

- 启动容器(不进入):`distrobox start 名称`
- 删除容器:`distrobox rm 名称`
- 查看运行日志:`podman logs 名称`
- 删除已有镜像:`podman image rm 镜像地址`