Install an sql dump file to a docker container wit

2019-07-11 03:11发布

问题:

I am just learning the basics of docker, but have come stuck on importing an SQl file from the local system. I am on windows 10 and have allowed my docker containers to access my shared drives. I have an SQL file located on D i would like to import to the base image of Maria DB i got from docker hub.

I have found a command to install that sql file on my image and tried to directly import the image from inside the container sql command prompt, but get a failed to open file error.

Below are the two methods i have tried, but where do i store my sql dump and how do i import it?

Method 1 tried via mysql command line

winpty docker run -it --link *some-mariadb*:mysql --rm mariadb sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'

Then

use *database-created* // previously created 

Then

source d:/sql_dump_file.sql

Method 2

docker exec -i my-new-database mysql -uroot -pnew-db-password --force < d:/sql_dump_file.sql

Update 5/12/2016

So after a disappointing weekend of playing around. I currently change the drive to C: drive as there seemed to be some unknown issue i can't debug with getting D: drive to work.

Then i found the inspect command to see what volumes are mounted to a container. I have the following, but i can't import to SQL as it says file does not exist, but it clearly says in docker that it is there and mounted, the SQL file is inside the map_me folder. I created a folder called map_me in the main directory of C:

docker inspect dbcontainer

       "Mounts": 
           [
            {
                "Source": "/map_me",
                "Destination": "/docker-entrypoint-initdb.d/sl_dump_fil‌​e.sql",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
           ]

回答1:

I recommend the following:

Mount your sql_dump_file.sql at /docker-entrypoint-initdb.d/ when creating the container. The official MariaDB image will restore it at startup.

docker run -d --name <containername> -v d:/sql_dump_file.sql:/docker-entrypoint-initdb.d/sl_dump_file.sql <environment_variables> <imagename> <startup commands>


回答2:

So after some tweaking and better understanding, i came to the conclusion after testing, that docker-compose is the way to go. So the first folder contains a my.cnf file that does the configuration, then the other folder which @Farhad identified is used to intilize the .sql file.

version: "2"

services:

    mariadb_a:
        image: mariadb:latest
        ports: 
         - "3306:3306"
        environment:
         - MYSQL_ROOT_PASSWORD=111111
        volumes:
         - c:/some_folder:/etc/mysql/conf.d
         - c:/some_other_folder:/docker-entrypoint-initdb.d