简体   繁体   English

当容器从另一个容器中启动时,docker-entrypoint-initdb.d 为空

[英]docker-entrypoint-initdb.d is empty when container is started from within another container

I have a directory structure like -我有一个目录结构,如 -

- Dockerfile
- docker-compose.yml
- db
  - schema.sql

and contents of each file is -每个文件的内容是 -

Dockerfile - Dockerfile -

FROM alpine:3.7

RUN apk update && \
    apk add --no-cache docker python3 && \
    apk add --no-cache --virtual .docker-compose-deps python3-dev libffi-dev openssl-dev gcc libc-dev make && \
    pip3 install docker-compose && \
    apk del .docker-compose-deps

ADD . .

docker-compose.yml - docker-compose.yml -

version: "2"

services:

  test_mysql:
    image: "mysql:5.7"
    container_name: test_mysql_v1
    volumes:
      - ./db:/docker-entrypoint-initdb.d/
    environment:
      - MYSQL_ROOT_PASSWORD=root
    ports:
      - "3310:3306"

schema.sql - schema.sql -

create database if not exists db2;

When I do -当我做 -

docker-compose up --build --remove-orphans

it behaves expectedly and I get a db called db2 inside test_mysql_v1 container.它的行为符合预期,我在test_mysql_v1容器中得到了一个名为db2的数据库。

However, when I do -但是,当我这样做时 -

1. docker build -t dfd_1 .

2. docker run -v /var/run/docker.sock:/var/run/docker.sock -it dfd_1 sh

from inside -

3. docker-compose up  --build --remove-orphans

Then in newly created test_mysql_v1 container, docker-entrypoint-initdb.d directory is empty (no db created).然后在新创建的test_mysql_v1容器中,docker -entrypoint-initdb.d目录为空(未创建数据库)。

Can someone please help how to make this work?有人可以帮助如何完成这项工作吗?

End result should be - when I run docker-compose up from inside dfd_1 container, it should create a mysql container with db already created .最终结果应该是 - 当我从dfd_1容器内部向上运行docker-compose 时,它应该创建一个 mysql 容器,其中db 已经创建

Additional notes -补充说明 -

  1. To bring down mysql container, I use - docker-compose down为了降低 mysql 容器,我使用 - docker-compose down

  2. My guess is when I do docker run -v /var/run/docker.sock:/var/run/docker.sock... , then I require another volume mount so that docker-compose.yml (inside dfd_1 ) can copy db to docker-entrypoint-initdb.d correctly.我的猜测是当我执行docker run -v /var/run/docker.sock:/var/run/docker.sock...时,我需要另一个卷挂载以便docker-compose.yml (在dfd_1内)可以将db复制到docker-entrypoint-initdb.d正确。

  3. Got nothing from docker logs.从 docker 日志中一无所获。

Any help is appreciated.任何帮助表示赞赏。 Thanks.谢谢。

Achieved it by passing path on host directory to the container within.通过将主机目录上的路径传递给其中的容器来实现它。

Changed docker run command as -docker 运行命令更改为 -

docker run -v /var/run/docker.sock:/var/run/docker.sock -e HOST_DIR=$PWD -it dfd_1 sh

And docker-compose.yml (used HOST_DIR env variable while mapping volume) as - docker-compose.yml (映射卷时使用HOST_DIR 环境变量)为 -

version: "2"

services:

  test_mysql:
    image: "mysql:5.7"
    container_name: test_mysql_v1
    volumes:
      - ${HOST_DIR}/db:/docker-entrypoint-initdb.d/
    environment:
      - MYSQL_ROOT_PASSWORD=root
    ports:
      - "3310:3306"

暂无
暂无

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

相关问题 无法使用 mysql docker 容器在 docker-entrypoint-initdb.d 中创建数据库 - Can't create db in docker-entrypoint-initdb.d with mysql docker container docker-compose up invalid mode /docker-entrypoint-initdb.d/ - 无法为服务数据库创建容器 - docker-compose up invalid mode /docker-entrypoint-initdb.d/ - cannot create container for service database sql转储是否被加密到docker容器docker-entrypoint-initdb.d中? - Are sql dumps moved into docker container `docker-entrypoint-initdb.d` encrypted? MySql Docker /docker-entrypoint-initdb.d 编码错误 - MySql Docker /docker-entrypoint-initdb.d wrong Encoding 自定义mysql高山映像未从作为卷安装的docker-entrypoint-initdb.d加载init.sql - Custom mysql alpine image not loading init.sql from docker-entrypoint-initdb.d mounted as a volume docker 权限被拒绝 mysql 映像中的 shell 脚本放置在 docker-entrypoint-initdb.d - docker permission denied for shell script in mysql image placed at docker-entrypoint-initdb.d 如何等待docker-entrypoint-initdb.d在Shell脚本/ Bash中加载数据库? - How to wait for docker-entrypoint-initdb.d to load DB in Shell Script/Bash? 为什么我的 docker-entrypoint-initdb.d 脚本(在 docker-compose.yml 中指定)没有被执行来初始化一个新的 MySQL 实例? - Why isn't my docker-entrypoint-initdb.d script (as specified in docker-compose.yml) executed to initialize a fresh MySQL instance? 命令&#39;/ bin / sh -c mysql -u wordpress -pwordpress wordpress &lt;/docker-entrypoint-initdb.d/wordpress.sql&#39;返回非零代码:1 - The command '/bin/sh -c mysql -u wordpress -pwordpress wordpress < /docker-entrypoint-initdb.d/wordpress.sql' returned a non-zero code: 1 docker-entrypoint-initdb 中的 MySQL 脚本不执行 - MySQL scripts in docker-entrypoint-initdb are not executed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM