mysqlnode.jsdockerdockerode

Docker Run command with MySQL import


I'm using dockerode for creating and running containers. I'm trying to import data to MySQL. my code:

docker.run('img_cwd',['mysql',
        '-h', 'localhost',
        '-u', 'user',
        `-ppassword`,
        'dbName',
        '<',
        `/tmp/21336/sql/data/import_file.sql"`],
    [process.stdout, process.stderr],
    {
        Tty: false,
        name: nameGenerator.getContainerName(),
        AttachStdin: true,
        OpenStdin: true,
        StdinOnce: true,
        HostConfig: {
            Binds: ['/C/tmp:/tmp']
        }
    })

under C:/ tmp I have to folder "21336"

after running my code I see that the import doesn't happen and the docker ps command show this:

15d405443805af1a2635e7bc456311de741352ff4ae6b1efb3d43cb10a73d62c   img_cwd             "mysql -h localhost -u user-ppassword dbName < /tmp/21336/sql/data/import_file.sql"   9 seconds ago       Exited (1) 8 seconds ago                       Import_b01k8qr29jh868vf0959cy_Data

when I start the container with '-it' flag and run the import command:

mysql -h localhost -u user-ppassword dbName < /tmp/21336/sql/data/import_file.sql

it works perfectly.

the image I use:

   FROM alpine:3.9 
   RUN apk update && \
        apk add --no-cache mysql-client
   CMD ["/bin/sh"]

the logs for the failed container run shows it does not recognize the command :enter image description here

enter image description here enter image description here


Solution

  • I solved it by creating a script

    #!/bin/sh
    
    mysql -h ${1} -u ${2} -p${3} ${4} < ${5}
    

    when I run the command:

    docker run imageName myScript HostParam UserPram PasswordParam DBName FileName 
    

    it works