Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am new to docker technologies. Still now I am using xampp for development of PHP. Recently, I have install docker on my localhost. I have gone through documentation. Now its time to implement practically. I know how to run docker images. But I want to learn best way to use mysql docker container for local development.

I am planning to use mysql docker container instead of mysql of xampp. Looking for help for best setup.

like image 917
Om Puri Avatar asked Jun 15 '17 16:06

Om Puri


People also ask

Can we use Docker for local development?

With Docker, the web frontend, Redis, and Postgres each run in a separate container. You can use Docker Compose to define your local development environment, including environment variables, ports you need accessible, and volumes to mount. Everything is defined in docker-compose.

How does Docker container connect to local database?

A simple solution to this in a Linux machine is to use the --network=”host” option along with the Docker run command. After that, the localhost (127.0. 0.1) in your Docker container will point to the host Linux machine. This runs a Docker container with the settings of the network set to host.

Can Docker communicate with localhost?

docker run --network="host" Alternatively you can run a docker container with network settings set to host . Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0. 0.1 ) will refer to the docker host.


2 Answers

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.com You 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/mysql
mysql -uroot -p < dbdump.sql

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

like image 177
Virendra Jadeja Avatar answered Oct 07 '22 05:10

Virendra Jadeja


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.

like image 35
Andrew B Avatar answered Oct 07 '22 06:10

Andrew B