1
00:00:02,120 --> 00:00:03,440
In the last lecture

2
00:00:03,440 --> 00:00:06,140
we saw how we can inspect an image.

3
00:00:06,140 --> 00:00:07,840
Okay that's nice to have

4
00:00:07,840 --> 00:00:11,290
but what if you want to look into a container

5
00:00:11,290 --> 00:00:13,520
or maybe not just look into it

6
00:00:13,520 --> 00:00:17,580
but add something to it or extract something from it

7
00:00:17,580 --> 00:00:20,060
whilst it is already running?

8
00:00:20,060 --> 00:00:21,477
Docker has a command for this

9
00:00:21,477 --> 00:00:25,520
and that's the CP command which stands for copy.

10
00:00:25,520 --> 00:00:28,430
It allows you to copy files or folders

11
00:00:28,430 --> 00:00:32,700
into a running container or out of a running container.

12
00:00:32,700 --> 00:00:34,650
And here's how you could use this.

13
00:00:34,650 --> 00:00:37,330
Let's say we have a folder named Dummy here

14
00:00:37,330 --> 00:00:39,260
with a test text file

15
00:00:39,260 --> 00:00:41,540
where I simply say, hello.

16
00:00:41,540 --> 00:00:44,130
This is on my local host project folder

17
00:00:44,130 --> 00:00:46,500
not inside of the container.

18
00:00:46,500 --> 00:00:49,850
Now we want to copy that into an already running container.

19
00:00:49,850 --> 00:00:52,640
And for this, I already started a container here

20
00:00:52,640 --> 00:00:54,130
which has this name.

21
00:00:54,130 --> 00:00:56,690
So we can now run Docker copy

22
00:00:56,690 --> 00:00:59,560
and first of all specify the source.

23
00:00:59,560 --> 00:01:03,230
So the folder or file we want to copy somewhere else.

24
00:01:03,230 --> 00:01:06,090
In this case, I want to copy the Dummy folder

25
00:01:06,090 --> 00:01:09,770
and now you could either point at a specific file

26
00:01:09,770 --> 00:01:13,500
or add a dot to copy everything in that folder

27
00:01:13,500 --> 00:01:14,833
which is what I will do.

28
00:01:15,760 --> 00:01:18,810
Then you add a blank and then the destination

29
00:01:18,810 --> 00:01:20,520
of this copy operation

30
00:01:20,520 --> 00:01:22,520
so the path you want to copy to.

31
00:01:22,520 --> 00:01:25,133
And data would be inside of your container.

32
00:01:26,020 --> 00:01:27,990
For this you need the container name

33
00:01:27,990 --> 00:01:30,000
because Docker of course needs to know

34
00:01:30,000 --> 00:01:32,330
which container to copy this into.

35
00:01:32,330 --> 00:01:35,530
You could have multiple running containers after all

36
00:01:35,530 --> 00:01:39,260
so you specify your container name then a colon

37
00:01:39,260 --> 00:01:42,060
and then the path inside of the container

38
00:01:42,060 --> 00:01:43,670
you want a copy to.

39
00:01:43,670 --> 00:01:46,850
Let's say Test but this name is up to you.

40
00:01:46,850 --> 00:01:49,953
This path will be created if it doesn't exist yet.

41
00:01:51,180 --> 00:01:53,690
If I now hit enter,

42
00:01:53,690 --> 00:01:56,700
the content of Dummy should have been copied

43
00:01:56,700 --> 00:02:00,210
into the Test folder inside of the container.

44
00:02:00,210 --> 00:02:02,980
Okay, but how can we now see it?

45
00:02:02,980 --> 00:02:07,980
Well, let's delete test text on our local host machine

46
00:02:08,020 --> 00:02:11,500
and let's run this command again but differently now.

47
00:02:11,500 --> 00:02:15,840
Now the first argument is the path and the container

48
00:02:15,840 --> 00:02:19,290
and the second argument is our local folder.

49
00:02:19,290 --> 00:02:23,130
So the destination is now our local folder.

50
00:02:23,130 --> 00:02:27,480
If I now hit enter you see this test folder

51
00:02:27,480 --> 00:02:29,520
which was created in the container

52
00:02:29,520 --> 00:02:31,790
was now copied into Dummy.

53
00:02:31,790 --> 00:02:35,700
And the test text file in there is all the back.

54
00:02:35,700 --> 00:02:36,770
And if we would have wanted

55
00:02:36,770 --> 00:02:41,490
to target this file specifically we could of course done so.

56
00:02:41,490 --> 00:02:43,790
We can also run the command like this

57
00:02:43,790 --> 00:02:47,640
and now it copies just the text file into the Dummy folder

58
00:02:47,640 --> 00:02:49,290
without the Test folder.

59
00:02:49,290 --> 00:02:52,980
This folder is only there because of the previous command,

60
00:02:52,980 --> 00:02:54,320
this command.

61
00:02:54,320 --> 00:02:56,220
And that's how you can copy files

62
00:02:56,220 --> 00:03:00,120
between your local host machine and running containers.

63
00:03:00,120 --> 00:03:03,920
Now this command can be useful for a couple of things.

64
00:03:03,920 --> 00:03:07,990
It of course would allow you to add something to a container

65
00:03:07,990 --> 00:03:11,670
without restarting the container and rebuilding the image.

66
00:03:11,670 --> 00:03:14,550
Let's say our source code changed.

67
00:03:14,550 --> 00:03:17,900
Normally we need to rebuild the image because of that

68
00:03:17,900 --> 00:03:19,653
and restart the container.

69
00:03:20,500 --> 00:03:23,530
We could of course also just copy the changed code

70
00:03:23,530 --> 00:03:25,020
into the container.

71
00:03:25,020 --> 00:03:27,080
Typically that is not what you do though

72
00:03:27,080 --> 00:03:29,130
because it's pretty error prone.

73
00:03:29,130 --> 00:03:32,160
It would be easy to forget a file which you changed

74
00:03:32,160 --> 00:03:34,650
and you might then have a strange behavior

75
00:03:34,650 --> 00:03:37,510
or a broken application in your container.

76
00:03:37,510 --> 00:03:40,480
It's also not really possible to replace files

77
00:03:40,480 --> 00:03:43,810
which are currently executed like the server JS file.

78
00:03:43,810 --> 00:03:46,150
So copying a file into a container

79
00:03:46,150 --> 00:03:48,700
is not a good solution for that.

80
00:03:48,700 --> 00:03:51,020
We will instead learn about a better way

81
00:03:51,020 --> 00:03:53,430
of updating code in a container

82
00:03:53,430 --> 00:03:56,540
without rebuilding the image later.

83
00:03:56,540 --> 00:03:58,470
Nonetheless there could be scenarios

84
00:03:58,470 --> 00:04:00,700
where copying something into a container

85
00:04:00,700 --> 00:04:02,900
could be interesting for example,

86
00:04:02,900 --> 00:04:05,760
configuration files for a web server

87
00:04:05,760 --> 00:04:07,600
which you want to change.

88
00:04:07,600 --> 00:04:09,030
But in my opinion,

89
00:04:09,030 --> 00:04:11,390
copying something out of a container

90
00:04:11,390 --> 00:04:13,360
can also be quite interesting

91
00:04:13,360 --> 00:04:15,780
because if your container, for example,

92
00:04:15,780 --> 00:04:18,600
would generate a bunch of log files,

93
00:04:18,600 --> 00:04:23,260
you could use the Docker CP command to copy these log files

94
00:04:23,260 --> 00:04:26,700
out of your container onto your local host machine

95
00:04:26,700 --> 00:04:29,150
so that you can reach them there.

96
00:04:29,150 --> 00:04:32,800
So all of a sudden this container black box

97
00:04:32,800 --> 00:04:35,480
into which you normally can't look

98
00:04:35,480 --> 00:04:37,210
becomes more transparent

99
00:04:37,210 --> 00:04:39,410
because you can copy files and folders

100
00:04:39,410 --> 00:04:43,450
from inside the container to your local host system.

101
00:04:43,450 --> 00:04:45,570
And that could be an interesting use case

102
00:04:45,570 --> 00:04:47,380
for the Docker CP command

103
00:04:47,380 --> 00:04:49,930
which is why I also wanted to show this here.

104
00:04:49,930 --> 00:04:53,520
It's not a command we'll use very often in this course

105
00:04:53,520 --> 00:04:55,753
but still a command you should be aware of.

