youhoc
Docker

icon picker
Container Volume & Persistent Data

Containers are immutable (unchanged) and ephemeral (temporary).
2 ways for persistent data:
volume = special location outside of container, but inside docker folder
bind mount = link container path to host path (on same server)
NAME VOLUMES
# list all volume
docker volume ls

# check docker hub to get folder location
VOLUME [/var/lib/mysql]

# inspect and check 'Volumes' and 'Mounts' point
docker container inspect mysql

# source: real host path
# each container use different data folder, UNLESS we assign both to use 1 source
# this data live outside of containers, even if we remove containers (it's safe)
"Source": "var/lib/docker/volumes/xxxxxxxxxx/_data"

# destination: container path
VOLUME [/var/lib/mysql]

# set custom name for volume, instead of "xxxxxxxxxxx"
# assign both to use 1 source of named VOLUME
docker container run -d --name mysql1 -v mysql-data:/var/lib/mysql
docker container run -d --name mysql2 -v mysql-data:/var/lib/mysql

---
# create a new volume
docker volume create pspl

# check docker hub to get folder location
VOLUME [/var/lib/postgres/data]

# map old postgres version to pspl volume
docker container run -d --name pspl1 -v pspl:/var/lib/postgres/data postgres:9.1

# check log and stop old container
docker container stop pspl1

# map new container to pspl volume
docker container run -d --name pspl2 -v pspl:/var/lib/postgres/data postgres:9.2

SHOULD CREATE VOLUME IN DOCKERFILE
BIND MOUNT
edit file in macOS (host), see changes in Docker (container)
Can’t use in Dockerfile BUT is great for developer testing
# this shows how we can extend/change an existing official image from Docker Hub

FROM nginx:stable-alpine
# highly recommend you always pin versions for anything beyond dev/learn

WORKDIR /usr/share/nginx/html
# change working directory to root of nginx webhost
# using WORKDIR is preferred to using 'RUN cd /some/path'

COPY index.html index.html

# I don't have to specify EXPOSE or CMD because they're in my FROM

# when we run this in our current code folder /dockerfile-sample-2
docker container run -d --name nginx -p 80:80 -v $(pwd):/usr/share/nginx/html nginx

# we map the host / current working folder --> the container folder
# if we change files in code folder, nginx will show latest files

# same build, but only files inside that container
# can't develop further
docker container run -d --name nginx -p 80:80 nginx

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.