These are my steps to create a MySQL Docker container, i am using Docker for Mac.
1 – Create a folder to save the database. If you do not create a Docker volume you will loose your data when you restart your container. My folder is /Users/keniocarvalho/VolumesDocker/mysqdata
2- Execute the command bellow:
docker run –name mysqldb -p 3306:3306 -v /Users/keniocarvalho/VolumesDocker/mysqdata/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest
3 – Enter on the mysql container using the command : docker exec -it mysqldb bash
4 – Inside the container type : mysql -uroot -p. The system will ask for the password. Here the password is password.
5 – To access the database from outside the container run this commands on the mysql console:
CREATE USER ‘root’@’%’ IDENTIFIED BY ‘root’;
GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ WITH GRANT OPTION;
FLUSH PRIVILEGES;
Verify the permission:
mysql> SELECT host, user FROM mysql.user;
+———–+——————+
| host | user |
+———–+——————+
| % | root |
| localhost | healthchecker |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+———–+——————+
Now you can access the mysql from outside of the container using port 3306 from any host.
Obs: This is not a setup for Production use only in your development workstation.