1 00:00:06,660 --> 00:00:08,580 - In this lesson, we're going to see how to access 2 00:00:08,580 --> 00:00:11,940 a MySQL relational database in Rust. 3 00:00:11,940 --> 00:00:15,990 We're gonna see how to use the Rust crate for MySQL. 4 00:00:15,990 --> 00:00:19,260 Rust has lots of crates for accessing different kinds 5 00:00:19,260 --> 00:00:22,500 of resource databases, NoSQL databases, 6 00:00:22,500 --> 00:00:24,390 message queues and such like. 7 00:00:24,390 --> 00:00:26,250 So there's a crate for MySQL. 8 00:00:26,250 --> 00:00:27,840 You need to include that in your project 9 00:00:27,840 --> 00:00:29,880 in order to access the MySQL database. 10 00:00:29,880 --> 00:00:32,280 So we'll see how to do that, quite simple. 11 00:00:32,280 --> 00:00:34,020 We'll see how to connect to a database. 12 00:00:34,020 --> 00:00:36,150 There's a connection string you've gotta specify 13 00:00:36,150 --> 00:00:38,160 in order to connect to a MySQL database. 14 00:00:38,160 --> 00:00:40,980 You specify what machine is the database running on, 15 00:00:40,980 --> 00:00:43,230 what port number, things like that, 16 00:00:43,230 --> 00:00:45,810 and then we'll see how to do queries 17 00:00:45,810 --> 00:00:47,820 and how to do inserts and updates 18 00:00:47,820 --> 00:00:49,710 and such like in the database. 19 00:00:49,710 --> 00:00:52,050 So this is where the project's located, 20 00:00:52,050 --> 00:00:53,300 lesson17_database_access. 21 00:00:54,600 --> 00:00:57,813 Let me open up that project in Visual Studio Code. 22 00:00:59,070 --> 00:01:00,600 Okay, so here we are. 23 00:01:00,600 --> 00:01:04,230 The source code is actually quite small, really, 24 00:01:04,230 --> 00:01:06,630 certainly compared to the demo we saw 25 00:01:06,630 --> 00:01:10,260 with the CSV parser in the previous lesson, 26 00:01:10,260 --> 00:01:13,140 and you'll notice I've got a MySQL. 27 00:01:13,140 --> 00:01:14,760 I've got a schema file here 28 00:01:14,760 --> 00:01:18,660 which creates a database structure, tables, 29 00:01:18,660 --> 00:01:20,543 and inserts some data, and I've got a Dockerfile. 30 00:01:22,020 --> 00:01:23,490 Interesting. 31 00:01:23,490 --> 00:01:27,210 So we're gonna run my SQL in a Docker container. 32 00:01:27,210 --> 00:01:30,780 I'm not gonna assume you've already got my SQL installed 33 00:01:30,780 --> 00:01:31,613 on your machine. 34 00:01:31,613 --> 00:01:33,630 You may have. You may not have. 35 00:01:33,630 --> 00:01:35,640 Instead, I'm just going to show you 36 00:01:35,640 --> 00:01:38,520 how to basically run my SQL in a Docker container. 37 00:01:38,520 --> 00:01:41,790 So you'd have to have Docker installed on your machine. 38 00:01:41,790 --> 00:01:43,620 If you don't have it installed, 39 00:01:43,620 --> 00:01:46,440 then if you just go into Chrome, 40 00:01:46,440 --> 00:01:50,070 go into a browser and search for Docker Desktop, 41 00:01:50,070 --> 00:01:53,670 you can install Docker Desktop on Windows, Mac or Linux. 42 00:01:53,670 --> 00:01:55,920 Quite straightforward, takes about five minutes, 43 00:01:55,920 --> 00:01:59,760 and then once you've installed Docker Desktop, start it, 44 00:01:59,760 --> 00:02:01,830 and then you'll be able to run commands 45 00:02:01,830 --> 00:02:05,250 to build images and to run containers. 46 00:02:05,250 --> 00:02:08,850 An image is basically a self-contained application, 47 00:02:08,850 --> 00:02:12,540 and a container is a running instance of that image. 48 00:02:12,540 --> 00:02:14,940 So we'll see how to do this in a moment. 49 00:02:14,940 --> 00:02:18,330 So it's certainly easier using a containerized MySQL 50 00:02:18,330 --> 00:02:21,930 than having to install MySQL directly on your machine. 51 00:02:21,930 --> 00:02:23,190 That's for sure. 52 00:02:23,190 --> 00:02:24,690 So all you need to do is to make sure 53 00:02:24,690 --> 00:02:27,060 you've got Docker Desktop or Docker installed 54 00:02:27,060 --> 00:02:28,620 on your machine, as I was saying. 55 00:02:28,620 --> 00:02:30,570 So in the project, 56 00:02:30,570 --> 00:02:33,090 there were a couple of files we need to look at, 57 00:02:33,090 --> 00:02:36,180 myschema.sql and Dockerfile. 58 00:02:36,180 --> 00:02:37,680 So let's have a look. 59 00:02:37,680 --> 00:02:39,330 So myschema.sql creates a schema. 60 00:02:41,733 --> 00:02:44,047 In MySQL terminology, a schema is like a database, okay? 61 00:02:45,890 --> 00:02:49,320 So this will create a database called MYSCHEMA, 62 00:02:49,320 --> 00:02:52,500 and then in that database, it creates various tables. 63 00:02:52,500 --> 00:02:54,750 We're not gonna use half of this in the demo, 64 00:02:54,750 --> 00:02:56,130 but I just wanted to show you how it'd look 65 00:02:56,130 --> 00:02:58,083 in a realistic situation. 66 00:02:59,190 --> 00:03:01,620 In the MYSCHEMA schema, 67 00:03:01,620 --> 00:03:03,780 I'm gonna create a table called EMPLOYEES, 68 00:03:03,780 --> 00:03:08,070 and an employee has an ID, which is AUTO_INCREMENT 69 00:03:08,070 --> 00:03:10,260 and which is also the PRIMARY KEY. 70 00:03:10,260 --> 00:03:13,140 An employee has a Name, which is a string, 71 00:03:13,140 --> 00:03:15,933 and a Salary and a Region where they work. 72 00:03:16,860 --> 00:03:20,220 I've got an enhanced employees table called EMPLOYEES2 73 00:03:20,220 --> 00:03:23,100 which has all of the stuff above 74 00:03:23,100 --> 00:03:25,700 but it also has an image, okay? 75 00:03:25,700 --> 00:03:28,260 So if you wanted to store binary large object, 76 00:03:28,260 --> 00:03:31,260 then you could put it in there, and it goes on and on. 77 00:03:31,260 --> 00:03:32,880 I've got various other tables 78 00:03:32,880 --> 00:03:35,430 which are kind of academically interesting. 79 00:03:35,430 --> 00:03:39,600 I just wanted it to be a kind of realistic database schema, 80 00:03:39,600 --> 00:03:43,800 and then what I do at the end is I insert some sample data. 81 00:03:43,800 --> 00:03:46,320 So USE MYSCHEMA, in other words, 82 00:03:46,320 --> 00:03:49,470 using that database that we just talked about, 83 00:03:49,470 --> 00:03:54,003 into the EMPLOYEES table, insert some employees, okay? 84 00:03:54,003 --> 00:03:55,200 So I think it's about a dozen 85 00:03:55,200 --> 00:03:57,480 or so employees inserted there, 86 00:03:57,480 --> 00:04:02,480 and I've inserted table data into other tables as well. 87 00:04:02,550 --> 00:04:05,400 All right, so that's myschema.sql. 88 00:04:05,400 --> 00:04:07,170 It's just a regular SQL file. 89 00:04:07,170 --> 00:04:10,080 You would run it in some kind of database tool 90 00:04:10,080 --> 00:04:13,053 to construct a database and to populate these tables. 91 00:04:13,950 --> 00:04:16,530 Okay, so there's nothing particularly, 92 00:04:16,530 --> 00:04:18,810 you know, revelatory there. 93 00:04:18,810 --> 00:04:21,120 The Dockerfile is more interesting. 94 00:04:21,120 --> 00:04:24,810 So I won't go into the theory of Docker now. 95 00:04:24,810 --> 00:04:26,550 Maybe that's another course, 96 00:04:26,550 --> 00:04:30,060 but the idea is you can build your own custom image, 97 00:04:30,060 --> 00:04:34,290 and an image is a self-contained file system, really. 98 00:04:34,290 --> 00:04:36,270 It's almost like a self-contained application. 99 00:04:36,270 --> 00:04:38,040 It contains everything you need to do, 100 00:04:38,040 --> 00:04:39,930 everything you need to run an application 101 00:04:39,930 --> 00:04:42,360 plus all of its dependencies all packaged up 102 00:04:42,360 --> 00:04:45,450 in a single image, completely standalone. 103 00:04:45,450 --> 00:04:47,760 The idea being you don't need to have anything else 104 00:04:47,760 --> 00:04:50,310 pre-installed on the machine apart from Docker. 105 00:04:50,310 --> 00:04:52,800 The image is a completely standalone package, 106 00:04:52,800 --> 00:04:56,850 like a mini virtual machine, but in miniature. 107 00:04:56,850 --> 00:05:01,380 So in a Dockerfile, you have a series of instructions 108 00:05:01,380 --> 00:05:02,790 in blue here. 109 00:05:02,790 --> 00:05:06,000 Each instruction helps to build a file system 110 00:05:06,000 --> 00:05:07,740 for the Docker image. 111 00:05:07,740 --> 00:05:10,800 So the first statement or the first instruction you have 112 00:05:10,800 --> 00:05:13,020 in a Dockerfile, oh, and by the way, these are comments. 113 00:05:13,020 --> 00:05:14,370 You would've guessed. 114 00:05:14,370 --> 00:05:16,830 The first instruction you typically have is FROM, 115 00:05:16,830 --> 00:05:19,260 and that pulls down from the Internet, 116 00:05:19,260 --> 00:05:23,250 there's a standard image on the Internet 117 00:05:23,250 --> 00:05:26,610 called mysql version 8 point something. 118 00:05:26,610 --> 00:05:29,340 When you have an image name, the image has a name, 119 00:05:29,340 --> 00:05:32,760 and after the colon, it also has a version, okay? 120 00:05:32,760 --> 00:05:35,010 So I've specifically pulled down from, 121 00:05:35,010 --> 00:05:38,040 there's an online repository called Docker Hub. 122 00:05:38,040 --> 00:05:42,150 From Docker Hub, I'm pulling down the mysql standard image. 123 00:05:42,150 --> 00:05:45,393 It's MySQL in an image, version 8. 124 00:05:46,230 --> 00:05:48,870 If you don't specify a version number or tag version, 125 00:05:48,870 --> 00:05:50,790 it'll pull down the latest version. 126 00:05:50,790 --> 00:05:54,330 I specifically want to run or pull down the image 127 00:05:54,330 --> 00:05:57,840 for mysql version 8.0.27. 128 00:05:57,840 --> 00:06:01,320 If you go onto, there's a website, hub.docker.com, 129 00:06:01,320 --> 00:06:04,440 hub.docker.com, where you can search for these images. 130 00:06:04,440 --> 00:06:09,390 So that will be downloaded from Docker Hub on the Internet, 131 00:06:09,390 --> 00:06:12,060 and it'll become part of my image, okay? 132 00:06:12,060 --> 00:06:14,913 Think of an image as being like a small virtual machine. 133 00:06:15,990 --> 00:06:20,100 When you run MySQL in a Dockerized container, 134 00:06:20,100 --> 00:06:23,820 every database listens on a particular port number, 135 00:06:23,820 --> 00:06:27,750 and as it happens, MySQL listens on port 3306. 136 00:06:27,750 --> 00:06:30,210 So whenever you have something containerized 137 00:06:30,210 --> 00:06:32,520 listening on a port, what you're meant to do 138 00:06:32,520 --> 00:06:35,010 is to expose that port number, okay? 139 00:06:35,010 --> 00:06:38,100 It's a technical detail that I'd rather not 140 00:06:38,100 --> 00:06:40,050 get into just now, but generally, 141 00:06:40,050 --> 00:06:42,810 whenever you've got containerized applications 142 00:06:42,810 --> 00:06:44,040 that need to listen on a port, 143 00:06:44,040 --> 00:06:46,540 you need to expose that port to the outside world. 144 00:06:47,460 --> 00:06:48,813 Okay, so I've done that. 145 00:06:50,730 --> 00:06:51,570 Right, next thing, 146 00:06:51,570 --> 00:06:54,060 this statement here defines an environment variable. 147 00:06:54,060 --> 00:06:56,460 Inside your Dockerized container, 148 00:06:56,460 --> 00:06:58,050 there will be an environment variable 149 00:06:58,050 --> 00:07:02,910 called MYSQL_ROOT_PASSWORD. 150 00:07:02,910 --> 00:07:05,910 It's the password of the root user. 151 00:07:05,910 --> 00:07:07,830 Now, if you look at the documentation online, 152 00:07:07,830 --> 00:07:11,400 if you go into the Docker Hub, hub.docker.com, 153 00:07:11,400 --> 00:07:14,790 and if you search for MySQL, you'll find, 154 00:07:14,790 --> 00:07:19,590 if you look at the documentation for MySQL, each image, 155 00:07:19,590 --> 00:07:22,530 well, lots of images have certain environment variables 156 00:07:22,530 --> 00:07:24,720 which they expect you to set, 157 00:07:24,720 --> 00:07:27,990 an environment in which the MySQL engine will run, 158 00:07:27,990 --> 00:07:30,240 and if you look at the documentation for MySQL, 159 00:07:30,240 --> 00:07:33,060 it says you must set at least 160 00:07:33,060 --> 00:07:37,550 the MYSQL_ROOT_PASSWORD environment variable, okay? 161 00:07:37,550 --> 00:07:39,033 So I've called it password. 162 00:07:40,140 --> 00:07:43,710 Fair enough, and then the last instruction 163 00:07:43,710 --> 00:07:45,330 is quite interesting. 164 00:07:45,330 --> 00:07:48,900 What I've done now, an image is like a file system actually. 165 00:07:48,900 --> 00:07:51,030 This Docker image that we're trying to build up 166 00:07:51,030 --> 00:07:52,920 is like a miniature file system, 167 00:07:52,920 --> 00:07:54,810 and what I'm doing here is I'm copying, 168 00:07:54,810 --> 00:07:59,490 from my development machine, I'm copying myschema.sql here 169 00:07:59,490 --> 00:08:02,340 into the Docker image particularly, 170 00:08:02,340 --> 00:08:06,630 in particular into this folder in the Docker image, okay? 171 00:08:06,630 --> 00:08:08,550 So the Docker image will have a directory 172 00:08:08,550 --> 00:08:13,170 called docker-entrypoint-initdb.d. 173 00:08:13,170 --> 00:08:15,690 That is a special directory name. 174 00:08:15,690 --> 00:08:19,560 Most relational databases, when they run in a container, 175 00:08:19,560 --> 00:08:22,980 when you start the image, when you run it, 176 00:08:22,980 --> 00:08:26,250 it'll look in this special directory first 177 00:08:26,250 --> 00:08:29,310 looking for schema or looking for SQL files 178 00:08:29,310 --> 00:08:33,510 in order to build the schema of the database 179 00:08:33,510 --> 00:08:35,880 and maybe to populate it with data. 180 00:08:35,880 --> 00:08:40,290 So if you wanted to, you could copy multiple SQL files 181 00:08:40,290 --> 00:08:42,150 into the same directory. 182 00:08:42,150 --> 00:08:46,260 So for example, I could have a file1.sql 183 00:08:46,260 --> 00:08:49,980 which creates the schema, and then I could also copy in, 184 00:08:49,980 --> 00:08:54,980 imagine I've got another file over here called file2.sql 185 00:08:55,380 --> 00:08:58,140 and copy that into the directory as well. 186 00:08:58,140 --> 00:09:01,200 So the Docker image, when it's been built, 187 00:09:01,200 --> 00:09:04,860 will have the mysql base image, which is just the engine, 188 00:09:04,860 --> 00:09:07,620 plus an environment variable, 189 00:09:07,620 --> 00:09:09,630 and it will also run these scripts. 190 00:09:09,630 --> 00:09:11,310 It'll run this script. 191 00:09:11,310 --> 00:09:13,890 Because that script is in the special folder, 192 00:09:13,890 --> 00:09:17,460 it'll run that script to maybe create the schema, 193 00:09:17,460 --> 00:09:19,050 and then it'll run that script 194 00:09:19,050 --> 00:09:22,383 because it's in the special folder to populate the schema. 195 00:09:24,139 --> 00:09:25,740 I wonder what would happen if I specified 196 00:09:25,740 --> 00:09:27,930 these files the other way around. 197 00:09:27,930 --> 00:09:29,340 What do you think would happen there? 198 00:09:29,340 --> 00:09:31,953 Which file would it execute first? 199 00:09:32,850 --> 00:09:36,030 The answer is it does it alphabetically, believe it or not. 200 00:09:36,030 --> 00:09:38,850 So it doesn't matter the order that you copy them 201 00:09:38,850 --> 00:09:40,080 into the directory. 202 00:09:40,080 --> 00:09:41,610 This directory, at the end of the day, 203 00:09:41,610 --> 00:09:44,310 it's just a directory with a couple of files. 204 00:09:44,310 --> 00:09:47,790 So at startup, when you first kind of run the image, 205 00:09:47,790 --> 00:09:51,600 it will execute the first file name 206 00:09:51,600 --> 00:09:53,430 in that directory alphabetically. 207 00:09:53,430 --> 00:09:55,110 So what a lotta developers do, 208 00:09:55,110 --> 00:09:57,570 if you have a lot of SQL files you need to copy in, 209 00:09:57,570 --> 00:10:01,140 they'll name the files something like a 0002 210 00:10:01,140 --> 00:10:06,140 and 0001 just to guarantee that this file 211 00:10:06,150 --> 00:10:08,880 will be executed first, because alphabetically, 212 00:10:08,880 --> 00:10:10,923 numerically, it comes before this one. 213 00:10:11,945 --> 00:10:13,410 There we are. So that's a curiosity. 214 00:10:13,410 --> 00:10:15,860 Let me just revert back to what I had originally. 215 00:10:18,240 --> 00:10:21,610 So this Docker file is like a recipe 216 00:10:22,500 --> 00:10:26,460 that I can give to Docker to build an image 217 00:10:26,460 --> 00:10:29,970 containing the tables described in my schema. 218 00:10:29,970 --> 00:10:33,570 So the next step is to actually build the SQL image 219 00:10:33,570 --> 00:10:37,170 based on the Docker file we just specified. 220 00:10:37,170 --> 00:10:39,690 So what you do is you say docker build, 221 00:10:39,690 --> 00:10:43,080 you can also say docker image build, if you prefer, 222 00:10:43,080 --> 00:10:45,630 minus t, t means tag name. 223 00:10:45,630 --> 00:10:47,610 You give your image a name. 224 00:10:47,610 --> 00:10:50,640 I've called my image mysql-image, okay? 225 00:10:50,640 --> 00:10:53,190 And then you give it a directory. 226 00:10:53,190 --> 00:10:55,470 So what I'm gonna do is I'm going to run that command 227 00:10:55,470 --> 00:10:59,940 from the root folder of my demo project, 228 00:10:59,940 --> 00:11:03,060 and dot basically will be the folder 229 00:11:03,060 --> 00:11:05,790 where your docker file is, right? 230 00:11:05,790 --> 00:11:07,650 So let's do that now, 231 00:11:07,650 --> 00:11:11,490 docker build minus t mysql-image, 232 00:11:11,490 --> 00:11:13,260 and then don't forget the dot. 233 00:11:13,260 --> 00:11:16,533 Okay, so in the root folder of my project, 234 00:11:19,260 --> 00:11:22,113 docker, I could say docker image build, 235 00:11:24,210 --> 00:11:26,130 or I could just say docker build, 236 00:11:26,130 --> 00:11:29,640 so people tend to just say docker build, minus t. 237 00:11:29,640 --> 00:11:32,973 Give my image a tag name of whatever you fancy, 238 00:11:32,973 --> 00:11:37,080 mysql-image, and don't forget the dot. 239 00:11:37,080 --> 00:11:39,030 The dot says in the current folder, 240 00:11:39,030 --> 00:11:41,280 it looked for the Dockerfile, 241 00:11:41,280 --> 00:11:43,380 and it'll basically execute the statements in here 242 00:11:43,380 --> 00:11:45,000 or the instructions, okay? 243 00:11:45,000 --> 00:11:49,620 And it'll grab myschema.sql from the current folder as well. 244 00:11:49,620 --> 00:11:51,780 So let's run that. 245 00:11:51,780 --> 00:11:54,720 It's gonna pull down images from Docker hub. 246 00:11:54,720 --> 00:11:55,710 Okay, so at this point obviously, 247 00:11:55,710 --> 00:11:57,300 you need an Internet connection. 248 00:11:57,300 --> 00:12:00,030 It goes without saying that you need Docker installed, 249 00:12:00,030 --> 00:12:01,980 and that was quite quick actually. 250 00:12:01,980 --> 00:12:04,920 So it has built an image 251 00:12:04,920 --> 00:12:08,280 hopefully called mysql-image. 252 00:12:08,280 --> 00:12:11,700 So to verify that the image has been built successfully, 253 00:12:11,700 --> 00:12:14,610 do a docker image ls, list the images 254 00:12:14,610 --> 00:12:16,323 that now exist on my machine. 255 00:12:17,610 --> 00:12:21,333 Okay, so let's do that then, docker image ls. 256 00:12:22,230 --> 00:12:23,940 I've got lots of images on my machine, 257 00:12:23,940 --> 00:12:27,453 so I'm looking for one called mysql-image. 258 00:12:28,740 --> 00:12:30,483 So if I scroll up to the top, 259 00:12:34,140 --> 00:12:39,123 hopefully here somewhere I've got a mysql-image built. 260 00:12:42,600 --> 00:12:44,250 In fact, I can't see it there. 261 00:12:44,250 --> 00:12:45,840 You can actually be more specific. 262 00:12:45,840 --> 00:12:49,290 You can say docker image ls, and there, 263 00:12:49,290 --> 00:12:50,160 you can actually give it the name 264 00:12:50,160 --> 00:12:53,730 of the image you're looking for, mysql-image, 265 00:12:53,730 --> 00:12:56,793 and then it'll only list the images with that name. 266 00:12:58,110 --> 00:12:59,970 Okay, so it was there. I couldn't find it. 267 00:12:59,970 --> 00:13:01,740 So I've got a repository, 268 00:13:01,740 --> 00:13:05,250 that's just the image name, mysql-image. 269 00:13:05,250 --> 00:13:08,340 So technically speaking, when you build an image, 270 00:13:08,340 --> 00:13:09,900 the name that we specified 271 00:13:09,900 --> 00:13:14,370 when we said docker build minus t mysql.image, 272 00:13:14,370 --> 00:13:16,740 the minus t was the tag name. 273 00:13:16,740 --> 00:13:18,090 It's actually the repository name. 274 00:13:18,090 --> 00:13:20,430 That's the actual name of the image, 275 00:13:20,430 --> 00:13:22,740 and tag here is a version number. 276 00:13:22,740 --> 00:13:24,090 I didn't give it a version number. 277 00:13:24,090 --> 00:13:27,840 I didn't say, like, :0.0.1. 278 00:13:27,840 --> 00:13:31,533 So it'll just give it, like, a pseudo tag number, latest. 279 00:13:32,490 --> 00:13:35,850 Created five weeks ago. Interesting. 280 00:13:35,850 --> 00:13:39,180 So anyway, what I've got so far is I've got an image. 281 00:13:39,180 --> 00:13:42,690 What I wanna do next is to basically run it. 282 00:13:42,690 --> 00:13:45,600 So to run an instance of an image, 283 00:13:45,600 --> 00:13:47,610 that's what a container is, okay? 284 00:13:47,610 --> 00:13:49,830 So you say docker run, 285 00:13:49,830 --> 00:13:52,950 and then you say you give a name to your container. 286 00:13:52,950 --> 00:13:56,070 So a container is a running instance of an image. 287 00:13:56,070 --> 00:14:00,783 So docker run, give my container a name, mysql-container. 288 00:14:02,190 --> 00:14:04,020 The minus d option, obviously you can look up 289 00:14:04,020 --> 00:14:08,550 the documentation on Docker, the Dock docs, in fact. 290 00:14:08,550 --> 00:14:12,690 Minus d means that it'll run the container in detached mode. 291 00:14:12,690 --> 00:14:14,310 It'll run it in a background window 292 00:14:14,310 --> 00:14:16,110 rather than running in the current window. 293 00:14:16,110 --> 00:14:18,560 We don't lose our console window, in other words, 294 00:14:19,590 --> 00:14:21,480 and then the minus p option, 295 00:14:21,480 --> 00:14:24,210 the minus p option is important. 296 00:14:24,210 --> 00:14:27,990 What's happening, inside my docker container, 297 00:14:27,990 --> 00:14:30,840 the MySQL engine will always be listening 298 00:14:30,840 --> 00:14:35,580 on that port number internally kind of inside Docker, 299 00:14:35,580 --> 00:14:36,960 and what you've gotta do is you've gotta map 300 00:14:36,960 --> 00:14:39,960 that internal port number to an external port 301 00:14:39,960 --> 00:14:42,000 on your actual machine, okay? 302 00:14:42,000 --> 00:14:44,190 And that's what the minus p option does. 303 00:14:44,190 --> 00:14:49,190 The minus p option says this port on my machine 304 00:14:49,950 --> 00:14:52,170 will be basically seized by Docker, 305 00:14:52,170 --> 00:14:55,860 and Docker will map it to this port number 306 00:14:55,860 --> 00:14:58,650 inside the container, okay? 307 00:14:58,650 --> 00:15:00,750 So from the outside world, 308 00:15:00,750 --> 00:15:02,640 that's the port number that I'll use, 309 00:15:02,640 --> 00:15:04,470 and that port number's like a wormhole 310 00:15:04,470 --> 00:15:06,783 into this port number inside the container. 311 00:15:06,783 --> 00:15:09,423 It's like a gateway into the container. 312 00:15:10,440 --> 00:15:11,990 Right, well, I need to do that, 313 00:15:13,740 --> 00:15:18,740 so docker run --name, give my container a name, 314 00:15:19,178 --> 00:15:21,933 mysql-container. 315 00:15:23,220 --> 00:15:24,990 Run it in detached mode. 316 00:15:24,990 --> 00:15:27,840 If you didn't say that, then it would just run 317 00:15:27,840 --> 00:15:30,420 and kind of block the current console. 318 00:15:30,420 --> 00:15:32,520 You'd have to then open another console window 319 00:15:32,520 --> 00:15:33,600 to actually do anything. 320 00:15:33,600 --> 00:15:38,190 Run it detached from the console, minus p. 321 00:15:38,190 --> 00:15:41,700 On my machine, port number 3306 will be mapped 322 00:15:41,700 --> 00:15:45,600 to port 3306 inside the container. 323 00:15:45,600 --> 00:15:47,760 So there we go. 324 00:15:47,760 --> 00:15:50,100 Oh, and then we have to say the container 325 00:15:50,100 --> 00:15:52,200 will be an instance of which image exactly. 326 00:15:52,200 --> 00:15:55,980 Oh, mysql image, mysql-image. 327 00:15:55,980 --> 00:15:58,320 So I think that's about it. Let me just double check. 328 00:15:58,320 --> 00:16:02,040 Run a container whose name is going to be that 329 00:16:02,040 --> 00:16:04,590 in detached mode in the background. 330 00:16:04,590 --> 00:16:07,050 Map this port number on my machine 331 00:16:07,050 --> 00:16:10,020 to that number inside the container, and by the way, 332 00:16:10,020 --> 00:16:13,320 the container will be an instance of that image. 333 00:16:13,320 --> 00:16:15,243 Well, that looks okay to me. 334 00:16:16,290 --> 00:16:17,740 We'll see. Let's have a look. 335 00:16:20,910 --> 00:16:24,570 Ah, now then, errors. These things do happen. 336 00:16:24,570 --> 00:16:29,280 It says I already have a container named mysql-container. 337 00:16:29,280 --> 00:16:32,340 Well, okay, so in that case, what you need to do 338 00:16:32,340 --> 00:16:34,920 is you need to stop that other container. 339 00:16:34,920 --> 00:16:39,880 I can say docker container stop 340 00:16:42,252 --> 00:16:45,330 mysql-container. 341 00:16:45,330 --> 00:16:48,753 Stop that other container that's currently running, it says. 342 00:16:51,720 --> 00:16:55,080 Okay, so it stopped, and then I can remove that container 343 00:16:55,080 --> 00:16:58,233 to get rid of it from my Docker registry, 344 00:16:59,280 --> 00:17:01,514 docker container rm. 345 00:17:01,514 --> 00:17:04,798 You stop the container first, and then you remove it, 346 00:17:04,798 --> 00:17:07,473 mysql-container. 347 00:17:10,200 --> 00:17:12,360 Rightio, so let's just do a quick, 348 00:17:12,360 --> 00:17:16,473 you can say docker container ls. 349 00:17:18,660 --> 00:17:21,213 Let's see what containers I've got at the moment. 350 00:17:22,320 --> 00:17:24,810 Right, so it was going to list all the containers 351 00:17:24,810 --> 00:17:28,530 that I have at the moment, and there aren't any. 352 00:17:28,530 --> 00:17:30,270 So that means I should now be able 353 00:17:30,270 --> 00:17:32,190 to actually start the container, 354 00:17:32,190 --> 00:17:34,083 like I hoped to a moment ago. 355 00:17:35,550 --> 00:17:38,160 Okay then, so docker run. 356 00:17:38,160 --> 00:17:41,520 Run a container whose name will be that, 357 00:17:41,520 --> 00:17:43,770 an instance of that image. 358 00:17:43,770 --> 00:17:47,103 Run it in detached mode with that port mapping. 359 00:17:48,210 --> 00:17:50,493 Right. Okay, let's see if that works. 360 00:17:53,490 --> 00:17:56,010 Right, well, it hasn't given me an error message. 361 00:17:56,010 --> 00:17:59,400 So in order to check that it's actually running, 362 00:17:59,400 --> 00:18:04,230 you can say docker container ls. 363 00:18:04,230 --> 00:18:06,600 In fact, when you say docker container ls, 364 00:18:06,600 --> 00:18:09,390 it only lists the containers which are actually running. 365 00:18:09,390 --> 00:18:13,170 Sometimes a container starts and then it errors 366 00:18:13,170 --> 00:18:15,960 or it starts and then it completes. 367 00:18:15,960 --> 00:18:17,910 Docker container ls would only list the ones 368 00:18:17,910 --> 00:18:20,010 which are actually running at the moment. 369 00:18:20,010 --> 00:18:23,100 If you say minus a, then it'll list all containers, 370 00:18:23,100 --> 00:18:25,500 even the ones which have stopped, okay, 371 00:18:25,500 --> 00:18:28,140 that have terminated maybe or have ended, 372 00:18:28,140 --> 00:18:29,310 so that's probably more useful. 373 00:18:29,310 --> 00:18:30,993 List all containers, please. 374 00:18:32,880 --> 00:18:35,040 Okay, so, oh, interesting. 375 00:18:35,040 --> 00:18:36,300 Here, I've got a few other containers 376 00:18:36,300 --> 00:18:37,503 that I wasn't expecting. 377 00:18:38,610 --> 00:18:39,720 That's, oh, I know what that is. 378 00:18:39,720 --> 00:18:41,673 That's from my Spring Boot course. 379 00:18:42,660 --> 00:18:46,020 Very good, but this is the one of interest. 380 00:18:46,020 --> 00:18:48,060 You can have as many containers running as you want. 381 00:18:48,060 --> 00:18:50,250 You typically have lots and lots of containers running. 382 00:18:50,250 --> 00:18:52,500 I've got a container. It's spread over a couple of lines. 383 00:18:52,500 --> 00:18:53,820 I wonder if this would look easier 384 00:18:53,820 --> 00:18:55,230 if I collapse it down a bit. 385 00:18:55,230 --> 00:18:56,370 Oh, that's better, isn't it? 386 00:18:56,370 --> 00:18:59,943 You can see each container has a container ID, 387 00:19:00,870 --> 00:19:03,570 and the name that we've given it, oh that's the image. 388 00:19:03,570 --> 00:19:05,940 It's gonna be an instance of that image. 389 00:19:05,940 --> 00:19:07,860 That's the command that would be executed 390 00:19:07,860 --> 00:19:10,080 when the container was executed. 391 00:19:10,080 --> 00:19:13,110 Created 41 seconds ago. How time flies. 392 00:19:13,110 --> 00:19:16,620 It's still up. It hasn't crashed and burned. 393 00:19:16,620 --> 00:19:20,190 Oh, there's a port mapping, 3306 is mapped, 394 00:19:20,190 --> 00:19:23,760 and that's the name of my container, 395 00:19:23,760 --> 00:19:27,930 and it's an instance of this image over here. 396 00:19:27,930 --> 00:19:29,910 So that looks fine. 397 00:19:29,910 --> 00:19:34,500 I've got a MySQL database running in a container on 3306. 398 00:19:35,460 --> 00:19:38,970 So that means we can then press ahead 399 00:19:38,970 --> 00:19:40,770 and have a look at our Rust application 400 00:19:40,770 --> 00:19:42,690 which is gonna talk to that database. 401 00:19:42,690 --> 00:19:44,190 It's quite exciting, isn't it?