You can help by commenting or suggesting your edit directly into the transcript. We'll review any changes before posting them. All comments are completely anonymous. For any comments that need a reply, consider emailing firstname.lastname@example.org.
We are experiencing playback issues from our video hosting provider. Please check back shortly.
Adding a Database
In this lesson, we'll add the next piece of our testing environment: the database.
Transcript(open in window)
[00:00] So far, we've created two Ignition containers in our Docker Compose stack and connected them via the Gateway Network. In this lesson, we'll build out the next component in our architecture diagram, which will be the database. We'll need a MariaDB image. So let's head over to Docker Hub to find one. I'm gonna get there via the Docker Image page in our user manual. We can search for MariaDB and see the different images that are available to us. Right here we can see the MariaDB relational database image and what's nice is that we can see this is marked as an official Docker image so we know that we can trust it. We'll come back to the overview page later, but I want to find a specific version to pull so we can go to tags. And I'm going to find the latest minor version. Which looks like 10.10.2. If we open our docker compose file, we can start creating the service for our database. We'll use the MariaDB image and then the 10.10.2 tag.
[01:08] Let's map port 3306. Then let's set up a volume to persist our database data. On the MariaDB overview page we can find information about the image and how to use it. We can scroll down and see if there's information here about environment variables, which we'll come back to. But then we see this section on where to store data. It shows how to set up a named volume to store the data and mentions at the end here that MariaDB writes its data files by default and the "/var/lib/mysql" directory. We'll connect the volume to that directory in our container. Let's move along to the network, which is going to be the backend network since it needs to communicate with the backend gateway. We can actually add a network alias to our container here if we wanted to specify a hostname when connecting to our various containers. Say for example that our production environment we're replicating connects to a database with the hostname of "main-db".
[02:09] We can add an alias of "main-db" and then if we were to later restore our gateway configuration into that environment that connection would resolve just fine. Next let's use environment variables to initialize the installation. You can review the different variables here, but I've already identified some that we can add. "MARIADB_ROOT_PASSWORD" is required. So let's add that and we can make the root password "ignition". Let's create a database on initialization which we can call ignition. And then let's create a user password combo and make both of those ignition, too. And that should be good on environment variables. Since we added a volume don't forget to add it to the list at the bottom of the stack and then we can save our compose file. Let's run "docker compose up" again.
[03:06] If you already have your containers running it'll identify what's new and apply those changes. And it'll download the MariaDB image. Once that's done, we can test this by running a bash terminal in the DB service. To log in to MariaDB we can use the "mysql" command and pass the username and passwords of "ignition". And then the database name is "ignition". And we can see that we have a database instance running. Let's get out of the database and bash terminal and head to our backend gateway web page. Create a new database connection and choose MariaDB.
[04:07] Then we can name this whatever, let's call it "MariaDB". Let's put the name of the service as the host and that was just "db" and then change "test" to "ignition". And type in the username and passwords which are both "ignition". Once you're done you can click create and this connection should be valid. If we go back and edit the connection, we can also change the host to the network alias that we specified which was "main-db". Once we click save changes that will also be valid. If we added multiple network aliases in our compose file, then we could use any one of them and this would resolve. Well stop the lesson here now that we have a valid database connection to the backend gateway and we'll continue on in the next lesson.