如何更改 "docker create volume" 命令的默认位置?

How to change the default location for "docker create volume" command?

当通过卷 API 创建卷时,即容器卷模式现在不再是最佳实践:

# docker volume inspect test-data
[
    {
        "Name": "test-data",
        "Driver": "local",
        "Mountpoint": "/var/lib/docker/volumes/test-data/_data"
    }
]

例如,我想在 /data 中存在 docker 个卷(安装在不同的物理卷中)。

这不可能用符号链接来做,可以用绑定挂载来做,但我想知道 Docker 中是否有一些配置来更改每个单独卷的默认位置.

2017 年:17.05.0-ce (2017-05-04), the PR 28696 deprecates --graph flag in favor or --data-root: commit 1ecaed0

The name "graph" is a legacy term from long ago when there used to be a directory at the default location /var/lib/docker/graph.

However, the flag would indicate the path of the parent directory of the "graph" directory which contains not only image data but also data for volumes, containers, and networks.
In the most recent version of docker, this directory also contains swarm cluster state and node certificates.

使用 issue 5922 and PR 5978,文档已更新。

Example:

ExecStart=/usr/bin/dockerd -H fd:// --data-root=/mnt/ssd/lib/docker

2016(现已弃用)

我只知道 docker 选项可以更改 /var/lib/docker 本身,而不是其子文件夹("graph" used by a docker daemon storage driver 的一部分)

参见docker daemon "Miscellaneous options"

Docker supports softlinks for the Docker data directory (/var/lib/docker) and for /var/lib/docker/tmp.
The DOCKER_TMPDIR and the data directory can be set like this:

DOCKER_TMPDIR=/mnt/disk2/tmp /usr/local/bin/docker daemon -D -g /var/lib/docker -H unix:// > /var/lib/docker-machine/docker.log 2>&1
# or
export DOCKER_TMPDIR=/mnt/disk2/tmp
/usr/local/bin/docker daemon -D -g /var/lib/docker -H unix:// > /var/lib/docker-machine/docker.log

如“Where are docker images stored on the host machine?”中所述(这也适用于 containers/volumes):

The contents of the /var/lib/docker directory vary depending on the driver Docker is using for storage.

您可以更改 Docker 存储其文件(包括卷)的位置,方法是更改​​其中一个名为 --data-rootstartup parameters

如果您使用 systemd 进行服务管理,该文件通常位于 /lib/systemd/system/docker.service。像这样编辑文件:

# Old - taken from the generated docker.service file in Ubuntu 16.04's docker.io package
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS

# New
ExecStart=/usr/bin/dockerd --data-root /new_location/ -H fd:// $DOCKER_OPTS

或者,您可以编辑 Docker 守护程序配置文件,默认为 /etc/docker/daemon.json

重新启动 Docker 守护程序,您的卷将在 /new_location/volumes/{volume_name}/_data

注意:在生产和本地都要小心!您还必须将现有数据从 /var/lib/docker/ 移动到新位置,以便您的 docker 安装能够按预期工作。

如果您希望特定文件夹位于特定位置,您可以使用新位置的符号链接。

我通过将 /var/lib/docker 的内容移动到一个新位置,然后放置一个指向新位置的符号链接,成功地移动了 docker 的存储位置(我从这里 https://askubuntu.com/questions/631450/change-data-directory-of-docker):

Caution - These steps depend on your current /var/lib/docker being an actual directory (not a symlink to another location).

1) Stop docker: service docker stop. Verify no docker process is running: ps aux | grep -i [d]ocker

2) Double check docker really isn't running. Take a look at the current docker directory: ls /var/lib/docker/

2b) Make a backup - tar -zcC /var/lib docker > /mnt/pd0/var_lib_docker-backup-$(date +%s).tar.gz

3) Move the /var/lib/docker directory to your new partition: mv /var/lib/docker /mnt/pd0/docker

4) Make a symlink: ln -s /mnt/pd0/docker /var/lib/docker

5) Take a peek at the directory structure to make sure it looks like it did before the mv: ls /var/lib/docker/ (note the trailing slash)

6) Start docker back up service docker start

7) restart your containers (resolve the symlink)

在 Ubuntu 18.04.1 LTS 的 Azure VM 上为我工作 Docker 18.09.2

如果您使用的是 Fedora(在 32 上测试),只需更改或添加 --data-root 标志以及您想要的路径到 /etc/sysconfig/docker 上的 OPTIONS 变量,这就是环境systemd 用来启动 dockerd 服务的文件。