How to use mysql docker container as mysql server for local development? How to use mysql docker container as mysql server for local development? docker docker

How to use mysql docker container as mysql server for local development?


Its good to learn new technologies. You want use mysql container instead of mysql of xampp stack. You should prefer official image. You can search docker images at hub.docker.comYou need to run this command from command line.

docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=db-password -v /my/own/datadir:/var/lib/mysql mysql

Above command will create docker container named mysql. You can able to access this on 127.0.0.1:3306 using user root and password as you given in command. Your mysql data will be stored at /my/own/datadir directory. So your data will be retained after restart.

Now its time to dump local mysql server data to container mysql. You can dump data using this command. Assuming that you have created backup dump file (say dbdump.sql for example). Copy dbdump.sql file to '/my/own/datadir/'

Now run this command.

docker exec -i -t mysql

from inside of container write these comands.

cd /var/lib/mysqlmysql -uroot -p < dbdump.sql

Note : You need to stop mysql of xampp before executing above commands. Enter password when asked.


So, my personal favorite way to do this is to use a tool called docker-compose. It allows you to define a simple yaml file, and then you have access to some extra commands that are prefixed docker-compose. I.e. docker-compose build, docker-compose up, etc. that make it much more convenient than running a bunch of separate docker commands and remembering container names for linking of containers and whatnot.

To give an example to help understanding see this docker-compose.yaml. In this file, you can conveniently declare your services (in this case db, php, and ansible <- each of which will be a separate container), and how they depend on each other, the paths to the individual Dockerfile for each container (build), and other goodies.

You can also specify environment information for the container to use (again, for the docker-compose I linked to, you can see ports, volumes, and environment for specifying information the container needs). In the example docker-compose file, in the environment: section of the db: service, you can see that it was trivial to specify this information, and upon starting the container, those values will automatically be used when installing/setting up mysql. It can be truly easy to have a running/configured mysql that is linked to other containers with one command. also there are other environment variables you can specify for the container to use (each one usually comes with several) click here to see a list

Take a look at a docker set up I made for LAMP stack development here. I have a couple different ones in my own github, but if you search github, you can find a bunch of docker/docker-compose set ups. They helped me get comfortable with docker (and still sometimes do) when I need to review how to set something up.

A really helpful starting guide can be found here. Docker compose has single-handedly made my workflow much easier to understand/implement several times over. It's definitely worth a look.


Please find this link, which contains below portion

volumes:db_data:/var/lib/mysql

Mysql will be installed in separate container but its /var/lib/mysql path maps to folder's db_data. So all databases stored on db_data folder.

php :

...depends_on:dbHere PHP container links with Mysql container.

Please check below link with detailed information.