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.
In this lesson, we'll take what we've learned so far and translate it to Docker Compose so that we can start up multiple containers at once.
Transcript(open in window)
[00:00] So far, we've looked at creating and running a single container with Ignition. However, what if we wanted to create an application that uses multiple containers, each running an Ignition Gateway or some other process. In this lesson, we'll learn about a new tool, Docker Compose, that will help us do this. In order to use Compose we can use a docker compose file to configure the services that we want to run in our application. You can use whatever you want here, but I'm going to use Visual Studio Code to edit and manage the docker compose file and I'd recommend that you do the same. If you don't already have it downloaded you can click the link in the description. Compose files use YAML, which is a data serialization language that's nice because it's easy to read and write. I'm going to start by saving my file as "docker- compose.yml". In order to learn how to write YAML code, let's look at how we might represent different data types in JSON.
[01:05] In JSON, we would represent a string with quotations around it. A dictionary is represented with a key, a colon, and a value. We would represent a list or an array with brackets and each item separated by commas. And then we can combine these to create more complicated data types. Now, let's look at the equivalents in YAML. For starters, we don't need to put quotation marks around our strings. Our dictionary key value pairs will still be linked by a colon But be sure to include a space after the colon as white space matters. Items in the list are denoted by the same level of indentation and a dash and a space. Then finally we can combine these in order to create the more complex types. I'm going to clean this up and then we can get started on our compose file.
[02:05] Each element in this file will be configured with a key value pair. The first thing we'll need to do is add our services key and then we can start defining the different containers within our application. For this first one. Let's recreate the Ignition container we made in the previous lesson. Let's call the service "gateway1". Let's start by defining the image and the value will simply be a string that string being the image and tag we've used before. Next we can define the list of ports. Then on the same level of indentation as the ports key. We can define our volumes which will be a list. Finally if we want to include runtime arguments like we did in the previous lesson, we can do that with the "command" key. The value here will just be a string. But if we have a lot of commands to add here it might be difficult to read so we can use the greater than symbol to denote a multi-line string.
[03:13] Then we can add each of our commands on a separate line and it automatically knows to ignore the new lines. Let's pass a gateway name of "docker-test" and then we can pass another argument like a max memory of two gigs. We have our first gateway, let's go ahead and add another one by copying and pasting this first block. Then we can change a couple of these settings here to differentiate it. Let's change the service name to "gateway2". We're already going to be using the host ort 9088. So let's change this to 9089, let's add a 2 to the volume name to create a new one, and let's change the gateway name as well by adding a 2. Now we're almost done, but since we're adding volumes, Compose is expecting us to list those out below the services. Let's go ahead and add a volumes key.
[04:04] And this will be the same level of indentation as our services key. And then we'll have a new line for each volume we'll be using for our gateways and be sure to include a colon at the end of each one. Let's save the file and now we can start our application. I'm going to use the terminal that's built into Visual Studio Code. If you're using a different IDE, this may look different or if you're using a simple text editor, you can also enter these commands and PowerShell or your OS's terminal. First make sure that you're in the right directory before you execute these commands. Otherwise, this won't work for you. Now we can use Docker Compose to start up our container. Let's take a look at the "docker compose" command to understand the tool we have at our disposal. We can see here that the "up" command will create and start our containers.
[05:01] We can quickly look over the options it has and see that the "-d" option will let us start up in detached mode. Let's use that to start up the application. Assuming everything goes smoothly, we should see a message that different parts of our application were created and/or started. If we take another look at the list of compose commands, there are a couple other commands we can use to monitor the status of our containers. For example "docker compose ps" will list our containers and "docker compose logs" will list the logs from our containers. Additionally if we wanted to run a command inside one of our containers we can use "exec". For example "docker compose exec gateway1 bash" will execute a bash shell in the "gateway1" container, which can be really valuable. We can type "exit" to leave the container. Let's head over to our web browser and check out our gateways. If you remember we mapped gateway1 to our host port 9088 and gateway2 to 9089.
[06:10] We can go ahead and skip through the commissioning stages here. If you get into situation where it's stuck starting, try reentering localhost and then your port. And as we can see, we have two different gateways running. We have "docker-test" and "docker-test2". We'll finish this part of the course here. We've learned about Docker containers and how to manage their life cycles. We've learned about volumes. We've simultaneously created two gateway containers using Docker Compose. In the next part of this course, we'll look at building out a testing environment with additional services, including a database connection and emails.