Throwaway MySQL Server in Docker

I had a MySQL database backup that I wanted to restore to a temporary MySQL Server instance so I could browse through the tables and perform some small queries. For this I needed a “throwaway MySQL Server”…

The following steps enable you to install a temporary MySQL Server in a Docker container, import the SQL dump, poke around in the DB and finally throw it all away cleanly. I used Debian 8, but I guess any Unix will do just fine.

  1. Install docker with this easy script:
    $ wget -qO- https://get.docker.com/ | sh
  2. Add yourself to the docker user group, then log out and log in again.
    $ sudo usermod -aG docker myusername
  3. Start the MySQL Server 5.5 instance:
    $ docker run --name temp-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.5
  4. Check the instance is running:
    $ docker ps
    CONTAINER ID  IMAGE     COMMAND                 CREATED         STATUS        PORTS     NAMES
    791ae858ac8f  mysql:5.5 "/entrypoint.sh mysql"  19 seconds ago  Up 18 seconds 3306/tcp  temp-mysql
  5. Start the MySQL command line client, connect it to the docker instance running the MySQL Server (–link) and mount the SQL dump file from your home directory in the container (-v):
    $ docker run -it --link temp-mysql:mysql -v ~/my_db_dump.sql:/root/my_db_dump.sql --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"'

    …and you get a MySQL prompt:

    Server version: 5.5.47 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>
  6. Create the new database, load the SQL dump file and query away:
    mysql> create database tempdb;
    mysql> use tempdb;
    mysql> source /root/my_db_dump.sql;
    mysql> show tables;
    mysql> ....
    mysql> quit;
  7. When you are done, you can stop and remove the docker container:
    $ docker stop 791ae858ac8f
    $ docker remove 791ae858ac8f

For inspiration I used Getting Started with Docker and mysql from the Docker Hub.

Leave a Reply

Your email address will not be published.