Assume that you have two docker services/containers. One must be ready to accept connections from the other one. As you have just noticed, I didn't say 'one must be running' because there is different between being 'ready to accept connections' and 'running'. The depends_on
key of docker compose doesn't always solve the problem because it just checks if the container is running. This is often the case if a service depends on MySQL, RabbitMQ, Elasticsearch like services. They take time to accept connections. To solve such issue, we can use an additional script. This script would periodically ping these services until they are ready to accept connections before actually running the main service.
Docker engineers solved this problem with a trick. We cannot have a Linux container on Windows, but we can put Linux in windows machine by using a hypervisor. So the solution was quickly coined. Docker Desktop for windows use a Hypervisor (usually Microsoft HyperV) for running a virtual machine, and it shares the VM OS to the docker container. Why is Docker so slow? The root of the issue is that Windows 10 is (was) using WSL (Windows Subsystem for Linux), which is a layer between Windows and Linux. Communication between these two (Hard Drive operations) can be quite slow. The following is to use docker-entrypoint-initdb.d to import sql. But it only takes one minute to import using the Navicat tool, How can i fix it. The text was updated successfully, but these errors were encountered: Copy link. I have noticed that when running a database server MySQL or Maria DB in a docker container on the M1 Technical Preview, it is extremely slow. Watching a visual inspection of the table creation you can see how slow it is. If I then wait 20 seconds and retry that command, it will work. I cannot seem to identify where the crash is in the logs. My guess is the SQL service is crashing because the PHP-FPM continues to run but the MySQL server connection refuses and in the logs I see: mysql1 2021-08-08T23:62Z 1 System MY-013577 InnoDB InnoDB.
In this example we have a Go and MySQL service. Go depends on MySQL. MySQL is slow in accepting connections. We will use our script in Go to force it to wait for MySQL to accept connections first. Once MySQL says it is ready then we will bring up the Go service.
Structure
Files
main.go
Dockerfile
init.sh
You can use exactly the same script for other services or just add other services here.
Docker Mysql Too Slow
docker-compose.yaml
Test
Failed to connect in 10 seconds
I actually changed the 1
second to 0.001
(1ms) in the script in order to simulate termination here.
Successfully connected in 3 seconds
In this article, we are going to see a step-by-step tutorial on how to run a MySQL database in a Docker Container.
1.Downloading a MySQL Server Docker Image
To download the image, open the command line and type this command:
The :latest tag will download the latest version of MySQL. If you want do download a specific version, simply replace the
latest (Ex: mysql-server :8.0)
2.Start a MySQL Container in Docker
The next step is to run a container in Docker with the MySQL image. To do this, execute the next command:
Let’s break down this command to understand it better:
- run - will run a new command in a new Docker container
- –name - will give a name to the new container created
- -p - will make the internal docker port visible outside docker
- -e - will change the root password. Here you can insert whatever password you want
- mysql/mysql-server:8.0 - will specify what image to run in the newly created container
To verify if the container was created and running, we can execute a docker ps (process status) command:
This will list all the running containers as shown above.
In the status column of the result from above, you can see the (health: starting) mention. After the container is initialized
and ready to run, you will se it change to (healthy).
3.Connecting to MySQL Server from within the container
To connect, we will first run the next command:
This will require the root password set in the previous step. After inserting the password, you should be inside MySQL
monitor.
Type exit to leave the program.
4.Stopping the MySQL container
To stop the container, simply execute the next command:
5.Deleting the MySQL container
To delete the container, make sure that it is stopped. Then, execute this command:
Docker Mysql Slow Log
Share this blog post on Twitter, Facebook, and LinkedIn