Docker Run command with MySQL import

236 Views Asked by At

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

2

There are 2 best solutions below

0
On BEST ANSWER

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

1
On

On Linux, < > means input output from resource and Linux shell breaks command to 2 parts one as input|output and other to which stream need to be passed.

Now with docker command too, these are two command docker run -it CONTAINER mysql -h localhost -u user-ppassword dbName < /tmp/21336/sql/data/import_file.sql. Moreover, /tmp/21336/sql/data/import_file.sql file is being looked up in host machine.