1 00:00:06,519 --> 00:00:09,929 Now that you know a bit about running containers, how do we manage them? 2 00:00:11,009 --> 00:00:14,544 Well, first you need to make sure that you provide Docker run 3 00:00:14,544 --> 00:00:18,079 arguments behind run and before the name of the container image. 4 00:00:18,640 --> 00:00:23,024 So if you want to do anything specific to Docker run, the arguments must 5 00:00:23,024 --> 00:00:27,409 follow Docker run directly and cannot be behind the name of the container image. 6 00:00:28,329 --> 00:00:31,510 One thing that you might want to do is to make containers accessible. 7 00:00:32,509 --> 00:00:37,284 Even if my nginx container has been started on port 80, that's 8 00:00:37,284 --> 00:00:42,060 port 80 within the container, but that's not reachable on the host. 9 00:00:42,659 --> 00:00:46,359 If you want to access your containers, you need to configure port forwarding. 10 00:00:47,060 --> 00:00:53,369 This is how you would do it. Docker run d to run it in the background P8080 to make it accessible on port 8080 externally. 11 00:00:57,990 --> 00:01:02,310 So the first port is support on the host. The second port is port within the container. 12 00:01:03,250 --> 00:01:06,829 Also important is the option to provide persistent storage to containers. 13 00:01:07,430 --> 00:01:11,495 That is happening by creating a bind mount and you do so using 14 00:01:11,495 --> 00:01:15,560 docker run D P8080 80v web var libhtml followed by the container image. 15 00:01:24,159 --> 00:01:27,200 The interesting part here is the part where you are using the bind mount. 16 00:01:28,060 --> 00:01:30,939 V is followed by the name of a directory on the host. 17 00:01:31,459 --> 00:01:36,890 And after that we have varlib HTML that is the name of the directory within the container. 18 00:01:37,609 --> 00:01:44,039 You should also know that if you are on an selinux system, and that goes for all 19 00:01:44,039 --> 00:01:50,469 in the red hat family, some more configuration is required. In fact, it's not very complicated. 20 00:01:51,069 --> 00:01:55,699 You put colon z behind the bind mount and then you should be okay. If 21 00:01:55,699 --> 00:02:00,329 you want to provide environment variables to container applications, you can use the env argument. 22 00:02:03,000 --> 00:02:06,560 So you use Docker run d n MariaDB root 23 00:02:06,560 --> 00:02:10,120 password is password to start mariadb for instance. 24 00:02:10,659 --> 00:02:16,294 Now, sometimes it's not going right when you try to start a container. So if a 25 00:02:16,294 --> 00:02:21,929 container fails to start, it doesn't show in the Docker PS output. You can use Docker 26 00:02:21,930 --> 00:02:27,564 PS a instead of and to find out why a container has failed to start. You 27 00:02:27,564 --> 00:02:33,199 use Docker log's container name to investigate parameters that are used for starting the container. 28 00:02:33,840 --> 00:02:38,300 You can use Docker inspect on either the container name or the image. Let me show you. 29 00:02:39,460 --> 00:02:45,387 So let me get started with a docker run D P8080 for nginx 30 00:02:45,387 --> 00:02:51,314 and then I can use docker ps to verify if it is listening. 31 00:02:51,314 --> 00:02:57,242 And there we can see that it is. Here the automatically generated name 32 00:02:57,242 --> 00:03:03,170 is modest gates and it is accessible on localhost port 8080. 33 00:03:04,289 --> 00:03:08,599 So if I would use curl to localhost 8080 34 00:03:08,599 --> 00:03:12,909 that is forwarding the traffic to the container. 35 00:03:13,449 --> 00:03:20,219 Oh, then we do need curl. Well let's use sudo apt install curl curl between APT and snap. I would always 36 00:03:20,219 --> 00:03:26,990 go for the APT packages because they are faster and if it's command line only, just use it. So here we 37 00:03:26,989 --> 00:03:33,759 have the response that I'm getting from my curl command and as you can see it's welcome to nginx. 38 00:03:34,159 --> 00:03:35,379 So that should be good enough. 39 00:03:36,020 --> 00:03:41,382 Next I'm going to run the MariaDB container. So docker run D MariaDB and 40 00:03:41,382 --> 00:03:46,745 let's just try to run it and figure out what is happening. Well the 41 00:03:46,745 --> 00:03:52,107 first thing that is happening is that it can't find the MariaDB image locally, 42 00:03:52,107 --> 00:03:57,470 so it needs to fetch it before it can start it. 43 00:03:58,629 --> 00:04:02,590 Now it has done so. So how about Docker ps? 44 00:04:03,129 --> 00:04:08,546 Well, Docker PS is showing a lot but no mariadb. Uh oh. So we need to 45 00:04:08,546 --> 00:04:13,963 do some troubleshooting. Docker PS A and there we can see an overview of all the 46 00:04:13,963 --> 00:04:19,379 containers that are currently running and also containers that have failed to get started. 47 00:04:19,639 --> 00:04:25,699 So I can see this affectionate banzai and I want to know what is going on with this container. 48 00:04:26,259 --> 00:04:28,279 And that is where Docker logs comes in. 49 00:04:28,800 --> 00:04:34,784 So Docker logs is followed by the name or if you want the ID of the container and that 50 00:04:34,784 --> 00:04:40,769 will give you output to the errors that were generated while starting the primary application in the container. 51 00:04:41,810 --> 00:04:46,759 And what you see here is that the database is uninitialized and the password option 52 00:04:46,759 --> 00:04:51,709 is not specified and you need to specify of mariad root password and much more. 53 00:04:52,709 --> 00:04:58,169 So let's do this last command again. Docker run d 54 00:04:58,169 --> 00:05:03,629 ENV mariadb root password is password followed by mariadb and 55 00:05:03,629 --> 00:05:09,090 then when I use Docker PS then I can see 56 00:05:09,090 --> 00:05:14,550 that my mariadb is up and running this time. 57 00:05:15,540 --> 00:05:20,350 Now if you are starting an application like MariaDB then probably you 58 00:05:20,350 --> 00:05:25,160 also want your MariaDB to be able to store data persistently. 59 00:05:25,959 --> 00:05:29,319 The problem is that containers by default have non persistent data. 60 00:05:29,879 --> 00:05:35,230 And that means that everything you write in the container is written inside the container 61 00:05:35,230 --> 00:05:40,580 and if ever the container is deleted, all your data is gone. The simple solution 62 00:05:40,579 --> 00:05:45,930 to fix that is by using the bind mount. So I'm going to create directories. 63 00:05:47,290 --> 00:05:54,275 Let me create it in my home directory and I'm calling it web and 64 00:05:54,275 --> 00:06:01,260 I'm going to run another MariaDB container using this bind mount option V home 65 00:06:01,259 --> 00:06:08,245 student web and I'm going to mount that to varlib MYSQL because varlib mysql 66 00:06:08,245 --> 00:06:15,230 that is where inside the container mariadb would normally create the the database. 67 00:06:16,069 --> 00:06:21,019 So when the mariadb container is going to write internally to varlib mysql 68 00:06:21,019 --> 00:06:25,970 it is going to end up in this home student web directory. 69 00:06:27,290 --> 00:06:32,696 So let's use Docker PS to figure out if that worked all right. And that seems to 70 00:06:32,696 --> 00:06:38,103 be working all right. It is up and running, and as a result, if I check the 71 00:06:38,103 --> 00:06:43,509 contents of this web directory, you can see that in fact MariaDB data is available here. 72 00:06:44,439 --> 00:06:51,040 Now, the nice thing about this is that if ever the container gets deleted, your data will exist persistently. 73 00:06:51,819 --> 00:06:54,980 And that concludes our short introduction to containers. 74 00:06:55,819 --> 00:07:00,490 Check out my Getting Started with Containers Recorded Video Course if you want to know more.