Requirements
- Docker installed
Configuration file
To download our mysql image and run a container, we will use docker compose yaml file. Below is a sample file
1. Start our docker container
From the compose file, we will pull an image from the public repository to our local machine
Command to run our compose file :
docker-compose -f sample_docker_compose.yaml up
2. Check our networks, volumes and view sql file inside our mysql container
If we were to containerize our application that will have access to our mysql container, then we must ensure they are on the same network and we can define in our compose file. Volume on the other hand will provide persistence of our data. It is more like a backup area on our local machine or remote servers. Volumes will help us maintain our data even after restarting a container.
- Command to check the networks:
docker network ls
- Command to check the volumes:
docker volume ls
- Command to check the running containers:
docker ps
- Command to access our container’s interactive shell:
docker exec -it ABCDE /bin/bash
ordocker exec -it ABCDE /bin/sh
whereABCDE
is the container ID
Once we are the container’s interactive shell, we can navigate to our volume location which we specified in our compose file i.e cd /home
and the you can see the sql file which was backed up from our local machine.
3. Create our database and import our sql file using two methods
a.) Method 1
After creating our database, we can use the database and import tables using the command SOURCE /home/backup.sql
Method 1 Tables
b.) Method 2
Having our known database e.g sql_compose_another, inside the container’s interactive shell, we can run the command below to import sql tables to that database
mysql -u root -pPassword sql_compose_another < /home/backup.sql
Method 2 Tables