Mount volumes to persist data in local & initialize database in Docker

  • When a docker container is deleted, relaunching it from the image will start a fresh new container without any of the changes made in the previously running container
  • This happens because when we create a new container from an image, we add a new writable layer on top of the underlying stack of layers present in the base docker image.
  • All changes made to the running container, such as creating new tables, inserting data etc are written to the writable container layer
  • This writable layer does not persist after the container is deleted, therefore if you launch a postgreSQL container and create database and delete the container all the data will also get lost.
  • In order to save or persist the data even after a container is deleted , you should use volumes.
  • volumes are directories (or files) that are outside of the container and exist as normal directories and files on the host filesystem
  • Idea is to launch the container but mount the volume on host file system so that even if the container is deleted , data can continue to persist in the host file system.
  • I will explain it by mounting volume to a postgreSQL container and also initializing the database with some initialization scripts to create table and load data
  • create a docker-compose.yml file
services: 
  db: 
    container_name: docker_pg
    environment: 
      POSTGRES_DB: northwind
      POSTGRES_PASSWORD: changeme
    image: "postgres:12"
    ports: 
      - "5432:5432"
    volumes: 
      - /Users/nola/docker/postgres/docker-pg-vol/data:/var/lib/postgresql/data
      - /Users/nola/docker/postgres/init/initData.sql:/docker-entrypoint-initdb.d/
version: "3"
volumes: 
  pg-data: 
    driver: local
  • volumes: Users/nola/docker/postgres/docker-pg-vol/data:/var/lib/postgresql/data means mount the localhost data directory Users/nola/docker/postgres/docker-pg-vol/data to containers data directory /var/lib/postgresql/data. Write to data will happen now in this host location
  • Initialize database: To initialize database i have created the initial scripts in my local at /Users/nola/docker/postgres/init/initData.sql and mounted that to /docker-entrypoint-initdb.d/ location of the container
  • Rest of the entries in docker compose files are straight forward it has container name, database name, password, ports of container mapped to local host port and image name
  • With this when you execute
docker compose up
  • This will bring the new container up with data file mounted to host computer and initialization scripts

Thank you !

8 thoughts on “Mount volumes to persist data in local & initialize database in Docker

  1. I’ve been browsing online more than three hours these days, yet I never found any fascinating article like yours. It’s pretty worth sufficient for me. Personally, if all site owners and bloggers made just right content as you did, the web will be a lot more useful than ever before. “No nation was ever ruined by trade.” by Benjamin Franklin.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s