简体   繁体   English

如何使用 docker compose 初始化包含容器中某些表的数据库?

[英]How can I initialize a database with some tables in a container using docker compose?

I have my db service:我有我的数据库服务:

...
mysql-server:
    container_name: silos-database
    init: true
    restart: always
    build:
      context: .
      dockerfile: Dockerfile-db
    environment:
      - MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD}
      - MYSQL_DATABASE=${DATABASE}
      - MYSQL_USER=${USER}
      - MYSQL_PASSWORD=${USER_PASSWORD}
    ports:
      - target: 3036
        published: 3036
    networks:
      - backend-net
    volumes:
      - type: volume
        source: db_vol
        target: /var/lib/mysql
...

This is my Dockerfile这是我的 Dockerfile

FROM mysql:latest

The init.sql file contain same tables that my database must have init.sql文件包含我的数据库必须具有的相同表

...
-- users data
CREATE TABLE IF NOT EXISTS users (
  email VARCHAR(128) PRIMARY KEY,
  username VARCHAR(64) UNIQUE,
  -- password is SHA256 hashed, so 64 characters
  password CHAR(64) NOT NULL
);

CREATE TABLE IF NOT EXISTS sessions (
  email VARCHAR(128) PRIMARY KEY,
  token TEXT NOT NULL,
  expireDate BIGINT(13) NOT NULL,
  FOREIGN KEY (email) REFERENCES users(email) ON UPDATE CASCADE ON DELETE CASCADE
);
...

How can I build a service with the content of init.sql file already inside the service?如何使用服务中已有的init.sql文件的内容构建服务?

The simplest way would be to add init.sql file to Your docker image:最简单的方法是将init.sql文件添加到您的 docker 图像中:

FROM mysql:latest
ADD init.sql /docker-entrypoint-initdb.d

If You want to provide init.sql during container start instead of build time, You can mount directory containing init.sql into container:如果您想在容器启动而不是构建时提供init.sql ,您可以将包含init.sql的目录挂载到容器中:

mysql-server:
    container_name: silos-database
    init: true
    restart: always
    build:
      context: .
      dockerfile: Dockerfile-db
    environment:
      - MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD}
      - MYSQL_DATABASE=${DATABASE}
      - MYSQL_USER=${USER}
      - MYSQL_PASSWORD=${USER_PASSWORD}
    ports:
      - target: 3036
        published: 3036
    networks:
      - backend-net
    volumes:
      - type: volume
        source: db_vol
        target: /var/lib/mysql
      - /local/path/with/initsql:/docker-entrypoint-initdb.d

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Docker 容器中使用架构初始化 MySQL 数据库? - How can I initialize a MySQL database with schema in a Docker container? 如何使用JDBC在数据库的某些表中搜索字符串? - How can I search a string in some of the tables in a database using JDBC? 如何连接我用 docker compose 创建的数据库实例? - How can I connect the database instance I created with docker compose? 我可以在 ZB76E98AF9AAA680979BF5A65B2 上使用 docker 组合 mysql + spring 引导容器吗? - Can I use docker compose mysql + spring boot container on kubernetes? Docker MySQL - cant connect SpringBoot app to MySQL database in docker container using docker-compose - Docker MySQL - cant connect SpringBoot app to MySQL database in docker container using docker-compose 如何配置我的 Docker Compose,以便我的数据库附带已经创建的表? - How can I configure my Docker Compose so my DB does ship with the tables already created? 使用docker-compose,数据库容器无法启动并运行 - With docker-compose, database container is not up and running Docker 在终端上运行带有表的 MysQL 容器,但不带有 docker-compose.yml - Docker Runs MysQL Container With Tables On Terminal, But Not With docker-compose.yml 如何将一个容器中的mysql数据库绑定到运行django博客应用程序的另一个容器(使用docker-compose) - How to tie mysql database in one container to another container running my django blog app (with docker-compose) 如何保留我的数据库,但更改 docker-compose 中的代码? - How can I keep my database, but change the code in docker-compose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM