1
00:00:02,380 --> 00:00:04,320
Now, there is more we can do

2
00:00:04,320 --> 00:00:08,840
with the docker command than with containers and images.

3
00:00:08,840 --> 00:00:13,300
We already saw a couple of different commands and action

4
00:00:13,300 --> 00:00:18,110
and I wanna come back to this mode which we have here

5
00:00:18,110 --> 00:00:21,810
when we restarted a container and the mode which we had

6
00:00:21,810 --> 00:00:24,043
when we initially started a container.

7
00:00:24,950 --> 00:00:26,930
Here, when we're restarting it

8
00:00:26,930 --> 00:00:31,930
the process in our terminal here finished immediately.

9
00:00:32,150 --> 00:00:35,777
We're not attached to this running docker container

10
00:00:35,777 --> 00:00:38,610
in the terminal anymore.

11
00:00:38,610 --> 00:00:42,100
Nonetheless, it clearly is still running

12
00:00:42,100 --> 00:00:44,530
as we can see with docker ps.

13
00:00:44,530 --> 00:00:48,710
So, this container is running, it's just not blocking us

14
00:00:48,710 --> 00:00:52,330
here in the terminal and that's different compared

15
00:00:52,330 --> 00:00:55,950
to the docker run command we executed before.

16
00:00:55,950 --> 00:01:00,090
If I run this again, binding to a different local port

17
00:01:00,090 --> 00:01:04,260
since 3000 is already used by the already running container

18
00:01:04,260 --> 00:01:06,030
but if I use a different port here

19
00:01:06,030 --> 00:01:08,850
I'm starting a new additional container

20
00:01:08,850 --> 00:01:13,680
based on the same image, so, therefore with docker ps

21
00:01:13,680 --> 00:01:15,470
in a different terminal here

22
00:01:15,470 --> 00:01:18,140
we see we got two containers up and running now

23
00:01:20,260 --> 00:01:23,650
but you see that for docker run, we're stuck

24
00:01:23,650 --> 00:01:27,080
in this process, I can't enter more commands here.

25
00:01:27,080 --> 00:01:29,830
I mean, I can type but I can't commit them.

26
00:01:29,830 --> 00:01:32,260
I can't confirm them with enter

27
00:01:32,260 --> 00:01:35,570
instead, this process is blocking this terminal

28
00:01:36,470 --> 00:01:39,700
and with docker start that was not the case.

29
00:01:39,700 --> 00:01:43,500
Now, this is not a blog or anything you have to accept

30
00:01:43,500 --> 00:01:47,440
this is something you can configure if you wanna be

31
00:01:47,440 --> 00:01:51,683
in this attached mode or in detached mode.

32
00:01:52,540 --> 00:01:56,570
And it's just a case that for starting with docker start

33
00:01:56,570 --> 00:02:00,840
the detached mode is the default for running with docker run

34
00:02:00,840 --> 00:02:03,760
the attached mode is the default.

35
00:02:03,760 --> 00:02:07,330
And now the question of course is which mode do you want

36
00:02:07,330 --> 00:02:09,539
and why does this even matter?

37
00:02:09,539 --> 00:02:12,880
Well, in this example application, I am logging

38
00:02:12,880 --> 00:02:16,923
something to the console whenever we set a new goal.

39
00:02:18,380 --> 00:02:23,380
So, here if I add, Learn Docker in-depth! as a goal

40
00:02:23,660 --> 00:02:27,680
on local host 3000, that's my started container

41
00:02:27,680 --> 00:02:32,680
which was detached, we see nothing in the console anywhere.

42
00:02:35,050 --> 00:02:38,460
On the other hand, if I go to local host 8,000

43
00:02:38,460 --> 00:02:42,710
which is that new container, we run with docker run

44
00:02:42,710 --> 00:02:46,180
and I set my goal here

45
00:02:48,050 --> 00:02:52,470
you will see that it also shows up into terminal here

46
00:02:52,470 --> 00:02:56,100
because here we're attached to this running container

47
00:02:56,100 --> 00:03:00,210
and attached simply means that we're listening to the output

48
00:03:00,210 --> 00:03:01,370
of that container.

49
00:03:01,370 --> 00:03:04,970
For example, to what's being printed to the console

50
00:03:05,850 --> 00:03:09,560
and this might or might not be what we want.

51
00:03:09,560 --> 00:03:12,350
We can also run a container in detached mode

52
00:03:12,350 --> 00:03:15,670
if we want to, we don't have to accept that default

53
00:03:15,670 --> 00:03:17,270
of being attached.

54
00:03:17,270 --> 00:03:20,800
For this, I'll quickly stop this more recent container

55
00:03:20,800 --> 00:03:23,240
which I started with docker stop

56
00:03:23,240 --> 00:03:25,420
which will take a couple of seconds

57
00:03:25,420 --> 00:03:28,480
so, that's the container we started with docker run

58
00:03:29,490 --> 00:03:32,940
and once this is stopped, I will re-run it

59
00:03:32,940 --> 00:03:37,940
but now in detached mode by adding -d as an extra flag

60
00:03:38,060 --> 00:03:39,740
in front of the image ID here

61
00:03:41,020 --> 00:03:44,700
if, I now enter it you see, now I just get the ID

62
00:03:44,700 --> 00:03:48,300
the automatically generated ID of the new container

63
00:03:48,300 --> 00:03:52,040
you see with docker ps that we have this container up

64
00:03:52,040 --> 00:03:56,170
and running that still shortened ID off that up

65
00:03:56,170 --> 00:03:58,050
and running container by the way

66
00:03:58,050 --> 00:03:59,880
and we see that container is running

67
00:03:59,880 --> 00:04:04,010
and therefor visiting local host 8,000 also works.

68
00:04:04,010 --> 00:04:07,890
But now if I Learn Docker in-depth!

69
00:04:07,890 --> 00:04:10,990
for example, here on local host 8,000

70
00:04:12,090 --> 00:04:14,520
we don't see that log into terminal

71
00:04:14,520 --> 00:04:17,149
where we execute a docker run

72
00:04:17,149 --> 00:04:19,959
because now we're not attached to the output

73
00:04:19,959 --> 00:04:24,960
since we used the -d flag to start in detach mode.

74
00:04:25,060 --> 00:04:27,520
The advantages that we now can use one

75
00:04:27,520 --> 00:04:30,920
and the same terminal to also do other things.

76
00:04:30,920 --> 00:04:34,550
If you still wanna look into your running container

77
00:04:34,550 --> 00:04:36,990
you got two ways of doing that.

78
00:04:36,990 --> 00:04:39,920
For one, we can see with docker help

79
00:04:41,730 --> 00:04:44,830
that there is an attached command.

80
00:04:44,830 --> 00:04:48,870
So, we can attach ourselves again to a running container.

81
00:04:48,870 --> 00:04:52,870
We can simply find the container and use its name

82
00:04:52,870 --> 00:04:57,720
or its ID both works and then run docker attach

83
00:04:57,720 --> 00:05:01,280
and that name and we are attached again

84
00:05:01,280 --> 00:05:04,830
and therefore now, if we change it again

85
00:05:07,660 --> 00:05:12,463
and set a new goal, we do see this output again.

86
00:05:13,560 --> 00:05:15,140
So, that's one way of doing it

87
00:05:16,140 --> 00:05:19,590
but actually let me stop this container again

88
00:05:22,110 --> 00:05:26,600
and let me restart it thereafter

89
00:05:26,600 --> 00:05:28,630
now in detached mode automatically

90
00:05:28,630 --> 00:05:31,340
since that is the default for docker start

91
00:05:31,340 --> 00:05:34,100
and let me show you another way of getting access to

92
00:05:34,100 --> 00:05:37,580
for example, the log messages that are printed inside

93
00:05:37,580 --> 00:05:38,423
of a container.

94
00:05:39,650 --> 00:05:40,950
This is running again.

95
00:05:40,950 --> 00:05:43,900
If I now type Learn Docker in-depth! here

96
00:05:43,900 --> 00:05:47,370
and commit this, so I save this and set this goal

97
00:05:47,370 --> 00:05:49,540
we don't see the goal because we are detached

98
00:05:49,540 --> 00:05:53,750
and we could attach again to see future log messages

99
00:05:53,750 --> 00:05:57,970
or another thing we can do another useful command

100
00:05:57,970 --> 00:06:02,180
is the docker logs command which fetches the logs

101
00:06:02,180 --> 00:06:04,193
that were printed by a container.

102
00:06:05,400 --> 00:06:09,440
And here we can first of all, find the container

103
00:06:09,440 --> 00:06:11,880
eloquent_brown is the name of the container

104
00:06:11,880 --> 00:06:13,390
I'm interested here

105
00:06:13,390 --> 00:06:16,380
and then we can run docker logs on that container

106
00:06:16,380 --> 00:06:19,620
and we see the past logs that were printed

107
00:06:19,620 --> 00:06:21,760
by that container.

108
00:06:21,760 --> 00:06:26,760
We can also, if we inspect the options of docker logs

109
00:06:27,100 --> 00:06:32,100
enter follow mode by adding -f to keep on listening.

110
00:06:32,730 --> 00:06:35,510
So, basically we're now attaching ourselves again.

111
00:06:35,510 --> 00:06:38,230
So, if I add -f here, we again get

112
00:06:38,230 --> 00:06:42,330
this attached process where we see future logs

113
00:06:42,330 --> 00:06:45,363
future output by that container again.

114
00:06:46,350 --> 00:06:48,880
So, these are the different options we have for that.

115
00:06:48,880 --> 00:06:51,520
And I'm spending a lot of time on that

116
00:06:51,520 --> 00:06:54,230
because it is important that you understand

117
00:06:54,230 --> 00:06:57,450
the difference between a running and a stopped container

118
00:06:57,450 --> 00:07:00,610
and an attached and a detached container.

119
00:07:00,610 --> 00:07:02,840
No matter if it's attached or detached

120
00:07:02,840 --> 00:07:06,300
it's still up and running but if you then need information

121
00:07:06,300 --> 00:07:09,620
from inside the container, you can use docker logs

122
00:07:09,620 --> 00:07:12,370
or attach yourself to the container again

123
00:07:12,370 --> 00:07:14,463
to get that extra information.

124
00:07:16,870 --> 00:07:19,530
Now ,with that I will again

125
00:07:21,620 --> 00:07:23,800
docker stop eloquent_brown

126
00:07:24,690 --> 00:07:28,833
to bring down that container and detach myself again here.

127
00:07:29,700 --> 00:07:34,040
Now, one last note about all of that, if you would want

128
00:07:34,040 --> 00:07:38,370
to restart a stopped container like eloquent_brown here

129
00:07:39,390 --> 00:07:42,010
in attached mode, right from the start

130
00:07:42,010 --> 00:07:46,370
you can do, so by adding the -a flag to the start command.

131
00:07:46,370 --> 00:07:48,843
Now, you start this in attached mode.

