Execute mysql commands in bash

Hi,

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

version: '3'


services:


 mysql_server:
  image: mysql/mysql-server:5.7
  container_name: mysql_server_container
  environment:
   MYSQL_ROOT_PASSWORD: root  
  ports:
   - "3306:3306"
  networks:
   - net1
  volumes:
   - "./test/mysql_db:/var/lib/mysql:rw"
   - "./WeatherStation_NGAC_Server_Docker:/docker-entrypoint-initdb.d/"

 weather_station_app:
  build:
   context: .
   dockerfile: Dockerfile_1
  ports:
   - "5684:5684"
  networks:
   - net1
  depends_on:
   - mysql_server
  volumes:
   - "./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;
      "
networks:
 net1:

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.

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

also,

-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.

That didn’t really work.

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

Hi,

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.

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

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

I assume you are using the offical dockerhub mysql container

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

https://hub.docker.com/_/mysql/