I tried to run the following command in Jenkinsfile
sh "docker exec -i ${d.id} sh -c \\\'exec mysql -uroot -p\\\"$MYSQL_ROOT_PASSWORD\\\"\\\' < \$(pwd)/mydb.sql"
what I get from the build console output is
+ docker exec -i b432d812cfcd026dd4e95cbc6df3f995d3a8f6fbd54c84d83fc5cea0f4a2c213 sh -c 'exec mysql -uroot -p"MakePasswdGreatAgain"'
and I got the following error
mysql: 1: mysql: Syntax error: Unterminated quoted string
where do I go wrong? why the line after < is terminated?
Related
I have a shell script that copies a .sql script to a docker container and executes it.
When I execute the line:
docker exec -it $existingMySQLContainer bash -c '"mysql -u root -proot < setup_db.sql"'
on my host machine outside of the script, it works. But when I execute it from within the script, I get:
bash: mysql -u root -proot < setup_db.sql: command not found
Why doesn't this work within the script?
Script:
#!/bin/sh
# check to see if the container with the name exists.
existingMySQLContainer=$(docker ps -a -q -f name="local-test-mysql-db")
if [ ! -z "$existingMySQLContainer" ]
then
# conatiner has been found so tear it down for a clean database
echo "Found existing local test db " $existingMySQLContainer
docker rm -f $existingMySQLContainer
fi
docker run --name local-test-mysql-db -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=root mysql:8
# run the docker container
existingMySQLContainer=$(docker ps -a -q -f name="local-test-mysql-db")
docker cp setup_db.sql $existingMySQLContainer:/setup_db.sql
docker exec -it $existingMySQLContainer bash -c '"mysql -u root -proot < setup_db.sql"'
I tried arranging the double quotes in various ways and no success.
Removing the doubles quotes to give just
Removing doubles quotes so the last line is:
ocker exec -it $existingMySQLContainer bash -c '"mysql -u root -proot < setup_db.sql"'
gives:
bash: mysql -u root -proot < setup_db.sql: command not found
SOLUTION
Docker MySQL just hadn't finished loading yet. I was trying to access it before it had finished loading.
This is the stage in Jenkinsfile where the problem comes from :
stage ('Build & Run container') {
imageMysql = docker.build('backend-server-mysql-dev', '--no-cache -f build/docker/mysql/Dockerfile .')
containerMysql = imageMysql.run("--name backend-server-mysql-dev -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_USER=root -e MYSQL_PASSWORD=mahmoud -e MYSQL_DATABASE=soextremedb")
sh 'docker ps | docker exec -it backend-server-mysql-dev /bin/bash | ls -l | mysqldump -u root -proot soextremedb < soextremedb.sql'
}
This is the error message:
Shell Script -- docker ps | docker exec -it backend-server-mysql-dev /bin/bash | ls -l | mysqldump -u root -proot soextremedb < soextremedb.sql -- (self time 566ms)
[soextremeBackEnd_Dev-MBC6SQWYSNVE6ADN2QOAOGZ4YYVT5E6K7Y2FUP6ROOROWRMCPFOA] Running shell script
+ docker ps
+ docker exec -it backend-server-mysql-dev /bin/bash
+ ls -l
+ mysqldump -u root -proot soextremedb
**mysqldump: Got error: 2002: "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")" when trying to connect the input device is not a TTY**
I think there are a couple of issues with the sh command.
First, | is used to send the output of one command on to the next command, but it looks like you're just trying to execute a sequence of commands. For that, you can use ; or &&. You might take a look at this answer for a great summary of shell operators.
Then, for your docker exec command, I think you actually want to call a series of commands non-interactively: leave off off the -it and use /bin/bash -c to pass a command string to the shell.
This will give you something like:
sh 'docker ps ; docker exec backend-server-mysql-dev /bin/bash -c "ls -l ; mysqldump -u root -proot soextremedb < soextremedb.sql"'
I have a Docker container running MariaDB and within the Dockerfile for the image I've copied an SQL file to a location in the container:
FROM mariadb
RUN mkdir /adhoc_scripts
COPY bootup.sql /adhoc_scripts
Once the container has spun up, I'm able to enter a shell within the container and confirm that the sql file does exist in the specified location:
$ docker exec -it my_mariadb_container bash
root#6e3f4b9abe17:/# ls -lt /adhoc_scripts/
total 4
-rw-r--r-- 1 root root 1839 Apr 10 18:35 bootup.sql
However when I exit the container and try to invoke the following command:
docker exec -it my_mariadb_container bash \
mysql mydb -u root -prootpass \
< /adhoc_scripts/bootup.sql
I get this error:
-bash: /adhoc_scripts/bootup.sql: No such file or directory
What am I doing wrong?
EDIT1: I tried changing the permissions of /adhoc-scripts to 777 as well, but that didn't help.
This is because the < operator operates on the whole docker exec ... command instead of the bash mysql ... part.
The error message is clear: your bash (outside the container) is trying to interpret the /adhoc_scripts/bootup.sql file.
To solve it, try this:
docker exec -it my_mariadb_container bash -c "mysql mydb -u root -prootpass < /adhoc_scripts/bootup.sql"
got a little script to pull up some mySQL server variants and test my scripts on different versions. My shellscript is:
docker pull mysql:$version
docker run -d -v $(pwd)/mysql.sh:/mysql.sh --name=mysql."$version" -e MYSQL_ROOT_PASSWORD='root' mysql:"$version"
docker exec -it mysql."$version" bash -c 'bash ./mysql.sh'
The mysql.sh file is:
/etc/init.d/mysql restart || service mysql restart || service mysqld restart
mysql -proot -e "SELECT ##version"
I get the following error:
./mysql.sh: line 1: /etc/init.d/mysql: No such file or directory
mysql: unrecognized service
mysqld: unrecognized service
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Anyone knows what's wrong?
docker does not use init.d structure for the services since it has no services. If you check inside the Dockerfile, you'll see that the mysql is started in foreground.
In your first script, the command docker exec -it mysql."$version" bash -c 'bash ./mysql.sh' is not needed. You should replace it with:
docker exec -it mysql."$version" mysql -proot -e "SELECT ##version"
The error indicates that /etc/init.d/mysql is not found. You need to change that to /usr/bin/mysql and it should work.
docker exec is not working for me. If I connect to a running container with docker exec -it mymachine bash and run sh -c 'mysql -u root < /tmp/schema.sql', it works.
If I just run docker exec mymachine sh -c 'mysql -u root < /tmp/schema.sql', it doesn't give any errors, but it doesn't do anything. Nor does it give me any errors if I run docker exec mymachine sh -c 'mysql -u root < /tmp/i_dont_exist.sql'.
How do I fix this?
try this -
docker exec mymachine "sh -c 'mysql -u root < /tmp/schema.sql'"
If not then the '<' operator will be picked up by the shell in the host machine and not the container.
I suspect that the < operator is trying to grab from your host rather than your container. Try enclosing the entire command in quotations.