1
00:00:00,000 --> 00:00:04,749
[Intro music]

2
00:00:04,749 --> 00:00:06,630
We now look at some of the Docker commands.

3
00:00:06,800 --> 00:00:09,560
At the end of this lecture, you will go through a hands-on

4
00:00:09,570 --> 00:00:12,330
quiz where you will practice working with this command.

5
00:00:13,020 --> 00:00:15,930
Let's start by looking at docker run command.

6
00:00:16,120 --> 00:00:20,116
The docker run command is used to run a Container from an image.

7
00:00:20,237 --> 00:00:24,100
Running the docker run nginx command will run an instance of the

8
00:00:24,110 --> 00:00:28,100
NGINX application on the Docker host if it already exists.

9
00:00:28,110 --> 00:00:32,240
If the image is not present on the host, it will go out to

10
00:00:32,240 --> 00:00:34,380
Docker Hub and pull the image down.

11
00:00:34,390 --> 00:00:36,870
But this is only done the first time.

12
00:00:37,120 --> 00:00:40,960
For the subsequent executions, the same image will be reused.

13
00:00:41,280 --> 00:00:46,080
The docker ps command lists all running Containers and some

14
00:00:46,090 --> 00:00:49,590
basic information about them, such as the Container ID, the

15
00:00:49,600 --> 00:00:52,660
name of the image we use to run the Containers, the current

16
00:00:52,670 --> 00:00:54,490
status, and the name of the Container.

17
00:00:54,940 --> 00:00:59,920
Each Container automatically gets a random ID and name created

18
00:00:59,930 --> 00:01:03,816
for it by Docker, which in this case is silly sammet.

19
00:01:03,816 --> 00:01:09,160
To see all Containers running or not use the -a option.

20
00:01:09,430 --> 00:01:14,149
This outputs all running as well as previously stopped or exited Containers.

21
00:01:14,420 --> 00:01:18,080
We'll talk about the command and Port fields shown in this

22
00:01:18,090 --> 00:01:19,430
output later in this course.

23
00:01:19,640 --> 00:01:24,290
For now, let's just focus on the basic commands. To stop running

24
00:01:24,300 --> 00:01:27,650
Container, use the docker stop command, but you must provide

25
00:01:27,680 --> 00:01:30,740
either the Container ID or the Container name in the stop

26
00:01:30,740 --> 00:01:34,500
command. If you're not sure of the name, run the docker ps

27
00:01:34,510 --> 00:01:38,340
command to get it. On success, you will see the name printed

28
00:01:38,350 --> 00:01:42,300
out and running docker ps again will show no running Containers.

29
00:01:42,620 --> 00:01:47,300
Running docker ps -a, however shows the Container silly_sammet

30
00:01:47,620 --> 00:01:51,760
and that it is exited state a few seconds ago.

31
00:01:52,310 --> 00:01:56,820
Now what if we don't want this Container lying around consuming

32
00:01:56,830 --> 00:01:59,350
space? What if we want to get rid of it for good?

33
00:02:00,160 --> 00:02:04,720
Use the docker rm command to remove a stopped or exited Container

34
00:02:04,940 --> 00:02:08,538
permanently. If it prints the name back, we're good.

35
00:02:08,720 --> 00:02:12,470
Run the docker ps command again to verify that it's no longer

36
00:02:12,479 --> 00:02:14,240
present. Good.

37
00:02:14,390 --> 00:02:18,020
But what about the NGINX image that was downloaded at first?

38
00:02:18,230 --> 00:02:19,700
We're not using that anymore.

39
00:02:19,920 --> 00:02:21,990
So how do we get rid of that image?

40
00:02:22,860 --> 00:02:26,730
But first, how do we see a list of images present on our

41
00:02:26,740 --> 00:02:30,870
host? Run the docker images command to see a list of available

42
00:02:30,870 --> 00:02:33,470
images and their sizes. On our host,

43
00:02:33,500 --> 00:02:37,520
we have four images, NGINX, Redis, Ubuntu, and Alpine.

44
00:02:37,700 --> 00:02:41,450
We will talk about Tags later in this course when we discuss

45
00:02:41,460 --> 00:02:46,490
about images. To remove an image that you no longer plan to

46
00:02:46,500 --> 00:02:49,310
use, run the docker rmi command.

47
00:02:49,600 --> 00:02:53,350
Remember, you must ensure that no Containers are running

48
00:02:53,360 --> 00:02:56,200
off of that image before attempting to remove the image.

49
00:02:56,420 --> 00:03:00,410
You must stop and delete all dependent Containers to be able

50
00:03:00,420 --> 00:03:01,520
to delete an image.

51
00:03:01,840 --> 00:03:06,420
When we ran the docker run command earlier, it downloaded

52
00:03:06,430 --> 00:03:09,630
the Ubuntu image as it couldn't find one locally.

53
00:03:09,820 --> 00:03:13,600
What if we simply want to download the image and keep

54
00:03:13,610 --> 00:03:17,630
so when we run the docker run command, we don't want

55
00:03:17,640 --> 00:03:19,040
to wait for it to download.

56
00:03:19,050 --> 00:03:24,843
Use the docker pull command to only pull the image and not run the Container.

57
00:03:24,870 --> 00:03:28,100
So in this case, the docker pull ubuntu command, pull

58
00:03:28,110 --> 00:03:30,920
the Ubuntu image and stores it on our host.

59
00:03:31,440 --> 00:03:35,560
Let's look at another example, say you were to run a Docker

60
00:03:35,570 --> 00:03:37,180
Container from an Ubuntu image.

61
00:03:37,380 --> 00:03:41,550
When you run the docker run ubuntu command, it runs an instance

62
00:03:41,560 --> 00:03:44,040
of Ubuntu image and exits immediately.

63
00:03:44,260 --> 00:03:47,410
If you were to list the running Containers, you wouldn't

64
00:03:47,420 --> 00:03:48,460
see the Container running.

65
00:03:48,560 --> 00:03:51,440
If you list all Containers, including those that are stopped,

66
00:03:51,560 --> 00:03:55,430
you will see that the new Container you ran, is in an excited

67
00:03:55,490 --> 00:03:57,706
state. Now, why is that?

68
00:03:58,280 --> 00:04:02,670
Unlike virtual machines, Containers are not meant to host

69
00:04:02,680 --> 00:04:03,840
an operating system.

70
00:04:04,060 --> 00:04:08,860
Containers are meant to run a specific task or process, such

71
00:04:08,870 --> 00:04:12,340
as to host an instance of a web server or application server

72
00:04:12,370 --> 00:04:16,130
or a database, or simply to carry some kind of computation

73
00:04:16,140 --> 00:04:17,540
or analysis task.

74
00:04:17,839 --> 00:04:21,890
Once the task is complete, the Container exits.

75
00:04:22,180 --> 00:04:26,950
A Container only lives as long as the process inside it is

76
00:04:26,959 --> 00:04:30,360
alive. If the web service inside the Container is stopped

77
00:04:30,510 --> 00:04:33,040
or crash, then the Container exits.

78
00:04:33,050 --> 00:04:37,180
This is why when you run a Container from an Ubuntu image,

79
00:04:37,400 --> 00:04:38,930
it stops immediately.

80
00:04:38,940 --> 00:04:42,560
Because Ubuntu is just an image of an operating system that

81
00:04:42,570 --> 00:04:45,770
is used as the base image for other applications.

82
00:04:46,040 --> 00:04:49,790
There is no process or application running in it by default.

83
00:04:50,040 --> 00:04:54,330
If the image isn't running any service and as is the case

84
00:04:54,340 --> 00:04:58,930
with Ubuntu, you could instruct Docker to run a process with

85
00:04:58,940 --> 00:05:00,190
the docker run command.

86
00:05:00,200 --> 00:05:04,150
For example, a sleep command with a duration of five seconds.

87
00:05:04,380 --> 00:05:08,220
When the Container starts, it runs the sleep command and

88
00:05:08,230 --> 00:05:10,620
goes into sleep for five seconds,

89
00:05:10,920 --> 00:05:14,340
post which the sleep command exits and the Container stops.

90
00:05:14,660 --> 00:05:18,950
What we just saw was executing a command when we run the

91
00:05:18,960 --> 00:05:21,860
Container. But what if we would like to execute a command

92
00:05:21,870 --> 00:05:23,360
on a running Container?

93
00:05:23,370 --> 00:05:26,910
For example, when I run the docker ps command, I can see

94
00:05:26,920 --> 00:05:30,720
that there is a running Container which uses the Ubuntu image

95
00:05:30,750 --> 00:05:32,550
and sleeps for 100 seconds.

96
00:05:32,840 --> 00:05:36,860
Let's say I would like to see the contents of a file inside

97
00:05:36,870 --> 00:05:38,270
this particular Container.

98
00:05:38,500 --> 00:05:42,610
I could use the docker exec command to execute a command

99
00:05:42,620 --> 00:05:46,660
on my Docker Container, in this case to print the contents

100
00:05:46,930 --> 00:05:49,090
of the /etc/hosts file.

101
00:05:50,040 --> 00:05:53,740
Finally, let's look at one more option before we head over

102
00:05:53,750 --> 00:05:55,030
to the practice exercises.

103
00:05:55,340 --> 00:06:00,311
I'm now going to run a Docker image I developed for a simple web application.

104
00:06:00,420 --> 00:06:04,920
The repository name is kodekloud/simple-webapp.

105
00:06:05,140 --> 00:06:09,190
It runs a simple web server that listens on Port 8080.

106
00:06:09,400 --> 00:06:13,820
When you run a docker run command like this, it runs in the

107
00:06:13,830 --> 00:06:18,070
foreground or in an attached mode, meaning you will be attached

108
00:06:18,080 --> 00:06:21,730
to the console or the standard out of the Docker Container,

109
00:06:21,960 --> 00:06:25,170
and you will see the output of the web service on your screen.

110
00:06:25,460 --> 00:06:29,000
You won't be able to do anything else on this console other

111
00:06:29,010 --> 00:06:31,700
than view the output until this Docker Container stops.

112
00:06:32,020 --> 00:06:34,240
It won't respond to your inputs.

113
00:06:34,960 --> 00:06:39,940
Press the Ctrl + C combination to stop the Container and

114
00:06:39,950 --> 00:06:43,880
the application hosted on the Container exits, and you get

115
00:06:43,890 --> 00:06:45,301
back to your prompt.

116
00:06:46,010 --> 00:06:50,510
Another option is to run the Docker Container in the detached

117
00:06:50,540 --> 00:06:52,820
mode, by providing the -d option.

118
00:06:53,040 --> 00:06:57,360
This will run the Docker Container in the background mode

119
00:06:57,480 --> 00:07:00,120
and you will be back to your prompt immediately.

120
00:07:00,440 --> 00:07:03,320
The Container will continue to run in the back end.

121
00:07:03,460 --> 00:07:06,910
Run the docker ps command to view the running Container.

122
00:07:07,650 --> 00:07:11,500
Now, if you would like to attach back to the running Container

123
00:07:11,510 --> 00:07:16,080
later, run the docker attach command and specify the name

124
00:07:16,110 --> 00:07:17,760
or ID of the Docker Container.

125
00:07:18,020 --> 00:07:22,130
Now, remember, if you're specifying the ID of a Container

126
00:07:22,140 --> 00:07:26,380
in any Docker command, you can simply provide the first few

127
00:07:26,390 --> 00:07:29,830
characters alone, just so it is different from the other

128
00:07:29,840 --> 00:07:31,120
Container IDs on the host.

129
00:07:31,380 --> 00:07:35,688
In this case, I specify a043d.

130
00:07:36,430 --> 00:07:40,848
Now, don't worry about accessing the UI of the web server for now.

131
00:07:41,060 --> 00:07:43,850
We will look more into that in the upcoming lectures.

132
00:07:44,120 --> 00:07:46,670
For now, let's just understand the basic commands.

133
00:07:47,120 --> 00:07:50,300
We'll now get our hands dirty with the Docker CLI.

134
00:07:50,300 --> 00:07:54,796
So let's take a look at how to access the practice lab environments next.

135
00:07:54,796 --> 00:07:59,979
[Outro music]
