How do I set up MySQL with Docker

MySQL can be easily set up using Docker, which allows you to create a lightweight, portable, and efficient database environment. Here’s a step-by-step guide to getting MySQL up and running with Docker.

Step-by-Step Guide to Setup MySQL with Docker

  1. Install Docker: First, ensure you have Docker installed on your machine. You can download it from the Docker website.
  2. Pull MySQL Docker Image: Use the following command to pull the latest MySQL image from Docker Hub:
  3. docker pull mysql:latest
  4. Run MySQL Container: Start a MySQL container by running this command, making sure to set the MySQL root password:
  5. docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
  6. Access MySQL Shell: You can connect to the MySQL database inside the container by running:
  7. docker exec -it mysql-container mysql -u root -p
  8. Create Databases and Tables: Now that you're connected, you can create your databases and tables as needed.

This process makes it convenient to manage MySQL databases in an isolated environment. You can stop and restart your container as needed, keeping your data safe and easily accessible.


MySQL Docker Docker setup MySQL container database management