【问题标题】:create tables on docker start在 docker start 上创建表
【发布时间】:2018-03-30 10:48:00
【问题描述】:

我是 docker 新手,我已经创建了我的应用程序,我想发送给另一个开发人员,但我似乎无法运行它运行 docker。

它说表不存在。我已经阅读了 docker 文档,但我不明白。

没有创建表。

我做错了什么?

我的码头文件

FROM php:7.1-apache

RUN docker-php-ext-install pdo pdo_mysql

COPY ./dump.sql /docker-entrypoint-initdb.d/

我的 docker-composer

version: '2'

volumes:
    logs:
        driver: local

services:
    slim:
        build: .
        working_dir: /var/www
        command: php -S 0.0.0.0:8080 -t public
        environment:
            docker: "true"
        depends_on:
          - db-mysql
        ports:
            - 80:8080
        volumes:
            - .:/var/www
            - logs:/var/www/logs
        links:
          - db-mysql

    db-mysql:
      image: mysql
      restart: always
      container_name: db-mysql
      ports:
        - "3307:3306"
      environment:
        MYSQL_DATABASE: path
        MYSQL_ROOT_PASSWORD: root
        MYSQL_USER: root
        MYSQL_PASSWORD: root
      volumes:
        - ./mysql_init:/docker-entrypoint-initdb.d
        - ./dump.sql:/docker-entrypoint-initdb.d

我的 dump.sql 有

CREATE TABLE IF NOT EXIST `paths` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `lat` double DEFAULT NULL,
  `long` double DEFAULT NULL,
  `token` varchar(225) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;

【问题讨论】:

  • 我认为dump.sql 文件应该被复制到mysql 图像中,而不是php:7.1-apache
  • 我不明白,请你用代码告诉我
  • 我在这里写了一个答案:Dockerize MySQL with database and tables.
  • 好的,但我可以在我的 Dockerfile 中有 2 个 froms,比如 FROM php 和 FROM mysql
  • 您可以为每个服务定义不同的Dockerfiles。但这意味着您必须更改 build: . 并使用 Dockerfile 的实际路径。

标签: mysql sql docker


【解决方案1】:

您必须将入口点脚本添加到数据库中。不适用于您的应用程序(dockerfile 对我来说不清楚)。有关更多详细信息,您可以查看此answer

docker-compose.yml(基本无持久化)

version: '3.1'

services:
  mysql:
   image: mysql
   restart: always
   container_name: db-mysql
   ports:
    - 3307:3306
   environment:
     MYSQL_DATABASE: path
     MYSQL_ROOT_PASSWORD: root
     MYSQL_USER: testuser
     MYSQL_PASSWORD: testpassword
   volumes:
    - ./dump:/docker-entrypoint-initdb.d

首先检查用户。默认情况下,root 用户已经存在,因此请给 mysql_user 一个不同的名称。其次,我挂载了一个目录,而不是文件。我有一个目录dump,其中包含我的dump.sql(也很基本):

CREATE TABLE paths (
  id int(11)
)

树看起来像这样:

docker-compose.yml
dump/

dump/ 包含dump.sql

在容器启动期间,这是安装在 mysql 容器内的 dump/ 目录。

$ docker-compose up -d

docker exec -it db-mysql bash

认证(可以选择,普通用户的root_user)我用普通的:

root@a110fe08e4b6:/# mysql -u testuser -p
Enter password:

检查数据库:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| path               |
+--------------------+
2 rows in set (0.00 sec)

使用路径和检查表:

mysql> use path;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_path |
+----------------+
| paths          |
+----------------+
1 row in set (0.00 sec)

显示路径表中的列:

mysql> show columns from paths;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

【讨论】:

  • 让我试试你的解决方案
  • 错误是什么?我已经对其进行了测试(打印了输出),所以它应该可以工作。
  • 没有错误只是这个mysql> 使用路径;数据库更改 mysql> 显示表;空集(0.00 秒)
  • 您是否复制了我的 docker-compose.yml 文件?和相同的转储目录和转储.sql。从那里开始。当它工作时,您可以更新 dump.sql 等。
  • 当我运行你的代码时,我得到这个:yaml.parser.ParserError: while parsing a block mapping in "./docker-compose.yml", line 8, column 5 expected ,但在“./docker-compose.yml”第 24 行第 6 列中找到了“
猜你喜欢
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
相关资源
最近更新 更多