MySQL DB w Docker for local testing - labsce1-wellness-bet/backend GitHub Wiki

Prerequisites

  1. Install MySQL or use homebrew brew install mysql
  2. Windows 10 Professional, Enterprise, or Server 2016 with a CPU supporting virtualization features OR Mac OSX 10.11+
  3. When running in Windows, you'll need to run an Administrator command prompt or powershell prompt for the docker commands. You can do this by right-clicking the Windows icon in the taskbar, then selecting 'Command Prompt (Admin)' or 'Windows Powershell (Admin)'.

Setting up a local MySQL test environment

  1. Sign up for Docker (making note of your credentials) and download Docker for Desktop
  2. Once docker is installed, run the following command to download the MySQL container template.
    • docker pull mysql:8.0.16
  3. Run the following command to create a MySQL container, replacing 'my-secret-pw' with an arbitrary password to connect to MySQL with.
    • docker run --name wellness-mysql --publish 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:8.0.16
      • Breakdown of the command
        • 'docker run': Runs a process in an isolated container.
        • '--name': Designates the container's name.
        • '--publish': Maps an external port to an internal port in the container. Without this, the container will have no external access.
        • '-e': Allows you to set environmental variables published by the container. The environmental variables are created during container template creation.
        • '-d': Daemonizes the container, causing it to run as a background process.
        • 'mysql:8.0.16': Defines the container template to use for the container.
  4. Run the command docker ps -a to list out all the containers on your system. Verify that the 'wellness-mysql' containers STATUS is listed as up.
  5. Download and install MySQL Workbench for Windows (exe) or OS X (dmg): https://dev.mysql.com/downloads/workbench/
  6. Start MySQL Workbench, click on 'database>manage connections...'
  7. Give Connection name, and set other inputs based on variables entered for docker run command. For above example Set 'Hostname' to 'localhost', 'Port' to '3306', 'Username' to 'root', and 'password' to 'my-secret-pw'
  8. Click 'Test Connection' to check if successful.
  9. Connect to database, click on 'database>connect to database'
  10. Select from stored connections and hit 'ok'
  11. Now Create a new schema in the connected server. Schema is synonymous for database in Mysql workbench.

Notes

General notes

  • docker ps -a lists of all the containers on your system.
  • docker stop wellness-mysql will stop the container named 'wellness-mysql'.
  • docker start wellness-mysql will start the container named 'wellness-mysql'.
  • docker rm wellness-mysql will delete the container names 'wellness-mysql' if it is already stopped.
  • docker image ls will list the container templates you've downloaded. You can use these to create new containers with docker run.

Notes for Windows

  • Docker for Windows install guide: https://docs.docker.com/docker-for-windows/install/
  • On Windows, Docker sets up a Hyper-V virtual machine on install to run docker containers on by default.
  • After Hyper-V is enabled, VirtualBox no longer works, but any VirtualBox VM images remain. The inverse is also true. Installing VirtualBox after Hyper-V will make any Hyper-V/Docker images no longer work.

Notes for OSX

  • Docker for OSX install guide: https://docs.docker.com/docker-for-mac/install/
  • For OSX, VirtualBox prior to version 4.3.30 must NOT be installed (it is incompatible with Docker for Mac). If you have a newer version of VirtualBox installed, itโ€™s fine.