Running MySQL in a Docker Container is great for being able to emulate various environments. The more advanced containers will include a bunch of executables and runtimes that will allow you to do run more intricate commands. These commands can be very useful for sharing with other teams for testing purposes. It doesn't matter what runtimes they have on their end, by using the docker
command, you'll be able to specify what runtimes to use to execute your code.
Joel Lord: [0:00] Here's how you can run a database using a container. First, you will use the same docker run command we used for your previous example. Next, you will need to map some ports. In this case, we are telling Docker that any requests incoming on port 3306 will be mapped to the port 3306 inside the container, which is the default port for MySQL.
[0:27] You'll want to run this application in detached mode, which means it will run in the background. To do this, you add the -d flag. You'll also eventually want to interact with this container, so we'll need to give it a name. You can use --name to do so.
[0:45] Next, MySQL will need a few environment variables to configure itself when it starts. You will need to add values for the user by using the environment variable MySQL_USER. We will add another environment variable for the password using MYSQL_PASSWORD = MySQL.
[1:09] You can also add a root password by using another environment variable called MYSQL_ROOT_PASSWORD. We'll use root for now.
[1:21] Finally, you specify which image you want to use. In this case, we'll be using MySQL 5.7. You should then see a long hexadecimal string. This is your container ID. To validate that your container was started, you can use the command docker ps. This will list any running container you currently have.
[1:47] You can see some basic information here like the ports that were mapped, as well as the name of this container. If you want to stop this container, you can use the very first few characters from the ID or the actual name of the container with a docker stop command. If you run docker ps again, you will notice that there are no more running containers.
[2:11] You can now restart this container with the previous command that you typed in. Now you should be getting in there. This is because we're trying to start a container called keithcourse-db, but we already had a container with this name in our internal registry.
[2:28] To restart the server, you will need to remove the previous container first by using docker rm keithcourse-db. Now, restart the container with the previous command again, but this time add the --rm flag. This will take care of automatically removing this container once it stops.
[2:56] You now have a running MySQL Server on your machine. Everything came preconfigured for you and it just works out of the box. If you share this same command with your team members, they will be able to run the same database.