0

When building a mysql container from docker-compose, once built is complete mysql takes few seconds before it comes alive. During this time I have a script that runs laravel migration. However it is impossible to connect to mysql for few seconds as it is still connecting (not sure exactly what it is doing). To fix that I added in my script a sleep 30 which pauses for 30s while it waits for mysql container to fully come up live.

I dont like this work around as it doesnt really fix the problem, what if next time I build the container it takes 31s? then the laravel migration would still fail.

Has anyone has this problem migrating a fresh (just booted) mysql database container? any ideas how I could fix this?

1 Answer 1

1

I'm not exactly sure about your docker configuration but as a linux user I would use netcat to test the TCP Connection. (On debian/ubuntu: apt install netcat)

To get the container IP, you could use:

containerIP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id)

And then wait for the connection with netcat

nc -w 45 -v $containerIP 3306 </dev/null

The -w sets the timeout you want

So you can run your migrate or do something else if it's failed.

if [ $? -eq 0 ]; then
    echo "Run your migrate"
else
    echo "Unable to connect"
fi
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.