Setting Up a MySQL Docker Container for Development: A Comprehensive Guide
Why Docker? Docker, a powerful tool for creating, deploying, and running applications by using containers, offers a solution. This article guides you through efficiently setting up a single MySQL container, importing a database schema, and establishing a secure connection using environment variables within your application.
Setting Up Your MySQL Container
Our journey begins by fetching the official MySQL image from Docker Hub.
docker pull mysql
Run MySQL Container
Next, we create a MySQL container with a predefined root password. For persistence beyond the lifetime of the container, we mount a volume from the host to the container’s data directory
docker run --name mysqlContainer -e MYSQL_ROOT_PASSWORD=secretpassword -d mysql
# Or with volume for data persistence
docker run --name mysqlContainer -e MYSQL_ROOT_PASSWORD=secretpassword -v /path/to/host/directory:/var/lib/mysql -d mysql
- docker run: This command instructs Docker to create and run a new container.
- — name mysqlContainer: This assigns a name (mysqlContainer) to the container for easier identification and management.
- -e MYSQL_ROOT_PASSWORD=secretpassword: This sets an environment variable named MYSQL_ROOT_PASSWORD within the container, defining the root password for MySQL access.
- -d: This flag runs the container in detached mode, allowing the container to operate in the background even after the terminal command finishes execution.
- mysql: This specifies the image to use for the container
- The -v flag instructs Docker to mount a volume. The format is <host directory>:<container directory>, specifying the locations on both the host machine and the container.
Accessing the MySQL Shell
To interact with your MySQL instance, access the shell as follow
docker exec -it mysqlContainer mysql -u root -p
- docker exec: This command allows us to execute commands within a running container.
- -it: This flag provides an interactive shell, essentially giving us a terminal session inside the container.
After entering the password, you can explore your MySQL server. Remember to exit; once done.
Importing Your Database
Often, you’ll need to import a pre-existing database. After copying your SQL dump into the container:
docker cp /path/to/directory/dump.sql mysqlContainer:/tmp/database.sql
- docker cp: The Docker command used for copying files.
- /path/to/directory/dump.sql: The full path to the SQL file on your host machine that you want to copy.
- mysqlContainer: The name of the running Docker container where you want to copy the file.
- /tmp/database.sql: The destination path within the container where the file will be placed. In this case, it’s copied to the /tmp directory and renamed to database.sql.
You can import it using as
#Option A
docker exec -it mysqlContainer bash
mysql -u root -p<secretpassword> < /tmp/database.sql
- bash: This tells docker exec to run the /bin/bash shell within the container.
- <: This is the redirection operator. It tells the mysql command to read the SQL statements from the specified file instead of standard input.
#Option B
docker exec -it mysqlContainer bash
mysql -u root -p<secretpassword>
source /tmp/database.sql
Find the Container’s IP Address
After populating database, the next step is to connect your application. You’ll need the container’s IP address.
docker inspect container_id | grep IPAddress
# Or
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_id
With the IP address, configure your application’s database connection as follows:
DB_CONNECTION=mysql
DB_HOST=CONTAINER_IP
DB_PORT=3306
DB_DATABASE=DB_NAME
DB_USERNAME=root
DB_PASSWORD=secretpassword
Replace CONTAINER_IP, DB_NAME, and secretpassword with your container’s IP, your database name, and the root password you set earlier.
Remember, while the root account and simple passwords are convenient for development, always use more secure settings for production environments. Happy coding! Have fun 🔥 💪. Find me on Github, Medium.
Have you tried using Docker for managing your MySQL databases? Share your experiences and tips in the comments below!