Launching a MySQL container

Learn how to launch MySQL containers and the options Docker provides.

In this chapter, you will discover how to launch a MySQL container using both the Docker CLI and Docker compose.

MySQL is a popular SQL database if you’re developing an application that requires data storage. It doesn’t matter if you’ve never used MySQL or a database before. The tutorial will help you become familiar with launching Docker containers. The concepts can be applied to any dependencies your application requires.

Locate a suitable MySQL image on Docker Hub

The following shows the official MySQL image details on Docker Hub.

Docker repositories offer one or more variations of an image, each with its own tag. Tags often match the application’s MAJOR.MINOR.PATCH number if releases follow semantic versioning concepts.

For MySQL, you can choose from various releases such as 5.6, 5.7, or 8.0:

  • If you select an exact version, such as 8.0.19, that edition of MySQL will always be installed.
  • Selecting the major and minor version, such as 8.0, will install the latest release of that minor edition. That could be 8.0.19 now but will become 8.0.20 in the future.
  • Selecting just the major version, such as 8, will install the latest major release.
  • Selecting latest (or not specifying a tag) will install the latest release regardless of version.

For this example, latest is a good choice, but you would normally use an exact edition, so all developers and the production server are using the same dependency.

📌 Docker makes it easy to install and test database upgrades at any time.

Launch a MySQL container

Launch a MySQL container by entering the following command in the below terminal after connecting it:

docker run \
  -it --rm --name mysql \
  -p 3306:3306 \
  --mount "src=mysqldata,target=/var/lib/mysql" \
  -e MYSQL_ROOT_PASSWORD=mysecret \
  mysql
Terminal 1
Terminal
Loading...

All Docker CLI commands start with docker and instruction, such as, run followed by options. Following is the explanation of the above command:

  • The docker run creates a container from a specified image (mysql on the last line) and starts it.

  • The option -it opens an interactive session.

  • For a container to delete itself right after it stops, option --rm is used.

  • The --name allows us to name the container, in this case, we named it mysql.

  • The option -p maps a host port (3306 on the right side of :) to a container port (3306 on the left side of :) to be exposed. We can change the exposed port to any port we like, say 3000 or 8080, then mysql would be accessible on the specified exposed port.

  • To mount the volume, option --mount is used. The src specifies the name of the volume and target specifies the path to be mounted. The storage for Docker is usually in /var/lib/ followed by the container’s name or ID. mysql is a folder containing data about the container mysql.

  • The environment variables are defined under the option e. The variables along with its value proceeds the e option.

  • Lastly, the Docker image is specified, which in this case is mysql. That image is downloaded if it’s not already available on the host.

It can take several minutes to download the image, launch a container, and initialize MySQL the first time the command is run. Subsequent launches will be almost instantaneous. The database will be ready to use when you see:

[System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections.

Docker options

docker run offers numerous options, but the main ones you will use are:

Various Docker options

📌 If you do not specify --rm, the container will remain available even once it has stopped. It’s possible to restart it, but there’s rarely any benefit. It’s simpler to execute the same docker run command again.

MySQL credentials

MySQL stores user access credentials such as the root user’s password in an internal database. This is created when MySQL initializes storage space on its first launch.

The docker run command above stores all database data in a Docker volume named mysqldata. This retains data between restarts, including the first root password you set.

If you alter the run command to use a different password on a subsequent launch (e.g., -e MYSQL_ROOT_PASSWORD=NewSecret), it will be ignored. To change the root password, you can either issue a MySQL ALTER USER command using a MySQL client or, more drastically, delete the Docker volume and initialize the database again.

It’s good practice to create a MySQL user that is granted limited rights to a specific database, i.e., it can read data but not alter table structures or examine other databases. Application configuration is often achieved by setting environment variables.

Your application could launch its own MySQL container, so using a root user with all privileges seems less dangerous. However, a locked-down user will always be more secure and prevent malicious or accidental damage.

The examples shown in this chapter use root for brevity, but you should use better credentials during development and deployment.