问题描述
我正在尝试使用 MySQL 数据库创建一个容器并向这些数据库添加架构.
我当前的 Dockerfile 是:
为了创建容器,我遵循 Docker 上提供的文档并执行此命令:
但是当我执行这个命令时,Container并没有被创建,在Container状态下可以看到CMD没有执行成功,实际上只执行了mysql
命令.>
无论如何,有没有办法用架构初始化数据库,或者我需要手动执行这些操作?
对于这个超长的答案,我很抱歉,但是,您还有一段路要走才能到达您想要的地方.我会说通常你不会将数据库的存储放在与数据库本身相同的容器中,你要么挂载一个主机卷,以便数据保留在 docker 主机上,或者,也许可以使用一个容器来保存数据(/var/lib/mysql).另外,我是 mysql 的新手,所以,这可能不是非常有效.话说……
我认为这里可能存在一些问题.Dockerfile 用于创建映像.您需要执行构建步骤.至少,从包含 Dockerfile 的目录中,您可以执行以下操作:
Dockerfile 描述了要创建的镜像.我对 mysql 不太了解(我是 postgres 粉丝),但是,我在互联网上搜索了如何初始化 mysql docker 容器".首先我创建了一个新目录来工作,我称之为 mdir,然后我创建了一个文件目录,我存放了一个 epcis_schema.sql 文件,该文件创建了一个数据库和一个表:
然后我在文件目录中创建了一个名为 init_db 的脚本:
(此脚本的大部分内容来自此处:https://gist.github.com/pda/9697520)
这是我创建的 files/run_db 脚本:
最后,Dockerfile 将它们全部绑定:
所以,我转到了我的 mdir 目录(其中包含 Dockerfile 和文件目录).然后我运行命令:
你应该看到这样的输出:
您现在拥有一张图片7f6d5215fe8d".我可以运行这个图像:
图像开始,我看到一个实例字符串:
然后我可以停止"它,然后重新启动它.
如果您查看日志,第一行将包含:
然后,在日志的末尾:
这些是来自/tmp/run_db 脚本的消息,第一个表示数据库是从保存的(初始)版本解压出来的,第二个表示数据库已经存在,所以使用了现有的副本.
这是我上面描述的目录结构的 ls -lR.请注意,init_db 和 run_db 是设置了执行位的脚本:
I am trying to create a container with a MySQL database and add a schema to these database.
My current Dockerfile is:
In order to create the container I am following the documentation provided on Docker and executing this command:
But when I execute this command the Container is not created and in the Container status it is possible to see that the CMD was not executed successfully, in fact only the mysql
command is executed.
Anyway, is there a way to initialize the database with the schema or do I need to perform these operations manually?
I am sorry for this super long answer, but, you have a little way to go to get where you want. I will say that normally you wouldn't put the storage for the database in the same container as the database itself, you would either mount a host volume so that the data persists on the docker host, or, perhaps a container could be used to hold the data (/var/lib/mysql). Also, I am new to mysql, so, this might not be super efficient. That said...
I think there may be a few issues here. The Dockerfile is used to create an image. You need to execute the build step. At a minimum, from the directory that contains the Dockerfile you would do something like :
The Dockerfile describes the image to create. I don't know much about mysql (I am a postgres fanboy), but, I did a search around the interwebs for 'how do i initialize a mysql docker container'. First I created a new directory to work in, I called it mdir, then I created a files directory which I deposited a epcis_schema.sql file which creates a database and a single table:
Then I created a script called init_db in the files directory:
(most of this script was lifted from here: https://gist.github.com/pda/9697520)
Here is the files/run_db script I created:
Finally, the Dockerfile to bind them all:
So, I cd'ed to my mdir directory (which has the Dockerfile along with the files directory). I then run the command:
You should see output like this:
You now have an image '7f6d5215fe8d'. I could run this image:
and the image starts, I see an instance string:
I could then 'stop' it, and restart it.
If you look at the logs, the first line will contain:
Then, at the end of the logs:
These are the messages from the /tmp/run_db script, the first one indicates that the database was unpacked from the saved (initial) version, the second one indicates that the database was already there, so the existing copy was used.
Here is a ls -lR of the directory structure I describe above. Note that the init_db and run_db are scripts with the execute bit set:
这篇关于如何在 Docker 容器中使用架构初始化 MySQL 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!