Search code examples
mysqlbashdockershellcontainers

Execute command thanks to a Bash script in a docker container


I work on a project and I need to automate the installation of the server. The server work with repository that I get on GitHub and after that the services are started to make the server working.

So thanks to my bash script I get all repositories, I start container, but after that I have a problem to restore the mysql DB :

I need to execute some command on my container, so I connect myself to the container like this :

docker exec -ti {my_container_name} sh

When my script execute this command, a shell is opened, and I can type the rest of my commands and when i type exit the rest of the following commands are executes :

cd /var/lib/mysql/restore

echo 'CREATE DATABASE apik;USE apik;' | cat - apik.db.sql > temp && mv temp apik.db.sql
mysql --user=root --password=$MYSQL_ROOT_PASSWORD < apik.db.sql

echo 'CREATE DATABASE apik_data;USE apik_data;' | cat - apik_data.db.sql > temp && mv temp apik_data.db.sql
mysql --user=root --password=$MYSQL_ROOT_PASSWORD < apik_data.db.sql

echo 'USE apik;' | cat - apik.data.sql > temp && mv temp apik.data.sql

mysql --user=root --password=$MYSQL_ROOT_PASSWORD < apik.data.sql
exit

All there commands work if I type them in the shell that have been open, but here is my problem .. I need to type them with my keyboard, but I would the script do it for me.

I search lot of things to do it, but i didn't find a good way to do it.

So what I want is when the docker exec ... command is execute, I want the commands after this command are execute in the shell that have been open by the first command.

Here's the code I have currently :

git clone git@<git_file>.git ${APIK_ROOT_DIRECTORY}/data/mysql/restore

docker exec -ti mysql_mysql_1 sh

cd /var/lib/mysql/restore

echo 'CREATE DATABASE apik;USE apik;' | cat - apik.db.sql > temp && mv temp apik.db.sql
mysql --user=root --password=$MYSQL_ROOT_PASSWORD < apik.db.sql

echo 'CREATE DATABASE apik_data;USE apik_data;' | cat - apik_data.db.sql > temp && mv temp apik_data.db.sql
mysql --user=root --password=$MYSQL_ROOT_PASSWORD < apik_data.db.sql

echo 'USE apik;' | cat - apik.data.sql > temp && mv temp apik.data.sql
mysql --user=root --password=$MYSQL_ROOT_PASSWORD < apik.data.sql

exit

We can see on my screen that when my script execute the docker exec command, a shell is open and my script is paused. And when I exit this shell, the rest of the command is exec. But I want that the commands be executed when the shell is open. Execution of my script


Solution

  • You can try this :

    git clone [email protected]:api-k/mysql-data.git ${APIK_ROOT_DIRECTORY}/data/mysql/restore
    
    docker exec -ti mysql_mysql_1 sh << 'EOF'
    cd /var/lib/mysql/restore
    
    echo 'CREATE DATABASE apik;USE apik;' | cat - apik.db.sql > temp && mv temp apik.db.sql
    mysql --user=root --password=$MYSQL_ROOT_PASSWORD < apik.db.sql
    
    echo 'CREATE DATABASE apik_data;USE apik_data;' | cat - apik_data.db.sql > temp && mv temp apik_data.db.sql
    mysql --user=root --password=$MYSQL_ROOT_PASSWORD < apik_data.db.sql
    
    echo 'USE apik;' | cat - apik.data.sql > temp && mv temp apik.data.sql
    mysql --user=root --password=$MYSQL_ROOT_PASSWORD < apik.data.sql
    EOF