I am working to built a docker image that will make a mysql docker container. I have this little program that I wrote that will create the db I need and do some routine to push some data in it. The issue is that I need to program to execute automatically when the container is building.
The program works fine if I run it manually but never run when I try to run it in the dockerfile or in the docker-entrypoint.sh. It seems that when the container is running this script the server is not fully up or at least not accessible through localhost? I guess there is some other way to run custom programs after the docker container is built ? Or is the solution on the way I try to access the database ?
Edit
So there is my docker compose file content
db:
build: ./databaseDocker
volumes:
- ./database:/var/lib/mysql
environment:
MYSQL_DATABASE: 'MyDatabaseName'
MYSQL_USER: 'company_root'
MYSQL_PASSWORD: '12345'
MYSQL_ROOT_PASSWORD: '123456'
ports:
- 3306:3306
networks:
- app-network
Dockerfile in ./databaseDocker folder
FROM mysql:5.7.34
COPY ./dbseeder /tmp/dbseeder
WORKDIR /tmp/dbseeder
RUN chmod 777 .
COPY ./db_custom_init.sh /docker-entrypoint-initdb.d/
EXPOSE 3306
the db_custom_init.sh file which contain the command I am running
./DBSeedApp Param1 Param2
The app is home written using .net core to seed data in the database based on some parameters. The app is trying to connect to database like so:
string connectionString = $"server={host};port={port};user={username};pwd={password};database={databaseName};";
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.Connection = conn;
conn.Open();
//do stuff
conn.Close();
}
}
One solution I might look for which I don't like would be to seed the data to an external database and dump the SQL backup on the docker container for it to be executed directly through mysql cli.However, this method seems ineficient and I really would like to work directly with the docker mysql server.
New attempt
I changed my init file for :
echo "Check DB!"
while ! mysqladmin ping -h localhost -u root -p123456; do
echo "Wait ..."
sleep 1
done
echo "DB ready!"
./DBSeedApp Param1 Param2
But no luck I also added this to my program:
private void VerifyConnections()
{
try
{
string connectionString = $"server={host};port={port};user={username};pwd={password};";
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
System.Console.WriteLine($"Trying to connect to {host}");
conn.Open();
System.Console.WriteLine($"Connection successful to {host}");
conn.Close();
}
}
catch (Exception)
{
System.Console.WriteLine($"Cannot connect to {host}");
}
}
But no luck it confirms me that it can't find the localhost db server. I also tried by specifying the db container name but doesn't work either.
Got it working but...
I got it working using a dirty hack. I used an external mysql server to "build my seed" to then dump an sql file from this external server.
I ended up creating a dump argument on my program which tells it to connect to the external server, build the seed and then export it into an sql file.
Then I run this on my init.sh file
./dbseedapp DUMP param1 param2 ...
mysql -u root -p mydbname --binary-mode < /tmp/dbseeder/temp/dump.sql
I used MysqlBackup.Net to generate the sql file from the external mysql server.
I'd like to find an other solution but for now this is the best I came up with. If someone have a better idea... let me know.