Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nasrulfuad/266478460df5b15999996ca52b6598b2 to your computer and use it in GitHub Desktop.
Save nasrulfuad/266478460df5b15999996ca52b6598b2 to your computer and use it in GitHub Desktop.
Create a mysql container in docker and connect to local machine

Connect to mysql container on docker from local machine

  1. Create a mysql volume
docker volume create mysql-volume
  1. Create a mysql container and pull mysql image from docker registry
docker run --name=nf-mysql -p 3306:3306 -v mysql-volume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -pw -d mysql
  1. Test to create a database inside the container nf-mysql
docker exec -it nf-mysql bash
mysql -u root -p
mysql> CREATE DATABASE MYSQLTEST;

By default, MySQL restricts connection other than the local machine (here Docker container) for security reasons. So, to connect from the local machine, you have to change the connection restriction:

mysql> update mysql.user set host = ‘%’ where user=’root’;
  1. Create a phpmyadmin volume
docker volume create phpmyadmin-volume
  1. Create a phpmyadmin container
docker run --name nf-phpmyadmin -v phpmyadmin-volume:/etc/phpmyadmin/config.user.inc.php --link nf-mysql:db -p 82:80 -d phpmyadmin
  1. Access it from http://localhost:82 or http://[::]:82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment