Create MySQL docker container and access externally

If you have a fairly simple setup with one network card and would like a MySQL container bound to both the internet docker IP and the IP of your network card you can run the following:

docker run -p --name=mysql -d mysql/mysql-server:latest

To breakdown the above command: : This tells Docker to bind on all interfaces
33060 : This is the HOST port which you'll use to connect to the container
3306 : This is the internal CONTAINER port on which MySQL is running

By default Docker will create a root password for the created MySQL container, and this root MySQL password can be obtained by running the following command:

docker logs mysql

mysql is the name of the container created above. You will see the root password in the output of the above command. Next, access the terminal of the container with the following command:

docker exec -it mysql bash

Login to MySQL with the root password obtainer above:

mysql -u root -p

Run the following command to create a new user which will be allowed to connect from a specific IP address:

create user "someuser"@"source-ip-address" identified by "your-secure-password";

Next, grant privileges for this user on a specific database:

grant all privileges on databaseName.* to "someuser"@"source-ip-address"

Should you want this user to connect from any source IP address, the above commands can be modified as follows:

create user "someuser"@"%" identified by "your-secure-password";

grant all privileges on databaseName.* to "someuser"@"%"

Notice the replacement of source-ip-address with %.

You should now be able to connect to your MySQL database with the following command:

mysql -h destination-ip-of-host-machine -P 33060 -u someuser -p