Skip to content

Instantly share code, notes, and snippets.

@dan-mckay dan-mckay/mysql-docker.md
Last active Jun 5, 2017

Embed
What would you like to do?

Set Up

Run mysql as root on port 3306 (password: 123)

docker run --name db -d -e MYSQL_ROOT_PASSWORD=123 -p 3306:3306 mysql:latest

Get running docker processes

docker ps

CONTAINER ID  IMAGE         ...  NAMES  
36e68b966fd0  mysql:latest  ...  db  

If the image isn't running, you will need to start it, remember its name...

docker start ps

Run that docker image in an interactive terminal

docker exec -it db /bin/bash

login

mysql -uroot -p123

Start up scripts

Put these on the local machine image somewhere. You will need to make the shell scripts executable chmod a+x

start.sh

#!/bin/sh

# Run the MySQL container, with a database named 'users' and credentials
# for a users-service user which can access it.
echo "Starting DB..."
docker run --name db -d \
  -e MYSQL_ROOT_PASSWORD=123 \
  -e MYSQL_DATABASE=users -e MYSQL_USER=users_service -e MYSQL_PASSWORD=456 \
  -p 3306:3306 \
  mysql:latest

echo "sleeping..."
sleep 20

# Wait for the database service to start up.
echo "Waiting for DB to start up..."
docker exec db mysqladmin --silent --wait=30 -uusers_service -p456 ping || exit 1

# Run the setup script.
echo "Setting up initial data..."
docker exec -i db mysql -uusers_service -p456 users < setup.sql

setup.sql

create table directory (user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, email TEXT);  
insert into directory (email) values ('homer@thesimpsons.com');  
insert into directory (email) values ('marge@thesimpsons.com');  
insert into directory (email) values ('maggie@thesimpsons.com');  
insert into directory (email) values ('lisa@thesimpsons.com');  
insert into directory (email) values ('bart@thesimpsons.com');

stop.sh

#!/bin/sh

# Stop the db and remove the container.
docker stop db && docker rm db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.