Part 6 - Data in Docker
Part 6 - Data in Docker
Follow 586K Followers · Editors' Picks Features Deep Dives Grow Contribute About
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one
This article is about using data with Docker. In it, we’ll focus on Docker
volumes. Check out the previous articles in the series if you haven’t yet.
We covered Docker concepts, the ecosystem, Dockerfiles, slimming down
images, and popular commands.
Spices
Quick FYI: this guide is current for Docker Engine Version 18.09.1 and
API version 1.39.
Temporary Data
Data can be kept temporarily inside a Docker container in two ways.
You have another option if you want better performance for saving
temporary data with Docker. If you don’t need your data to persist
beyond the life of the container, a tmpfs mount is a temporary mount that
uses the host’s memory. A tmpfs mount has the benefit of faster read and
write operations.
Many times you will want your data to exist even after the container is
long gone. You need to persist your data.
Persistent Data
There are two ways to persist data beyond the life of the container. One
way is to bind mount a file system to the container. With a bind mount,
processes outside Docker also can modify the data.
Bind mounts are difficult to back up, migrate, or share with other
Containers. Volumes are a better way to persist data.
Volumes
A Volume is a a file system that lives on a host machine outside of any
container. Volumes are created and managed by Docker. Volumes are:
persistent
encryptable
nameable
That’s a lot of useful functionality! Now let’s look at how you make a
Volume.
Volumes
Creating Volumes
Volumes can be created via a Dockerfile or an API request.
VOLUME /my_volume
Then, when the container is created, Docker will create the volume with
any data that already exists at the specified location. Note that if you Top highlight
Volumes also can be instantiated at run time from the command line.
my_volume .
Inspect
List Docker volumes with docker volume ls .
Remove
Then you can delete the volume with docker volume rm my_volume .
Dangling volumes are volumes not used by a container. You can remove
all dangling volumes with docker volume prune . Docker will warn you and
ask for confirmation before deletion.
system prune to clean up all your Docker resources. Then you should
should be able to delete the volume.
You will often use flags to refer to your volumes. For example, to create a
volume at the same time you create a container use the following:
target=/container/path/for/volume my_image
In the old days (i.e. pre-2017) 😏the --volume flag was popular.
Originally, the -v or --volume flag was used for standalone containers
and the --mount flag was used with Docker Swarms. However, beginning
with Docker 17.06, you can use --mount in all cases.
The syntax for --mount is a bit more verbose, but it’s preferred over --
volume for several reasons. --mount is the only way you can work with
services or specify volume driver options. It’s also simpler to use.
You’ll see a lot of -v ’s in existing code. Beware that the format for the
options is different for --mount and --volume . You often can’t just replace
a -v in your existing code with a --mount and be done with it.
The biggest difference is that the -v syntax combines all the options
together in one field, while the --mount syntax separates them. Let’s see
--mount in action!
--mount — options are key-value pairs. Each pair is formatted like this:
key=value , with a comma between one pair and the next. Common
options:
type — mount type. Options are bind , volume , or tmpfs . We’re all
about the volume .
source — source of the mount. For named volumes, this is the name
of the volume. For unnamed volumes, this option is omitted. The key
can be shortened to src .
Wrap
Recap of Key Volume Commands
docker volume create
docker volume ls
docker volume rm
Common options for the --mount flag in docker run --mount my_options
my_image :
type=volume
source=volume_name
destination=/path/in/container
readonly
Now that you’ve familiarized yourself with data storage in Docker let’s
look at possible next steps for your Docker journey.
Next steps
If you want to go deeper, check out Nigel Poulton’s book Docker Deep Dive
(make sure to get the most recent version).
If you want to do a lot of building while you learn, check out James
Turnbull’s The Docker Book.
I hope you found this series to be a helpful intro to Docker. If you did,
please share it with others on your favorite forums or social media
channels so your friends can find it, too! 😃
I’ve written some articles on orchestrating containers with Kubernetes
you can read here. I write about articles about Python, data science, AI,
and other tech topics. Check them out follow me if you’re into that stuff.
Every Thursday, the Variable delivers the very best of Towards Data Science: from
hands-on tutorials and cutting-edge research to original features you don't want to
miss. Take a look.
2K 6
Your home for data science. A Medium publication sharing concepts, ideas and
codes.
81 1
These days, data scientists are spoilt for choice when it comes to
selecting which method they will use on a database. Academic literature
has moved well beyond the well-known and once widely-used Ordinary
Least Squares method. Contrary to most other fields of research, new
theories and methods are usually implemented into business applications
relatively quickly, and as it stands today, the field of Time Series
Forecasting is arguably the best-researched and most-used one.
This article is not a deep dive into a single method that works best under
certain circumstances. Instead, I would like to show you that no
method…
103
One of the most attractive aspects of AWS is its ‘pay as you go’ pricing
model. But, sometimes these AWS costs spiral out of control. We, in this
article, came up with five AWS Cost Optimization Solutions and Tools that
will reduce AWS costs and make sure the spending stays in line with
business’ anticipated budgets.
“No-Cloud” policy will be nearly extinct by the year 2020. Also, by 2019,
more than 30% of the 100 largest vendors’ new software investments
will move to “Cloud-only” from Cloud-first. …
Source: Pixabay
114 1
You have been observing that since the past few years, happy employees
are the key profit generators of your company and in all these years, you
noted down the happiness index of all your employees and their
productivity. Now you have tons of this employees’ data just lying around
in excel files and you just recently heard “Data is the new oil. The
companies that will win are using math.” — Kevin Plank, founder and
CEO of Under Armour, 2016.
You are wondering if you could also win by somehow mathifying this data
that could predict the productivity of your…
316 2
Efficiently manage large Hacklang at Slack: A I’m a trendy developer — 8 Fantastic Resources for
lists in Cloud Firestore Better PHP I’m one of THOSE Tech Interview Prep
Dana Hartweg in The Startup Scott Sandler in Several People Mr. Anne Dev Michael Vinh Xuan Thanh in
Are Coding Better Programming
Testing guide for Cloud Guide: OKD 4.5 Single What do I do in between Bare Metal Beginner: A
Firestore functions and Node Cluster re:Invent live streams? Note
security rules Craig Robinson in The Startup
Build Lambda functions William McCann
Dana Hartweg in The Startup Leon Stigter in
HackerNoon.com