10

I am new to Docker, and still use XAMPP for PHP. Recently, I have installed Docker on my localhost. I have gone through the documentation. Now it's time to implement it practically. I know how to run Docker images, but I want to learn the best way to use the MySQL Docker image for local development.

I am planning to use a MySQL Docker container instead of MySQL in XAMPP. Looking for help with the best setup.

3 Answers 3

12

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 will be 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.

Sign up to request clarification or add additional context in comments.

5 Comments

I am following your instruction. My dbdump file is 5.6 GB. I run your command, after 1 hour its still running. How much time it will take? Is it normal?
Well, its depends on PC configuration. I mean read-write speed of your hard drive and network configuration. Your dump fie is very large. So, in your case it's normal.
Thank you so much. I want exactly this type of container.
How does one do this without existing data? I'm installing Apache Guacamole.
You don't need to run additional commands which do ssh into docker container and import the db dump.
7

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.

2 Comments

How can I import my dbdump using docker-compose?
Thank you very much. This will help me a lot to learn docker. You idea of use of docker compose is very nice.
1

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: db Here PHP container links with Mysql container.

Please check below link with detailed information.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.