This document provides helpful Docker commands and snippets.

Remove unused containers, images, volumes and networks

docker system prune --all --volumes

Install a MySQL server

Pull image:

docker pull mysql/mysql-server:8.0

Start container:

docker run --name=mysql80 -p 3306:3306 -e MYSQL_ROOT_PASSWORD='<root_password>' -e MYSQL_USER='<user>' -e MYSQL_PASSWORD='<password>' -d mysql/mysql-server:8.0

Run mysql from the container:

docker exec -it mysql80 mysql -u root -p

Run mysql from the terminal:

mysql -h -P 3306 -u <user> -p

Note: Connect to instead of localhost on Ubuntu to solve ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2).

Send a SQL file to the container:

docker cp file.sql mysql80:/file.sql

In a mysql session, execute the SQL file and quit:

source /file.sql