简体   繁体   中英

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 -

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 -

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 -

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.

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).

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 .

Additional notes -

  1. To bring down mysql container, I use - 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.

  3. Got nothing from docker logs.

Any help is appreciated. Thanks.

Achieved it by passing path on host directory to the container within.

Changed docker run command as -

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 -

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM