In this step, we'll launch a MySQL database in a container in which the data will be stored. We'll begin with the database since the application is dependent on the database for storage.
With Docker, all containers are based on a Docker image. The first step will be to find the image for MySQL. There are a lot of options on Docker Hub, but we'll stick with the official repository, which can be found here.
One way to look up an image is to run the command
docker search <name>.
Running the command
docker search --limit 5 mysql will display the matching repositories, limited to five results with the
--limit x option.
To pull the image to your host, run
docker pull mysql.
An image can be run by using the command
docker run <options> <image-name>. For example, we can run the mysql image in the background by running
docker run -d mysql. By default, a container in Docker is launched in the foreground. The option
-d makes sure that the process runs in the background.
Finally, to launch an instance of the MySQL image in a container, execute the command
docker run --name tbl06db -e MYSQL_ROOT_PASSWORD=tbl06pw -e MYSQL_DATABASE=tbl06 -p 3306:3306 -d mysql
The command option
--name gives the container a unique name; tbl06db .
In addition, we set the
root password to "tbl06pw", and create and use a database called "tbl06".
-p exposes the port 3306, which lets the application interact with the database (port 3306 is default for MySQL)
Finally, the option
-d runs the process in the background.