In the last lecture
we saw how we can inspect an image.
Okay that's nice to have
but what if you want to look into a container
or maybe not just look into it
but add something to it or extract something from it
whilst it is already running?
Docker has a command for this
and that's the CP command which stands for copy.
It allows you to copy files or folders
into a running container or out of a running container.
And here's how you could use this.
Let's say we have a folder named Dummy here
with a test text file
where I simply say, hello.
This is on my local host project folder
not inside of the container.
Now we want to copy that into an already running container.
And for this, I already started a container here
which has this name.
So we can now run Docker copy
and first of all specify the source.
So the folder or file we want to copy somewhere else.
In this case, I want to copy the Dummy folder
and now you could either point at a specific file
or add a dot to copy everything in that folder
which is what I will do.
Then you add a blank and then the destination
of this copy operation
so the path you want to copy to.
And data would be inside of your container.
For this you need the container name
because Docker of course needs to know
which container to copy this into.
You could have multiple running containers after all
so you specify your container name then a colon
and then the path inside of the container
you want a copy to.
Let's say Test but this name is up to you.
This path will be created if it doesn't exist yet.
If I now hit enter,
the content of Dummy should have been copied
into the Test folder inside of the container.
Okay, but how can we now see it?
Well, let's delete test text on our local host machine
and let's run this command again but differently now.
Now the first argument is the path and the container
and the second argument is our local folder.
So the destination is now our local folder.
If I now hit enter you see this test folder
which was created in the container
was now copied into Dummy.
And the test text file in there is all the back.
And if we would have wanted
to target this file specifically we could of course done so.
We can also run the command like this
and now it copies just the text file into the Dummy folder
without the Test folder.
This folder is only there because of the previous command,
this command.
And that's how you can copy files
between your local host machine and running containers.
Now this command can be useful for a couple of things.
It of course would allow you to add something to a container
without restarting the container and rebuilding the image.
Let's say our source code changed.
Normally we need to rebuild the image because of that
and restart the container.
We could of course also just copy the changed code
into the container.
Typically that is not what you do though
because it's pretty error prone.
It would be easy to forget a file which you changed
and you might then have a strange behavior
or a broken application in your container.
It's also not really possible to replace files
which are currently executed like the server JS file.
So copying a file into a container
is not a good solution for that.
We will instead learn about a better way
of updating code in a container
without rebuilding the image later.
Nonetheless there could be scenarios
where copying something into a container
could be interesting for example,
configuration files for a web server
which you want to change.
But in my opinion,
copying something out of a container
can also be quite interesting
because if your container, for example,
would generate a bunch of log files,
you could use the Docker CP command to copy these log files
out of your container onto your local host machine
so that you can reach them there.
So all of a sudden this container black box
into which you normally can't look
becomes more transparent
because you can copy files and folders
from inside the container to your local host system.
And that could be an interesting use case
for the Docker CP command
which is why I also wanted to show this here.
It's not a command we'll use very often in this course
but still a command you should be aware of.