Docker Community Forums

Share and learn in the Docker community.

Execute mysql commands in bash

(Katkol) #1


I tried to create a mysql docker container and run a java application based on it. Here is the yml file.

version: '3'


  image: mysql/mysql-server:5.7
  container_name: mysql_server_container
   - "3306:3306"
   - net1
   - "./test/mysql_db:/var/lib/mysql:rw"
   - "./WeatherStation_NGAC_Server_Docker:/docker-entrypoint-initdb.d/"

   context: .
   dockerfile: Dockerfile_1
   - "5684:5684"
   - net1
   - mysql_server
   - "./WeatherStation_NGAC_Server_Docker:/dbscripts"
  command: >
      /bin/bash -c "
        sleep 15;
        exec mysql_server_container mysql -uroot -proot -hlocalhost < dbscripts/policydb.sql
        exec mysql_server_container mysql -uroot -proot -hlocalhost < dbscripts/accountingdb.sql
        java -jar server.jar;

when I try to execute docker exec container … in terminal, it works fine. but if i use the same command with bash as in yml file it gives an error /bin/bash: line 2: exec: mysql_server_container: not found. Here is the execution log.

mysql_server_container | [Entrypoint] MySQL Docker Image 5.7.21-1.1.4
mysql_server_container | [Entrypoint] Starting MySQL 5.7.21-1.1.4
weather_station_app_1  | /bin/bash: line 2: exec: mysql_server_container: not found

From here, we can see that mysql_server_container is already started before the applicaiton execution.

Can some one help me why i get this message.

(Sam) #2

you cannot send commands to a container…

you can execute commands HERE in THIS container…

so, the command you were trying to issue is ‘mysql_server_container’, which doesn’t exist


-h localhost

is not correct, in the weather station you need to talk to the mysql_server_container where the database is…
localhost is inside the weather station container

-h mysql_server_container

might work.

(Katkol) #3

That didn’t really work.

(Sam) #4

thats a little vague… could you give us some detail and what didn’t work

(Katkol) #5


even I change the host name to container, it didn’t work. Basically, I want to run a MySQL script before I run the java application. Anyways to do that? If I run mysql -uroot -proot ... command in the same container as mysql, then it returns ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) and if I run in the other container, it says mysql not found. Don’t know what is the correct way to do this. but if i run docker exec container_name mysql .... in terminal, it works fine.

(Sam) #6

the problem is that the MYSQL server is not yet running, it is started as part of the container startup which ‘command’ overrides…

(Katkol) #7

I actually tried it this way: command: > /bin/bash -c "/etc/init.d/mysql start; mysql -uroot -proot , which should start mysql server before executing the script. but when executed, bin/bash: /etc/init.d/mysql: No such file or directory

(Sam) #8

I assume you are using the offical dockerhub mysql container

here is the link to its doc on how to do what you want