Docker and MySQL

Containers are all the rage, and Docker is the container engine of choice these days.  So, I wanted to provide this tutorial for setting up a MySQL server instance in a Docker container.


  • Docker Desktop is installed and running.  If you’re on a version of Windows that doesn’t support Docker Desktop, then you can use Docker Toolbox.
  • Ability to access Docker commands from a terminal or command prompt.
  • MySQL Workbench is downloaded and installed on your computer

Getting Image and Creating a Container:

  1. Pull the MySQL image from DockerHub.  For the purpose of this specific tutorial, we’ll assume the image is mysql and not mysql-server.
    docker pull mysql
  2. Create a MySQL Container with container port 3306 mapped host port 3306.  This will create a container named cse3330db.  You should replace <some_password> with a password of your choice.  Don’t use a passwords that you might use in other places as it will be floating around in free text.
    docker run --name cse3330db -e MYSQL_ROOT_PASSWORD=<some_password> -p 3306:3306 -d mysql:latest

Connect to The Container

Use MySQL Client inside the container

Remember that MySQL is a client server architecture.  This image contains both the database server as well as the command line client tools to access the server.  To execute mysql (which is the client) inside the container, use the following command.

docker run -it --link cse3330db:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'

Starting a Bash Shell in the Container

If you want to start a shell inside the container itself, use the following command:

docker exec -it cse3330db bash

There’s very limited functionality, but you will be able to use basic shell commands.

Connecting from MySQL Workbench

  1. From the Database menu, choose ‘Manage DB Connections …’
  2. Choose ‘New’ in the bottom left corner of the dialog.
  3. Give your connection a new name
  4. Choose the Connection Tab
  5. For Hostname, use
  6. For Port, use 3306.
  7. For username, use root.
  8. Click Test Connection.  A dialog should pop up asking for the root user’s password. This is the one you used when creating the image above.
  9. If the connection test was successful, click close.
  10. You should see your new server connection on the Home screen of MySQL Workbench now.  Click on it to open the connection.

If there are no errors, then congrats!

Speak Your Mind



This site uses Akismet to reduce spam. Learn how your comment data is processed.