Stop container before using volume

This commit is contained in:
Stefan Scherer 2020-05-17 18:06:16 +02:00
parent 9864607e6b
commit ba1cc7351e
No known key found for this signature in database
GPG Key ID: 505AF50C5D02E697

View File

@ -8,7 +8,6 @@ When a container runs, it uses the various layers from an image for its filesyst
Each container also gets its own "scratch space" to create/update/remove files. Any
changes won't be seen in another container, _even if_ they are using the same image.
### Seeing this in Practice
To see this in action, we're going to start two containers and create a file in each.
@ -57,7 +56,6 @@ What you'll see is that the files created in one container aren't available in a
1. Go ahead and remove the first container using the `docker rm -f` command.
## Container Volumes
With the previous experiment, we saw that each container starts from the image definition each time it starts.
@ -71,8 +69,6 @@ the same files.
There are two main types of volumes. We will eventually use both, but we will start with **named volumes**.
## Persisting our Todo Data
By default, the todo app stores its data in a [SQLite Database](https://www.sqlite.org/index.html) at
@ -95,7 +91,9 @@ Every time you use the volume, Docker will make sure the correct data is provide
docker volume create todo-db
```
1. Start the todo container, but add the `-v` flag to specify a volume mount. We will use the named volume and mount
1. Stop the todo app container once again in the Dashboard (or with `docker rm -f <id>`), as it is still running without using the persistent volume.
1. Start the todo app container, but add the `-v` flag to specify a volume mount. We will use the named volume and mount
it to `/etc/todos`, which will capture all files created at the path.
```bash
@ -107,7 +105,7 @@ Every time you use the volume, Docker will make sure the correct data is provide
![Items added to todo list](items-added.png){: style="width: 55%; " }
{: .text-center }
1. Remove the container for the todo app. Use `docker ps` to get the ID and then `docker rm -f <id>` to remove it.
1. Remove the container for the todo app. Use the Dashboard or `docker ps` to get the ID and then `docker rm -f <id>` to remove it.
1. Start a new container using the same command from above.
@ -123,7 +121,6 @@ Hooray! You've now learned how to persist data!
and more! This will be especially important once you start running containers on multiple hosts in a clustered
environment with Swarm, Kubernetes, etc.
## Diving into our Volume
A lot of people frequently ask "Where is Docker _actually_ storing my data when I use a named volume?" If you want to know,
@ -152,12 +149,10 @@ need to have root access to access this directory from the host. But, that's whe
If you wanted to look at the actual contents of the Mountpoint directory, you would need to first get inside
of the VM.
## Recap
At this point, we have a functioning application that can survive restarts! We can show it off to our investors and
hope they can catch our vision!
However, we saw earlier that rebuilding images for every change takes quite a bit of time. There's got to be a better
way to make changes, right? With bind mounts (which we hinted at earlier), there is a better way! Let's take a look at
that now!
way to make changes, right? With bind mounts (which we hinted at earlier), there is a better way! Let's take a look at that now!