- Let's talk about sharing data between containers
and between containers and hosts.
Docker offers this feature called volumes.
Volumes are sort of like shared folders,
they're virtual discs that you can store data in
and share them between the containers and between containers
and the host, or both.
So they have two main varieties of volumes,
these virtual discs, available within Docker.
You've got persistent ones; you can put data there
and it will be available on the host, and when the container
goes away, the data will still be there.
And you have ephemeral volumes; these exist as long
as the container is using them, but when no container
is using them, they evaporate.
So they're sort of ephemeral, they'll stick around
as long as they're being used, but they're not permanent.
These are not part of images, no part of volumes
will be included when you download an image, and no part
of volumes is gonna be involved if you upload an image.
They're for your local data, local to this host.
So first, let's talk about sharing data
between the host and the container.
These are kind of like shared folders you're used
to when working with virtual machine systems,
such as Virtual Box, and let's also talk about sharing
a file into a container, it's very similar.
I'm gonna go ahead and make a folder
to share with my container.
Mkdir example.
Now I'm in /Users/arthurrulfeldt, that's my home directory
on this system, yours, of course, will be different.
I'm gonna go ahead and start a container to share
that folder into, so the usual docker run -ti
and here's the new part, -v and then I'm gonna specify
the entire path to my example folder.
/Users/arthurrulfeldt/example: if you don't specify
the entire path, this example will appear to work,
but at the end your data won't be there.
Then, after a colon, I'm gonna specify the path
within the container where the shared folder can be found.
After the volume specification,
then I'm gonna put the image to start with,
followed by the program to run, bash.
Now inside my container, I'm gonna do ls /shared-folder/
I see that there's nothing there.
Touch /shared-folder/my-data.
Now if I look at the shared folder,
I see my data from within the container.
Now, when I exit the container, if I look
at example, there I see my data.
The data survived the expiration
of the container that created it,
because it was shared with the host in a volume.
For sharing a single file, it's actually just the same.
Just pass the path to the file, instead of the path
to a folder, and it shares a file.
Just one little trick is make sure that the file exists
before you start the container, or Docker will assume
that it's going to be a folder and share it as a folder.
Alright, now let's talk about the more interesting case
of sharing data between containers.
This introduces the new argument
to Docker Run called volumes-from.
These are shared discs that exist only as long as they're
being used, and when they're shared between containers,
they'll be common for the containers that are using them.
So I'm gonna run docker run -ti -v /shared-data.
So I'm creating a volume for this container,
which is not shared with the host.
So, docker run -ti -v /shared-data this creates the folder,
and I'll run it on ubuntu and I'll run bash.
Alright, let's put some data in that shared folder.
(typing on keyboard)
Echo hello output redirected to /shared-data/data-file.
Alright, we have some data there.
Now, I'm gonna start up another Docker container,
first let me look up the name of that container
that I just started by running Docker PS.
Okay, copy.
Docker run, --volumes-from, this is the new part,
and I'm gonna put --volumes-from, and then paste in the name
of the machine that has the volumes I want to use.
Ubuntu,
Bash.
Now, if I go look in /shared-data/, it's a directory.
Ls /shared-data/, I see the data-file there.
And I'll add some more data.
Echo more redirected to /shared-data/more-data.
Now if I exit the original container,
my shared data is still there.
If I start up a third container,
do docker ps, find the name of that one.
I see the name of it there adoring_lumiere.
(typing on keyboard)
Docker run -ti --volumes-from,
paste it in.
I looked up the name of the container on the left here,
I've pasted it in to the right,
ubuntu bash.
(typing on keyboard)
/shared-data/ and I see the data here.
So this file, data-file originated in one container,
was inherited by a second container, and then inherited
by a third container, even though the machine
that created it is gone.
Now when I exit both of these containers,
this is the important part to understand,
right then, when I exited the last container using
this volume, it went poof!
And now it's gone.
That's what volumes are for, they are ephemeral.
They can be passed from one container to the next,
but they are not saved; they will eventually go away.