创建、填充和使用 Docker 个卷
Creating, populating, and using Docker Volumes
过去几天我一直在使用 Docker,希望尽快将我的 Python-MySQL webapp 移至 Docker。
推论是我需要使用 Docker 卷并且最近被难住了。我可以直接通过
创建一个卷
$ docker volume create my-vol
或通过在 docker 运行 调用中引用不存在的卷来间接引用,但我不知道如何用我的 .sql 数据库文件填充这些卷,而不复制通过 Docker 文件中的 COPY 调用进行归档。
我试过直接在包含 .sql 文件的目录中创建卷(上面提到的第一种方法)并在我的 [=24= 中安装包含 .sql 文件的目录] 调用,它确实将 .sql 文件移动到容器中(我通过在容器内导航 bash shell 看到了它)但是当 运行ning 一个 mariadb连接到包含数据库的 mariadb 容器的容器(如 mariadb docker 自述文件中所建议),它只有标准数据库(information_schema、mysql、performance_schema)
如何创建包含我预先存在的 .sql 数据库的卷?
在 docker 容器中使用 mariadb 时,图像支持 运行ning .sql
文件作为容器首次启动的一部分。这允许您在数据可访问之前将其推送到数据库中。
Initializing a fresh instance
When a container is started for thefirst time, a new database with the specified name will be created and
initialized with the provided configuration variables. Furthermore, it
will execute files with extensions .sh
, .sql
and .sql.gz
that are
found in /docker-entrypoint-initdb.d
. Files will be executed in
alphabetical order. You can easily populate your mariadb services by
mounting a SQL dump into that directory and provide custom images with
contributed data. SQL files will be imported by default to the
database specified by the MYSQL_DATABASE
variable.
这意味着如果你想向容器中注入数据,当它第一次启动时。在您的 Dockerfile
、COPY
中将 .sql
文件放入路径 /docker-entrypoint-initdb.d/myscript.sql
的容器中 - 它将在您在环境变量 [=19] 中指定的数据库上调用=].
像这样:
FROM mariadb
COPY ./myscript.sql /docker-entrypoint-initdb.d/myscript.sql
然后:
docker run -e MYSQL_DATABASE=mydb mariadb
接下来的问题是您希望如何管理数据库存储。你在这里基本上有两个选择:
- 创建一个卷绑定到主机,mariadb 存储数据库。这将使您能够从主机轻松访问数据库存储文件。
docker的例子 运行:
docker run -v /my/own/datadir:/var/lib/mysql mariadb
- 创建一个docker 卷并将其绑定到容器中的存储位置。这将是一个由 docker 管理的卷。此卷将在容器重新启动之间保留数据。
docker volume create my_mariadb_volume
docker run -v my_mariadb_volume:/var/lib/mysql mariadb
docs for the mariadb docker 图片中也包含了。如果您要使用这张图片,我建议您从上到下阅读它。
过去几天我一直在使用 Docker,希望尽快将我的 Python-MySQL webapp 移至 Docker。
推论是我需要使用 Docker 卷并且最近被难住了。我可以直接通过
创建一个卷$ docker volume create my-vol
或通过在 docker 运行 调用中引用不存在的卷来间接引用,但我不知道如何用我的 .sql 数据库文件填充这些卷,而不复制通过 Docker 文件中的 COPY 调用进行归档。
我试过直接在包含 .sql 文件的目录中创建卷(上面提到的第一种方法)并在我的 [=24= 中安装包含 .sql 文件的目录] 调用,它确实将 .sql 文件移动到容器中(我通过在容器内导航 bash shell 看到了它)但是当 运行ning 一个 mariadb连接到包含数据库的 mariadb 容器的容器(如 mariadb docker 自述文件中所建议),它只有标准数据库(information_schema、mysql、performance_schema)
如何创建包含我预先存在的 .sql 数据库的卷?
在 docker 容器中使用 mariadb 时,图像支持 运行ning .sql
文件作为容器首次启动的一部分。这允许您在数据可访问之前将其推送到数据库中。
Initializing a fresh instance
When a container is started for thefirst time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions
.sh
,.sql
and.sql.gz
that are found in/docker-entrypoint-initdb.d
. Files will be executed in alphabetical order. You can easily populate your mariadb services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by theMYSQL_DATABASE
variable.
这意味着如果你想向容器中注入数据,当它第一次启动时。在您的 Dockerfile
、COPY
中将 .sql
文件放入路径 /docker-entrypoint-initdb.d/myscript.sql
的容器中 - 它将在您在环境变量 [=19] 中指定的数据库上调用=].
像这样:
FROM mariadb
COPY ./myscript.sql /docker-entrypoint-initdb.d/myscript.sql
然后:
docker run -e MYSQL_DATABASE=mydb mariadb
接下来的问题是您希望如何管理数据库存储。你在这里基本上有两个选择:
- 创建一个卷绑定到主机,mariadb 存储数据库。这将使您能够从主机轻松访问数据库存储文件。
docker的例子 运行:
docker run -v /my/own/datadir:/var/lib/mysql mariadb
- 创建一个docker 卷并将其绑定到容器中的存储位置。这将是一个由 docker 管理的卷。此卷将在容器重新启动之间保留数据。
docker volume create my_mariadb_volume
docker run -v my_mariadb_volume:/var/lib/mysql mariadb
docs for the mariadb docker 图片中也包含了。如果您要使用这张图片,我建议您从上到下阅读它。