Skip to content

Instantly share code, notes, and snippets.

@matthewpick
Created May 4, 2018 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewpick/150e50f351dc213ed8989ea8d826a0a4 to your computer and use it in GitHub Desktop.
Save matthewpick/150e50f351dc213ed8989ea8d826a0a4 to your computer and use it in GitHub Desktop.

Running MySQL in a Docker container

Docker

Step 1

Clone the mysql dockerfile repo

git clone https://github.com/docker-library/mysql.git
cd mysql/5.7

Step 2

Create an env file

development.env

MYSQL_ROOT_PASSWORD=mypassword
MYSQL_USER=SvcUser
MYSQL_PASSWORD=myservicepassword
MYSQL_DATABASE=my_service

Step 3

Build and Run the image

docker build .
docker run --name=mysql1 --env-file development.env -p 3306:3306 <image-id>

Connecting to MySQL

Via command line

docker exec -it mysql1 bash
mysql -u SvcUser -pmyservicepassword

Via local machine

mysql -h 127.0.0.1 -u SvcUser -pmyservicepassword

Note: must use 127.0.0.1 and not localhost

Via another local Docker container

ifconfig # find your local ip address

# test that you can connect to your dev machine's ip address
mysql -h 192.168.1.142 -u SvcUser -pmyservicepassword
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment