Skip to content
youhoc
  • Pages
    • Home
    • Modern App Guidelines
    • Linux
      • Day 1: Linux Distributions & Navigation
      • Day 2: User Management
      • Day 3: File Permission & Ownership
      • Day 4: Package Management
      • Day 5: Services Management
    • Javascript
      • JS The Weird Part
        • Execution Context
        • Types & Operators
        • Objects & Functions
        • Error Handling & Strict Mode
        • Typescript, ES6, Tra
      • Modern JS
        • JS in the Browser
        • Data Storage JSON
        • Modern JS
        • Advanced Objects & Methods
        • Webpack & Babel
        • Async
      • jQuery
        • In-depth Analysis of jQuery
      • React-ready JS
        • Arrow Function
        • Template Literals
        • Logical AND, OR, Ternary, Nullish Operators
        • Destructuring & Rest Operator
        • Array Method
        • Immutability and Spread Operator
        • Promises, Async/Await, Callback
    • PHP
      • gruntJS
      • composer
      • MySQL
    • Docker
      • Container Basics
      • Container Networking
      • Container Image
      • icon picker
        Container Volume & Persistent Data
      • Dockerfile
      • Docker Compose
      • Docker Registry
    • Node.js
      • 1. Installing & Exploring
      • 2. Core Modules
      • 3. Get User Input
      • File System & Input Arguments
      • 5. Express Web Server
      • 6. Deploy to Heroku & Github
      • Authentication
      • 7. Databases
      • 8. Rest API
    • ReactJS
      • React from Andrew
        • Summary from Next
        • 1. Basics
        • 2. React Components
        • 3. Webpack
        • 4. Styling with SCSS
        • 5. React Router
        • 6. React Hook
      • Modern React From The Beginning
        • Intro to JSX
        • Vite Build Tools
        • Basic Component Creation
        • Component State
        • Props & Component Composition
        • useState with Inputs & Form Submission
        • useEffect, useRef & Local Storage
        • Async / Await and Http Request in React
        • React Router: Declarative Mode
        • ContextAPI
        • React Router: Framework Mode
          • File-routing & HTML Layouts
          • Server-side Data Query
          • Links & Navigation
          • Loaders
    • Typescript
      • Type User vs UserProp
    • Payload CMS

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.