1 00:00:00,000 --> 00:00:11,560 In this video, you'll learn how to run containers in either Docker or Popman. 2 00:00:11,560 --> 00:00:17,000 So the Popman command on Red Hat is fully compatible with the Docker command. 3 00:00:17,000 --> 00:00:21,320 And if you're on Red Hat and you want to follow along exactly with the Docker command, there's 4 00:00:21,320 --> 00:00:25,480 an easy solution, use export alias Docker is Popman. 5 00:00:25,480 --> 00:00:30,000 And unless you are a very advanced user, you won't notice any difference. 6 00:00:30,000 --> 00:00:35,959 To run a container, the basic idea is the command Docker run image, where image refers 7 00:00:35,959 --> 00:00:41,720 to the name of the specific container image that was found on Docker Hub. 8 00:00:41,720 --> 00:00:45,759 Docker Hub is the standard container registry that Docker is using. 9 00:00:45,759 --> 00:00:48,160 Check it out on hub.docker.com. 10 00:00:48,160 --> 00:00:52,360 It has a website that allows you to search for container images. 11 00:00:52,360 --> 00:00:58,639 The run command is commonly used with options minus D, which runs the container in detached mode. 12 00:00:58,639 --> 00:01:03,599 Without minus D, it's going to occupy your screen and you don't see anything else. 13 00:01:03,599 --> 00:01:08,040 Minus IT is commonly used to run a container in interactive terminal mode. 14 00:01:08,040 --> 00:01:13,279 That is what you want to do if you want to run a shell on the container you just started. 15 00:01:13,279 --> 00:01:18,480 And to get an overview of all containers that you just started, you use Docker PS, optionally 16 00:01:18,519 --> 00:01:22,839 with the minus A option, which will also show you containers that have been started in the 17 00:01:22,839 --> 00:01:26,080 past and which are now no longer running. 18 00:01:26,080 --> 00:01:27,559 Let me show you. 19 00:01:27,559 --> 00:01:31,239 So we have already done a Docker run minus D nginx. 20 00:01:31,239 --> 00:01:37,480 And if at this point I'm using Docker PS, then I can see my currently running container. 21 00:01:37,480 --> 00:01:41,040 The container has an ID, which I can use for managing. 22 00:01:41,040 --> 00:01:45,480 It uses standard command, which is normally a script with the name Docker entry point, 23 00:01:45,480 --> 00:01:48,839 which is going to take care of what exactly you want to do. 24 00:01:48,839 --> 00:01:51,239 It's exposing its services on the port. 25 00:01:51,239 --> 00:01:56,400 And it has an automatically generated name that you might want to use for managing. 26 00:01:56,400 --> 00:02:02,760 If I would use Docker run minus IT BusyBox, then we have another example. 27 00:02:02,760 --> 00:02:07,519 I'm using the BusyBox container image, which is a commonly used container image. 28 00:02:07,519 --> 00:02:09,800 It's commonly used because it's so small. 29 00:02:09,800 --> 00:02:14,919 It's a minimal Linux distribution that fits in just a couple of megabytes. 30 00:02:14,919 --> 00:02:17,679 Now here you can see that suddenly I'm in a root shell. 31 00:02:17,679 --> 00:02:22,000 And that is because I'm within the BusyBox container image. 32 00:02:22,000 --> 00:02:29,399 I would advise if you're ever wondering where you are, check out the OS release file. 33 00:02:29,399 --> 00:02:34,360 And oh boy, BusyBox is so small, it doesn't even have an OS release. 34 00:02:34,360 --> 00:02:37,639 OS release is a file that's a Linux Foundation standard. 35 00:02:37,639 --> 00:02:44,399 It should be there on all Linux distributions to identify the distribution where you are. 36 00:02:44,399 --> 00:02:47,559 Now here I'm in the shell that was started by BusyBox. 37 00:02:47,559 --> 00:02:50,800 If I'm going to use exit, I'm exiting that shell. 38 00:02:50,800 --> 00:02:55,479 But then when I'm using Docker PS, I don't see BusyBox anymore. 39 00:02:55,479 --> 00:02:58,080 That's because the shell was the primary command. 40 00:02:58,080 --> 00:03:01,839 And if you exit the container primary command, it's gone. 41 00:03:01,839 --> 00:03:04,919 So now I need Docker PS minus A. 42 00:03:04,919 --> 00:03:11,279 And Docker PS minus A is showing all of them, including containers that were exited. 43 00:03:11,279 --> 00:03:14,119 Now when containers are running, you can manage them. 44 00:03:14,119 --> 00:03:17,399 In order to do so, there's a couple of commands involved. 45 00:03:17,399 --> 00:03:22,639 Starting with Docker inspect, which shows properties of your containers. 46 00:03:22,639 --> 00:03:25,679 Docker stop will stop a running container. 47 00:03:25,679 --> 00:03:30,479 And Docker exec minus T, container name slash bin slash bash, 48 00:03:30,479 --> 00:03:33,559 opens a shell in a currently running container. 49 00:03:33,559 --> 00:03:35,559 And that is useful for debugging. 50 00:03:35,559 --> 00:03:36,919 Let me show you. 51 00:03:36,919 --> 00:03:42,759 So I want to inspect my quirky query, which is the nginx container. 52 00:03:42,759 --> 00:03:48,279 Docker inspect qutab will do it for me. 53 00:03:48,279 --> 00:03:55,240 And here we can see in JSON format everything that is going on in the container. 54 00:03:55,240 --> 00:03:56,919 An important part is the path. 55 00:03:56,919 --> 00:04:01,679 Path is the default application, which was started with these arguments. 56 00:04:01,679 --> 00:04:04,919 And here you can see that it has started nginx. 57 00:04:04,919 --> 00:04:09,600 And if you scroll further down, you can find all the properties of the container. 58 00:04:09,600 --> 00:04:15,080 And that is really useful if your container isn't doing what you want it to do. 59 00:04:15,080 --> 00:04:19,320 Now this quirky query container is running in the background. 60 00:04:19,320 --> 00:04:26,839 And I can connect to it using Docker exec minus IT for the interactive terminal on a quirky query. 61 00:04:26,839 --> 00:04:31,399 And I'm going to run slash bin slash bash. 62 00:04:31,399 --> 00:04:35,720 Now one thing you should notice, not all containers have bin bash on board. 63 00:04:35,720 --> 00:04:39,440 Because containers are often used as minimal environments. 64 00:04:39,440 --> 00:04:45,559 And if it's giving an error message on bin bash, I would advise you use bin sh instead. 65 00:04:45,559 --> 00:04:50,679 So now I'm inside the container, and I can check, for instance, which processes are running. 66 00:04:50,679 --> 00:04:58,799 Well, that doesn't work exactly for the simple reason that, as you can see here, the ps command is not found. 67 00:04:58,799 --> 00:05:00,760 That's the nature of containers. 68 00:05:00,760 --> 00:05:03,119 Containers are minimal environments. 69 00:05:03,119 --> 00:05:09,440 They are images that contain what you need to run your application and nothing else. 70 00:05:09,440 --> 00:05:13,720 So in this case, ps command not found, well, that's not going to work. 71 00:05:13,720 --> 00:05:17,320 But I can use ls to see files in this container. 72 00:05:17,320 --> 00:05:21,000 When I use exit, don't worry, you don't shut the container down. 73 00:05:21,000 --> 00:05:23,920 Look, Docker ps is still showing it. 74 00:05:23,920 --> 00:05:28,959 I used exit on the Docker exec shell that I just started. 75 00:05:28,959 --> 00:05:31,880 And this Docker exec shell was a secondary command. 76 00:05:31,880 --> 00:05:37,040 This doesn't show, so it doesn't shut down the entire container. 77 00:05:37,040 --> 00:05:41,920 If you want to shut down a container, use Docker stop on your container. 78 00:05:41,920 --> 00:05:43,720 And now it will go away. 79 00:05:43,720 --> 00:05:45,519 Let's check that. 80 00:05:45,519 --> 00:05:48,119 Docker ps doesn't show it anymore. 81 00:05:48,119 --> 00:05:50,200 And that's exactly what I wanted. 82 00:05:50,200 --> 00:05:54,160 We also need to talk about accessing container workloads. 83 00:05:54,160 --> 00:05:59,320 The entry point application, that's a standard application that your container is starting. 84 00:05:59,320 --> 00:06:00,880 And that can expose a port. 85 00:06:00,880 --> 00:06:04,239 But this port, by default, is not accessible from the outside, 86 00:06:04,239 --> 00:06:08,760 because network-wise, containers run in an isolated environment. 87 00:06:08,760 --> 00:06:14,119 Only root containers will have an IP address on the isolated container network. 88 00:06:14,119 --> 00:06:21,959 To access a container, you need port forwarding to expose the container port on the host where it is running. 89 00:06:21,959 --> 00:06:27,640 You do that using a command like docker run minus p 8080 colon 80 nginx, 90 00:06:27,640 --> 00:06:34,959 which will run the container based on an nginx image and expose the container port on port 8080. 91 00:06:34,959 --> 00:06:39,079 You cannot expose ports on containers that have already been started. 92 00:06:39,079 --> 00:06:40,320 Let me show you. 93 00:06:40,320 --> 00:06:46,679 So I'm going to use docker run minus p 8080 colon 80 nginx. 94 00:06:46,679 --> 00:06:51,799 Of course, the first port number, that's where you expose your port, needs to be available. 95 00:06:51,799 --> 00:06:54,959 And oops, let me also use minus d. 96 00:06:54,959 --> 00:07:01,839 Notice that the arguments for docker run should always be behind docker run and not behind the container name. 97 00:07:01,839 --> 00:07:05,440 Because if you put the argument behind the name of the container image, 98 00:07:05,440 --> 00:07:12,000 it's going to be interpreted as an argument for the primary command that your container image is going to run. 99 00:07:12,000 --> 00:07:20,040 Good. Now when I use docker ps, I can see that it has 0000 8080 forwarding to port 80, 100 00:07:20,040 --> 00:07:27,239 meaning that if I would use curl to localhost 8080, I'm getting access. 101 00:07:27,239 --> 00:07:29,480 Oh, no curl? Let's fix that. 102 00:07:29,480 --> 00:07:32,799 sudo apt install curl. 103 00:07:32,799 --> 00:07:34,640 And now I'm going to try it again. 104 00:07:34,640 --> 00:07:38,040 curl localhost, and there we can see welcome to nginx. 105 00:07:38,040 --> 00:07:42,279 It doesn't give anything cool, but it does prove that the container is accessible.