To find publicly available Docker images, go to https://hub.docker.com/ and search. You'll find many Docker images ready to go. For example, Nextcloud, and its associated database, was shown earlier installed alongside the hello-world application when we kicked the tires. Both are available from their respective project teams and it's simply (more or less) a matter of typing docker run nextcloud to install and run the containers. The process of installing Nextcloud, and its associated database, as well as many other packaged applications, such as GitLab, is very similar to what we're about to do to build AuthNet, so the skills you're about to learn are very practical.
Just for MySQL, there are over 11,000 containers available. Fortunately, the two containers provided by the MySQL team are very popular and easy to use. The mysql/mysql-server image is a little easier to configure, so let's use that.
A Docker image name can be specified, along with a tag that is usually the software version number. In this case, we'll use mysql/mysql-server:5.7, where mysql/mysql-server is the container name, and 5.7 is the tag. MySQL 5.7 is the current GA release. Download the image as follows:
$ docker pull mysql/mysql-server:5.7
5.7: Pulling from mysql/mysql-server
4040fe120662: Pull complete
d049aa45d358: Pull complete
a6c7ed00840d: Pull complete
853789d8032e: Pull complete
Digest: sha256:1b4c7c24df07fa89cdb7fe1c2eb94fbd2c7bd84ac14bd1779e3dec79f75f37c5
Status: Downloaded newer image for mysql/mysql-server:5.7
This downloaded four images in total, because this image is built on top of three other images. We'll see later how that works when we learn how to build a Dockerfile.
A container can be started using this image as follows:
$ docker run --name=mysql --env MYSQL_ROOT_PASSWORD=f00bar mysql/mysql-server:5.7
[Entrypoint] MySQL Docker Image 5.7.21-1.1.4
[Entrypoint] Initializing database
[Entrypoint] Database initialized
...
[Entrypoint] ignoring /docker-entrypoint-initdb.d/*
[Entrypoint] Server shut down
[Entrypoint] MySQL init process done. Ready for start up.
[Entrypoint] Starting MySQL 5.7.21-1.1.4
We started this service in the foreground. The container name is mysql. We set an environment variable, which, in turn (according to the image documentation), initializes the root password as shown. In another window, we can get into the container and run the MySQL client as follows:
$ docker exec -it mysql mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.21 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
The docker exec command lets you run programs inside the container. The -it option says the command is run interactively, on an assigned terminal. Substitute bash for mysql, and you have an interactive bash command shell.
This mysql command instance is running inside the container. The container is configured by default to not expose any external port, and it has a default my.cnf file.
The database files are locked inside the container. As soon as that container is deleted, the database will go away. Docker containers are meant to be ephemeral, being created and destroyed as needed, while databases are meant to be permanent, with lifetimes measured in decades sometimes.
In other words, it's cool that we can easily install and launch a MySQL instance. But there are several deficiencies:
- Access to the database from other software
- Storing the database files outside the container for a longer lifespan
- Custom configuration, because database admins love to tweak the settings
- It needs to be connected to AuthNet along with the user authentication service
Before proceeding, let's clean up. In a Terminal window, type:
$ docker stop mysql
mysql
$ docker rm mysql
mysql
This closes out and cleans up the containers. And, to reiterate the point made earlier, the database in that container went away. If that database contained critical information, you just lost it with no chance to recover the data.