This Gist describes the steps to build a MySQL Docker Image, where the standard mysql port (3306) is exposed to the host.
If you're behind a proxy, you'll need to ensure the http_proxy
and https_proxy
environment variables are set, so the proxy
configuration can be propagated to the created Docker Container. I also found I had to set these settings in my Mac's Docker installation preferences GUI, for some reason it would not pick up my system proxy settings.
For example:
export http_proxy=http://someproxy.corpdomain.com:80
export https_proxy=http://someproxy.corpdomain.com:80
Pull the MySQL image from the Docker repository:
docker pull mysql/mysql-server:8.0
When it comes to running the Docker Image, you have several choices to make, read the documentation for more detail on this. I'm only going to change a few things:
- set the name of the container to
mysql
- Store the database data on the host machine, this enables me to blow away the docker container at any time and re-create it without losing any data. In effect I'm separating the database 'engine' (the docker container) from the database storage (the volume on the host where the database storage is persisted).
I created a folder within my home folder:
cd ~/work/docker
mkdir mysql
Now we are all prepared, we can tell Docker to run the image, we'll give the container a name as well:
docker run --name mysql -p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=Do-not-do-this \
-e MYSQL_ROOT_HOST=% \
-v ~/work/docker/mysql:/var/lib/mysql \
-d mysql/mysql-server:8.0
- The
-name
argument names the container - The first
-e
argument sets theMYSQL_ROOT_PASSWORD
environment variable, this is not secure because environment variables are easily discoverable - The second
-e
argument enables the root user to connect from any host, again not recommended for security reasons, but if you wish to connect from the host machine then you need to do this. - The
-v
argument mounts the folder to store the database data
Once the initial setup of the database has completed, I like to stop the container and then start it again to leave it running in the background. To do this, in another terminal do the following:
docker stop mysql
docker start mysql
or simply:
docker restart mysql
If you ever need to get rid of the container, then the following will remove the container, you can recreate it again using docker run:
docker stop mysql
docker rm mysql