简体   繁体   中英

Docker Runs MysQL Container With Tables On Terminal, But Not With docker-compose.yml

So I'm trying to run a Mysql docker container with some tables setup. It looks like when I do it on the terminal with the following commands, I'm able to access a mysql terminal and access created tables. The Dockerfile , with initial.sql , the file that creates the tables, in the same folder:

FROM mysql:5.7.30

ENV MYSQL_DATABASE football_simulation
ENV MYSQL_ROOT_PASSWORD password
ENV MYSQL_USER alee
ENV MYSQL_PASSWORD anotherpassword
ADD initial.sql /docker-entrypoint-initdb.d

EXPOSE 3306

Docker commands:

docker build -t test/SNAPSHOT .
docker run --name footysimdb -p3308:3306 -d test/SNAPSHOT
docker exec -it footysimdb /bin/bash

Login to mysql terminal and access tables:

mysql -ualee -panotherpassword
use football_simulation;
show tables;

This is fine. I'm trying to simplify this by using docker-compose.yml now. However, running docker-compose up on this docker-compose.yml in the same directory does not work:

version: '3'

services:

        mysql-development:
                image: mysql:5.7.30
                environment:
                        MYSQL_ROOT_PASSWORD: password
                        MYSQL_DATABASE: football_simulation
                        MYSQL_USER: alee
                        MYSQL_PASSWORD: anotherpassword
                ports:
                - "3308:3306"
                volumes:
                        - initial.sql  /docker-entrypoint-initdb.d/

        admin:
                image: adminer
                ports:
                - "8080:8080"

The database football_simulation is accessible on port 8080 but the tables do not show up. What am I doing wrong?

You are doing the mounting incorrect. First you do not use : delimeter. Also the way you are trying to do it looks like initial.sql will be interpreted as named volume. If you want to mount single file to a directory docker-entrypoint-initdb.d in the container - you should use bind mounts. It would look like:

./initial.sql:/docker-entrypoint-initdb.d/

so initial.sql file from the current context will be mounted through bind mount into /docker-entrypoint-initdb.d directory as initial.sql file.

You can read about different types of volumes in offical docs .

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